Separate schedules for Azure webjob functions?

折月煮酒 提交于 2020-01-02 05:03:50

问题


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.



来源:https://stackoverflow.com/questions/29376271/separate-schedules-for-azure-webjob-functions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!