dotnet-labs / ServiceWorkerCronJob

Schedule Cron Jobs using HostedService in ASP.NET Core
MIT License
264 stars 72 forks source link

How to manually start/stop jobs #8

Closed amyrprv closed 3 years ago

amyrprv commented 3 years ago

How I can have control over jobs and manually start/stop themes?

changhuixu commented 3 years ago

I utilize the Service and make an API endpoint so that the front end can trigger the process defined in my Service class. I haven't tried to stop the process when it is running. But you probably can try to pass a cancellation token to the process.

The service I referred to is like the one below.

https://github.com/dotnet-labs/ServiceWorkerCronJob/blob/69938658e45ac1aee8efcba2ce505e0deee48929/ServiceWorkerCronJobDemo/Services/MyCronJob2.cs#L30-L32

In the CronJob class, you call the service to do work. You can do the same thing in your controller, and you don't need to initiate a scope in the controller. Then admins can click a button in a web page to run the job on demand.