0

I have a web app that's running a bunch of data processing jobs on the backend, so rather than bogging down the server I want to schedule jobs and decided to give Quartz.Net a go.

The obvious thing to do I guess is to create the scheduler as a windows service. I have something similar running, but find a windows service is a bit of a pain for several reasons (one of them being if it crashes it needs to be manually restarted).

I was thinking that a more elegant way would be to create the service as a singleton, my questions would be:

  • if I have several ashx backend calls, can I just declare it like this:

    public static IScheduler Scheduler { get; private set; }
    

in each ashx and it would be a singleton across all backend calls?

  • Can I check for running jobs scheduler.GetCurrentlyExecutingJobs(); and the shut the scheduler down if there aren't any?

  • The main reason I want to use Quartz.Net is to limit the amount of jobs running concurrently, so basically I just create simple jobs that run now. So from what I read, if there are no threads available, a job will be rejected and then handled once threads become available again. Does scheduler.GetCurrentlyExecutingJobs(); return all rejected/waiting jobs as well?

    • How do I start the scheduler if it's down? It could be down because IIS closed it or because my code closed it if no jobs are running. Ideally, if I add a new job, the scheduler will start up. I guess, I could maintain my own list of jobs in an array and manage it like that - or is there a way to do it via Quartz directly? Or do I better make the scheduler permanent like this - IIS app pool recycle + quartz scheduling?
Community
  • 1
  • 1
christian.stock
  • 181
  • 2
  • 15

1 Answers1

1

Since you have multiple questions, I've copied them down here for clarity:

  1. if I have several ashx backend calls, can I just declare it like this...

If you have multiple classes defined for your ashx, then each class will get a new instance of the scheduler. So no, just declaring a private static property on each one does not give you a singleton. If you declare a public static property in your global.asax file, then yes, you'll have yourself a singleton.

  1. Can I check for running jobs scheduler.GetCurrentlyExecutingJobs(); and the shut the scheduler down if there aren't any?

Yes. Alternatively you can call the shutdown method and use the overload that allows you to specify whether to wait until all jobs are finished

  1. Does scheduler.GetCurrentlyExecutingJobs(); return all rejected/waiting jobs as well?

No, just currently executing ones. You can get a list of scheduled jobs from the scheduler though by calling GetJobKeys and then perhaps calling GetJobDetail if needed.

  1. How do I start the scheduler if it's down?

You can call Start to start the scheduler, but you can't call it if you've already called Shutdown, so, you'd have to create a new instance of the scheduler and then start it.

jvilalta
  • 6,679
  • 1
  • 28
  • 36
  • Do you have a suggestion for my requirement? [link](https://stackoverflow.com/questions/74403074/asp-net-core-2-1-calling-rest-api-in-a-schedule) – Cenk Nov 17 '22 at 17:35