这些变量是否被垃圾收集?

发布于 2025-01-04 01:38:45 字数 1348 浏览 0 评论 0原文

我正在尝试让 Quartz.net 调度程序正常工作,但我不知道为什么它不会触发安排在未来日期的作业。我已经使用每分钟触发一次的 cron 触发器进行了测试,它可以工作(工作和所有),所以我知道这不是我的工作代码的问题。

我尝试过的事情:

  1. 使 ISchedulerFactory 成为全局静态变量
  2. 使 IScheduler 成为全局静态变量
  3. 我在 Application_Start 的末尾添加了电子邮件通知,这样我就知道它何时触发
  4. 每次我对调度程序代码进行更改时,我都会重新启动应用程序,它会触发我的通知电子邮件,所以我知道它已重新启动。

我正在共享托管环境上运行该程序(不确定这是否会对它产生任何影响)。我的猜测(这只是猜测)是某些东西正在被垃圾收集,但我不确定是什么以及为什么。

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:

  1. Making the ISchedulerFactory a global static variable
  2. Making the IScheduler a global static variable
  3. I have added an email notification to the end of the Application_Start so I know when it is firing
  4. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

安穩 2025-01-11 01:38:45

如果没有请求传入,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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文