2

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

Jehof
  • 34,674
  • 10
  • 123
  • 155
Matt
  • 2,691
  • 3
  • 22
  • 36
  • The WithSimpleSchedule looks fine. You haven't just forgotten to schedule the job - ie Scheduler.ScheduleJob(imageJob, ImageTrigger); – sgmoore Sep 13 '12 at 17:06
  • Sorry, did have that in but forgot to add it in the post – Matt Sep 14 '12 at 07:19
  • @Matt: Are you trying to schedule your jobs in a web service? – LeftyX Sep 14 '12 at 08:27
  • You should be able to call ImageTrigger.GetNextFireTimeUtc(); and ImageTrigger.GetFireTimeAfter(dt) to see if the job is been scheduled correctly. – sgmoore Sep 14 '12 at 08:37
  • Try enabling logging, if running in debug mode try setting Visual Studio to break on all exceptions. It might even be that your job constructor throw error or something goes wrong (asynchronously) otherwise (not having no-arg public constructor in job etc). – Marko Lahma Sep 14 '12 at 09:45
  • +1 Thanks for the complete example of how to build a simple trigger. – ashes999 Apr 26 '13 at 10:15

1 Answers1

0

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.

LeftyX
  • 35,328
  • 21
  • 132
  • 193