Question

I have a web application and I am trying to setup a trigger to start when the application has started and then trigger every 5 minutes

Everything is within Global.asax - seemed like the right place to put it:

public class Global : HttpApplication
{
    public static StdSchedulerFactory SchedulerFactory;
    public static IScheduler Scheduler;
    public static ITrigger ImageTrigger;

    protected void Application_Start(object sender, EventArgs e)
    {
        SchedulerFactory = new StdSchedulerFactory();
        Scheduler = SchedulerFactory.GetScheduler();

        Scheduler.Start();

        ImageTrigger = TriggerBuilder.Create()
                                     .WithIdentity("ImageTrigger", "Group1")
                                     .StartNow()
                                     .WithSimpleSchedule(x => x.RepeatForever().WithIntervalInMinutes(5))
                                     .Build();

        var imageJob = JobBuilder.Create<DownloadImages>()
                                     .WithIdentity("DownloadImages" , "Group1")
                                     .Build();
        Scheduler.ScheduleJob(imageJob, ImageTrigger);
    }
...
}

So I assumed having a simple schedule use .WithIntervalInMiniutes() with cause the job to be invoked or have it got it massively wrong?

P.s. I have also tried:

        AlertTrigger = TriggerBuilder.Create()
                                     .WithIdentity("AlertTrigger", "Group1")
                                     .StartNow()
                                     .WithCronSchedule("0 0/1 * * * ?")
                                     .Build();

Followed by screaming at the computer!

Thanks in advance for your help.

Matt

Was it helpful?

Solution

I've tried your code and it works properly.
I don't think a web-service is the best option to run scheduled jobs cause of it's nature.

I would suggest you to read ASP.NET Application Life Cycle.

Application_Start

Called when the first resource (such as a page) in an ASP.NET application is requested. The Application_Start method is called only one time during the life cycle of an application. You can use this method to perform startup tasks such as loading data into the cache and initializing static values. You should set only static data during application start. Do not set any instance data because it will be available only to the first instance of the HttpApplication class that is created.

ASP.NET worker processes running in IIS are shutdown and recycled after a certain amount of time of inactivity. You can change this behavioral, though.

Another interesting article can be read here.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top