Is it possible to set up separate schedules for individual non-triggered functions in an Azure webjob? I ask because I have half a dozen individual tasks I'd like to run at different times of the day and at different intervals and don't want to create a separate project for each.
问题:
回答1:
Yes you can using the TimerTriggerAttribute
- Azure WebJobs SDK Extensions
- nuget download page
Here is a sample code:
public class Program
{
static void Main(string[] args)
{
JobHostConfiguration config = new JobHostConfiguration();
// Add Triggers for Timer Trigger.
config.UseFiles(filesConfig);
config.UseTimers();
JobHost host = new JobHost(config);
host.RunAndBlock();
}
// Function triggered by a timespan schedule every 15 sec.
public static void TimerJob([TimerTrigger("00:00:15")] TimerInfo timerInfo,
TextWriter log)
{
log.WriteLine("1st scheduled job fired!");
}
// Function triggered by a timespan schedule every minute.
public static void TimerJob([TimerTrigger("00:01:00")] TimerInfo timerInfo,
TextWriter log)
{
log.WriteLine("2nd scheduled job fired!");
}
}
You can also use a CRON expression to specify when to trigger the function:
Continuous WebJob with timer trigger and CRON expression
回答2:
You can write a plain old console app with an endless loop that pauses at the end of each iteration with Thread.Sleep(). Each time the loop wakes up it checks a schedule that it keeps internally and calls whatever functions are supposed to run at that time. The schedule can be stored in app.config so you can change it without recompiling the WebJob, just upload a new app.config and restart it.
Or if you prefer you can omit the endless loop and schedule the WebJob at a fixed interval, say every 30 minutes, and have it check its internal schedule each time it runs.
回答3:
I ended up using Quartz.net with a continuous job for scheduling various activities internal to the web job. This has worked very well.