0

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!

objectively C
  • 960
  • 9
  • 25
  • 1
    It seems that your ApplicationPool will be recycled. – Rabban May 16 '17 at 14:34
  • Possible duplicate of [How to keep quartz .net's scheduler alive?](http://stackoverflow.com/questions/23452920/how-to-keep-quartz-nets-scheduler-alive) – Rabban May 16 '17 at 14:34
  • 2
    just to note that it's a bad idea to keep an asp.net running without pool recycling you should take a look for another solution like windows service – BRAHIM Kamel May 16 '17 at 14:50

2 Answers2

0

+1 to @BRAHIMKamel. You could consider using Hangfire to do some job processing in your ASP.NET application.

Be aware though, that as long as you rely on IIS application pool, your job could be terminated and restarted at any time. That means that your job code must tolerate multiple subsequent executions. Regarding e-mails that means that you should check whether you've already sent what you want before attempting to actually send message.

starteleport
  • 1,231
  • 2
  • 11
  • 21
  • how would you recommend checking that the jobs have been executed or not? – objectively C May 16 '17 at 17:10
  • @objectivelyC, I would save the user's last sent email's date and check it before sending. The key consideration is whether duplicated messages are tolerable or it's better not to send them at all than have them doubled. If the former is the case, you should update last sending date after the e-mail is sent, otherwise, before that. – starteleport May 16 '17 at 18:44
0

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.

objectively C
  • 960
  • 9
  • 25