Asp.Net Core - Start background task and ensure only one are running - asp.net-core

I would like to start a long running task from an API controller and return 200 when the task is started.
I want only one task running and if another request are coming in, the controller should check if there is already a task running.
It the task is running just forget the request. If not start a new task.
Was thinking of using this code fire and forget in ASP.NET Core with dependency alive to start the task. Then I need some thread safe place to store a IsRunning variable.

Have you checked Hangfire
?
In can be run in cluster mode and also you can query to check if a specific task in running.

Related

Nlog in hangfire jobs deadlock

I am using hangfire as a custom workflow engine in a dotnet core application, which works perfectly fine until I start logging. I log to DB as a requirement. So normal database target. I enabled a sync=true. Once I turn logging on I start getting deadlocks. All is using DI. Again all works fine without log, once I add a rule to write the logs in db I get a deadlock. It’s not even a lot of concurrent jobs, only 11 in my test. Help pls?! Already lost a week on this and still stuck.
I am not sure it’s related, but I am also getting this at the same time: asynchronous exception: timeout flushing all targets

Android service needs to periodically access internet

I need to access the internet periodically (every 5 minutes or so) to update a database with a background service. I have tried the following using Android 8 or 9:
Use a boot receiver to start a JobService.
In onStartJob of the class that extends JobService, create a LocationListener and request a single location update.
Have onStartJob schedule the job again to run in 5-10 minutes.
Return true from onStartJob.
In the OnLocationChanged of the LocationListener, write to a local file, and start a thread to make a PHP request to update the database.
Everything works fine while the underlying process is running. When the process dies, the service keeps periodically updating the local file, but the the URLConnection.getResponseCode() now throws an exception - java.net.ConnectionException: failed to connect to ...
Is there a way to get around this using the above approach? If not, how can I have a background service access the internet even after the underlying process dies?

Service Task running without starting a process

In my process, I have a service task(which is calling a method of a java class) running in loop after a certain interval of time. But before I go to tasklist and start the process I can see in the console that my process is running as the method is being called. The method has simple sysout statements. Is it because of the camunda internal architecture? or I have not configured properly?

Get actual Environment status through TFS api

We use Visual Studio LabManagement infrustructure and TFS API to run our automation test from builds. Some of our test require reboot of system before they can be launched.
We noticed strange behaviour. When QtAgent is stoped by system (and not with a button Stop, pressed by human being), Test Controller is not notified automatically and environment remains in Ready state (status).
It goes to error when we invoke WaitForEnvironmentReady activity to determine that test machine is really ready to execute test (or other word connection between QtAgent service and Test Controller has been already established).
Is there a way to force Test Controller to get actual information about state of Environment without waiting for timeout is expired.
The controller will come to know about the agent going down after a timeout period specified in the testcontroller configuration xml. Once the controller detects this the environment state should move to notready on next refresh.
You can change these defaults as needed
See - http://msdn.microsoft.com/en-us/library/ff934570.aspx

Parallel.For Termination vb.net

I have a service that scans network folders using a parallel.for method.
However recently I am finding if I stop the service then while windows says the service is stopped the process is still running in task manager. However it is at 0 cpu and the memory does not change. If I try and end the task (even a force in command prompt) it just says access denied and i have to reboot the server.
What would be the best way to make sure everything terminates?
I thought of adding a global Boolean that in the stop procedure it turns true and part of my parallel code will check for that and call s.stop.
Thank you
In brief, when your service is stopped, it needs to cancel all pending and running operations, then wait for those operation to actually finish. Check out the MSDN reference for Task Cancellation.