Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Separate schedules for Azure webjob functions?

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.

like image 656
Brian Vallelunga Avatar asked Mar 31 '15 18:03

Brian Vallelunga


People also ask

How do I change my Azure WebJob schedule?

To change the schedule, or to Modify the CRON value just use the App Service Editor to modify the WWWROOT/App_Data/jobs/triggered/YOUR_WEBJOB_NAME/settings. job file; By the time of writing, App Service Editor is still in preview but it will work.

Can you schedule an Azure function?

Schedule a Function in Azure by Timer TriggerOne of the triggers for Azure Functions is the timer-trigger – allowing you to run a function on a schedule. With the timer trigger, you can use cron-expression to define when the function needs to run.

What is difference between WebJob and Azure function?

Webjobs run as background processes in the context of an App Service web app, API app, or mobile app whereas Functions run using a Classic/Dynamic App Service Plan.


2 Answers

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

like image 103
Thomas Avatar answered Nov 15 '22 06:11

Thomas


I ended up using Quartz.net with a continuous job for scheduling various activities internal to the web job. This has worked very well.

like image 44
Brian Vallelunga Avatar answered Nov 15 '22 05:11

Brian Vallelunga