hangfire: Cancel already running recurring job - c#

I have problem with cancellation recurring job.
When i invoke RecurringJob.RemoveIfExist(myJobId) then job is removed from database, but already ran job is not cancelled.
In my case i have while(!cancellationToken.IsCancellationRequest) background job
And i also use ThrowIfCancellationRequested but job still not cancelling
When i use BackgroundJob instead ofRecurringJob then all woks fine.
I tried to to use BackgroundJob.Delete(myJobId) but is not work too.
Also, i passed my own cancellation token and CancellatioToken.None
can anyone help me?

Related

hangfire recurring job: prevent parallel execution of the same job

I have implemented a recurring job which needs to run every minute. Now and then the job has a hickup as an API-Call is involved which can take a bit longer to response. So that the job is enqued a second time, even though it wasn't finished in the previous run.
My Question:
How do I prevent a Hangfire job to run if another instance of the same job is already running?
Thank you!

HostingEnvironment.QueueBackgroundWorkItem - Clarification?

I've read Stephen's article about fire and forget background actions in Asp.net.
It is not recommended to use Task.Run for fire-and-forget because Asp.net doesn't know that you've queued a task.
So if a recycle is about to occur, the task has no way of knowing it.
That's where HostingEnvironment.QueueBackgroundWorkItem gets in.
It will know that a recycle is about to happen and will invoke the Cancellation Token.
But!
FWIK - Background tasks are being "terminated" once the main thread has finished.
That means that if a request gets in (a new thread is being created/fetched) and it invokesTask.Run , and the response has finished (but Task has not) then the Task will be terminated.
Question:
Does QueueBackgroundWorkItem solve this problem ? or does it only exist to warn about recycle?
In other words, if there's a request which runs QueueBackgroundWorkItem and the response has finished, will the QueueBackgroundWorkItem continue to execute its code?
The docs say: " independent of any request", but I'm not sure if it answers my question though
According to the documentation, this method tries to delay application shutdown until background work has completed.
Differs from a normal ThreadPool work item in that ASP.NET can keep track of how many work items registered through this API are currently running, and the ASP.NET runtime will try to delay AppDomain shutdown until these work items have finished executing.
Also, it does not flow certain contexts which are associated with the current request and are inappropriate for request-independent background work:
This overloaded method doesn't flow the ExecutionContext or SecurityContext from the caller to the callee. Therefore, members of those objects, such as the CurrentPrincipal property, will not flow from the caller to the callee.
In ASP.NET there is no way to make sure that background work ever completes. The machine could blue screen, there could be a bug terminating the worker process, there could be a timeout forcing termination and many other things.
Or, your code could have a bug and crash. That causes the queued work to be lost as well.
If you need something executed reliably, execute it synchronously before confirming completion, or queue it somewhere (message queue, database, ...).
That means that if a request gets in (a new thread is being created/fetched) and it invokesTask.Run, and the response has finished (but Task has not) then the Task will be terminated.
No, Task.Run works independently of HTTP requests. In fact, there is no way to cancel a Task except if the task's code cancels itself.

ThrowIfCancellationRequested() vs Abort()

What am I trying to achieve?
I am trying to cancel a long running task from within another task using a CancellationToken. It's impossible for me to handle the cancellation and throw in the long running task because it will never touch the code that handles the cancellation (the task processes a bad regex pattern that takes forever anyway this is not important). I tried to handle from within another Task and poll for the cancellation request, and when I call ThrowIfCancellationRequested() it actually throws in that thread. So the long running task is still alive and hanging.
How I solved this
Well instead of using token's ThrowIfCancellationRequested() i acually called Abort() on the long running task's Thread and it works like charm.
And my question is: I am pretty sure it's not very elegant and I wanted to know if it is OK what I did there and how else can I approach this situation?
Thread.Abort is evil because it is highly dangerous. Cancellation in .NET (and any other platform I'm aware of) is cooperative. Either make the action cancel itself on demand or isolate it so that you can just ignore it.

Monitoring Edge.js Tasks from C#

My goal is to be able to cancel a long running Node.js task that has been called from C# using Edge.js.
When using tasks normally, you can specify a cancellation token, give your task some time to execute, then cancel it if it is going beyond your time limit.
I can't figure out how to define/call the Edge.Func Node function/task and pass in a cancellation token to be able to kill the process if it goes too long. Maybe I'm thinking about this from the wrong angle...
Any insight would be most helpful, thanks!

How do I cancel a schedule for a task?

I need to create a scheduled task. For that I'm trying to learn how to do that, and am currently thinking of using TaskSchedulerClass from TaskScheduler Class Library. I got a code examples here (and there's another one I might try here). But - I don't want to actually run it without making sure I know how to cancel it when needed.
So my question is: How do I cancel such a task-registration in code?
(Just "Try it!" won't suffice in this case. Because the point is I don't want to get stuck with something I can't stop.)
What about TryDequeue Method?
It's attempts to dequeue a Task that was previously queued to this scheduler.
You need to use the same Task you sent to QueueTask function.
If you don't have the instance of this Task you can try and retrieve it using GetScheduledTasks, but it stated it in use For debugger support only

Categories