Search code examples
c#asp.netquartz.netquartz

ASP .NET - Quartz Scheduler only Executes jobs on user visit


I need my emails to be sent automatically from my .NET application. However, when scheduling jobs for the future, the jobs are only executing when a user visits the site.

Global.asax.cs

void Application_Start(object sender, EventArgs e)
{
    // Code that runs on application startup
    RouteConfig.RegisterRoutes(RouteTable.Routes);
    BundleConfig.RegisterBundles(BundleTable.Bundles);
    JobScheduler.Start();
}

Job Scheduler (ScheduledTasks.cs)

public class EmailJob : IJob
{
    public void Execute(IJobExecutionContext context)
    {
        BDL.Utilities.sendEmailNotification(to, subject, body);
    }
}

public static void start()
{
    foreach (AspNetUser user in users)
    {
        IJobDetail job = JobBuilder.Create<EmailJob>().WithIdentity((jobidentity))
            .UsingJobData("to", user.Email)
            .UsingJobData("subject", subject)
            .UsingJobData("body", bodyholder).Build();

        ITrigger trigger = TriggerBuilder.Create()
            // schedule for 1 day in the future
            .StartAt(expdate.AddDays(1)))
            .WithIdentity(triggerid)
            .Build();

        scheduler.ScheduleJob(job, trigger);
    }
}

I have scheduled a trigger for 1 day in the future. This trigger should cause my job to execute 1 day in the future. However, for some reason this job will only execute if a user visits my site on the day the job is scheduled to fire. How can I make these Jobs be executed automatically by Quartz???

EDIT: My goal is to accomplish this without messing with the Application Pool threads. The accepted answer shows that this question is about merely replacing the user interaction with automated scripts. Whereas the duplicate asks for a way to maintain the application pool threads, and in Java I might add!


Solution

  • I was able to come up with a more elegant solution. Using the tool Zapix I was able to schedule my website to be quality checked every 20 minutes. Zapix simply visited the site and received and http response. By using Zapix, it mimicked the functionality of manually visiting the website to trigger the emails.