简体   繁体   English

在Quartz.Net中计划Web服务-Windows Service或Singleton

[英]scheduling web services in Quartz.Net - windows service or singleton

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. 我有一个Web应用程序,该应用程序在后端运行大量数据处理作业,因此我不想停滞服务器,而是想安排作业并决定让Quartz.Net来运行。

The obvious thing to do I guess is to create the scheduler as a windows service. 我想要做的显而易见的事情是将调度程序创建为Windows服务。 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). 我正在运行类似的东西,但是由于某些原因,发现Windows服务有些痛苦(其中之一是当崩溃时需要手动重新启动)。

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: 如果我有几个ashx后端调用,可以这样声明吗:

     public static IScheduler Scheduler { get; private set; } 

in each ashx and it would be a singleton across all backend calls? 在每个ashx中,所有后端调用都将是一个单例吗?

  • Can I check for running jobs scheduler.GetCurrentlyExecutingJobs(); 我可以检查正在运行的作业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. 我想使用Quartz.Net的主要原因是为了限制同时运行的作业数量,因此基本上我只创建了现在运行的简单作业。 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(); 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. 可能是因为IIS关闭了它,或者因为如果没有作业在运行,我的代码也将其关闭了。 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? 我猜想,我可以在阵列中维护自己的作业列表并像这样进行管理-还是可以直接通过Quartz进行操作? Or do I better make the scheduler permanent like this - IIS app pool recycle + quartz scheduling ? 还是最好像这样使调度程序永久化-IIS应用程序池回收+石英调度

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... 如果我有几个ashx后端调用,可以这样声明吗?

If you have multiple classes defined for your ashx, then each class will get a new instance of the scheduler. 如果您为ashx定义了多个类,则每个类将获得调度程序的新实例。 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. 如果在global.asax文件中声明了一个公共静态属性,那么可以,您将拥有一个单例。

  1. Can I check for running jobs scheduler.GetCurrentlyExecutingJobs(); 我可以检查正在运行的作业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 或者,您可以调用shutdown方法并使用重载,该重载允许您指定是否等待所有作业完成

  1. Does scheduler.GetCurrentlyExecutingJobs(); 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. 您可以通过虽然调用get从调度计划作业的列表GetJobKeys ,然后也许还称GetJobDetail如果需要的话。

  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. 您可以调用Start来启动调度程序,但是如果已经调用Shutdown,则不能调用它,因此,必须创建一个新的调度程序实例,然后再启动它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM