I'm creating a new Azure WebJob project -- which appears to be a polished version of a console app that can run as a web job.
I want this job to run based on a schedule but in the Main()
method -- see below -- Microsoft gives you the host.RunAndBlock()
for the job to run continuously.
Do I need to change that if I want the job to run at regularly scheduled intervals?
static void Main()
{
var host = new JobHost();
// The following code ensures that the WebJob will be running continuously
host.RunAndBlock();
}
There are 2 ways that I know of for scheduling a web job instead of making it run continiously:
1.Create a scheduled WebJob using a CRON expression
2.Create a scheduled WebJob using the Azure Scheduler
You can find the documentation for both ways here:
https://azure.microsoft.com/en-us/documentation/articles/web-sites-create-web-jobs/
I think you need
RunAndBlock
in case of Scheduled or Continuous but you can remove it if you have your job as on-demandWhen using the Azure WebJobs SDK you can use
TimerTrigger
to declare job functions that run on a schedule. For example here's a function that runs immediately on startup, then every two hours thereafter:You can get
TimerTrigger
and other extensions by installing the Microsoft.Azure.WebJobs.Extensions nuget package. More information onTimerTrigger
and the other extensions in that package and how to use them can be found in the azure-webjobs-sdk-extensions repo. When using theTimerTrigger
, be sure to add a call toconfig.UseTimers()
to your startup code to register the extension.When using the Azure WebJobs SDK, you deploy your code to a Continuous WebJob, with AlwaysOn enabled. You can then add however many scheduled functions you desire in that WebJob.
An easy way to trigger a WebJob on a schedule would be to code it as a regular console application, and just add a 'settings.job' with the cron based scheduling configuration to the project. For example, the following definition would trigger it every 5 minutes:
No need to use JobHost, just make sure your WebApp is configured as 'Always On'. You should then deploy the job as a triggered WebJob.