这些变量是否被垃圾收集?
我正在尝试让 Quartz.net 调度程序正常工作,但我不知道为什么它不会触发安排在未来日期的作业。我已经使用每分钟触发一次的 cron 触发器进行了测试,它可以工作(工作和所有),所以我知道这不是我的工作代码的问题。
我尝试过的事情:
- 使 ISchedulerFactory 成为全局静态变量
- 使 IScheduler 成为全局静态变量
- 我在 Application_Start 的末尾添加了电子邮件通知,这样我就知道它何时触发
- 每次我对调度程序代码进行更改时,我都会重新启动应用程序,它会触发我的通知电子邮件,所以我知道它已重新启动。
我正在共享托管环境上运行该程序(不确定这是否会对它产生任何影响)。我的猜测(这只是猜测)是某些东西正在被垃圾收集,但我不确定是什么以及为什么。
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
// construct a scheduler factory
ISchedulerFactory schedFact = new StdSchedulerFactory();
// get a scheduler
IScheduler sched = schedFact.GetScheduler();
sched.Start();
// construct job info
JobDetail jobDetail = new JobDetail("myJob", null, typeof(Recorder));
jobDetail.JobDataMap["domain"] = "www.mydomain.com";
jobDetail.JobDataMap["userId"] = "2";
// Create trigger (everything is in UTC!!!)
CronTrigger cronTrigger = new CronTrigger("Schedule");
cronTrigger.StartTimeUtc = TriggerUtils.GetEvenSecondDate(DateTime.UtcNow);
cronTrigger.TimeZone = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time"); // run in pacific timezone
cronTrigger.CronExpressionString = "0 30 13 ? * MON-FRI *";
sched.ScheduleJob(jobDetail, cronTrigger);
}
I am trying to get Quartz.net scheduler to work, but I don't know why it is not firing for jobs scheduled at a future date. I have tested with a cron trigger that fires every minute and it works (job and all), so I know it isn't a problem with my job code.
Things I have tried:
- Making the ISchedulerFactory a global static variable
- Making the IScheduler a global static variable
- I have added an email notification to the end of the Application_Start so I know when it is firing
- Every time I make changes to the scheduler code I restart the app and it fires my notification email, so I know it was restarted.
I am running this program on a shared hosting environment (not sure if that would have any effect on it). My guess (and this is only a guess) is something is being garbage collected, but I'm not sure what and why.
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
// construct a scheduler factory
ISchedulerFactory schedFact = new StdSchedulerFactory();
// get a scheduler
IScheduler sched = schedFact.GetScheduler();
sched.Start();
// construct job info
JobDetail jobDetail = new JobDetail("myJob", null, typeof(Recorder));
jobDetail.JobDataMap["domain"] = "www.mydomain.com";
jobDetail.JobDataMap["userId"] = "2";
// Create trigger (everything is in UTC!!!)
CronTrigger cronTrigger = new CronTrigger("Schedule");
cronTrigger.StartTimeUtc = TriggerUtils.GetEvenSecondDate(DateTime.UtcNow);
cronTrigger.TimeZone = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time"); // run in pacific timezone
cronTrigger.CronExpressionString = "0 30 13 ? * MON-FRI *";
sched.ScheduleJob(jobDetail, cronTrigger);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果没有请求传入,ASP.NET 进程可以被 IIS 关闭,因此不会触发任何代码。
这就是为什么网络应用程序不是类似服务(始终运行)行为的良好来源的原因。
我已经在带有页面/网络服务的网络应用程序中看到了这一点,该服务通过
cURL
工具从外部进行 ping 操作。如果您有兴趣进一步调试,请在
Application_End
中添加一些通知,以确保进程在计时器触发您的计划作业之前实际关闭。ASP.NET process can be shut down by IIS if no requests are coming in, so none of this code would be fired.
This is the reason why web-apps are not a good source of service-like (always running) behavior.
I've seen this implemented in a web-app with a page/web service which gets pinged via
cURL
tool from the outside.If you're interested in debugging this further, add some notification in
Application_End
just to make sure that the process is actually shut down before the timer fires your scheduled job.