Create a Scheduled Azure WebJob with PowerShell
There is a close relationship between Azure Scheduler and Azure WebJobs. Specifically Azure WebJobs does not have any internal support for scheduling, WebJobs relies on the Azure Scheduler to call into the *.scm.azurewebsites.net
website.
As such it is possible to use PowerShell cmdlets for these services to setup Azure WebJobs to be triggered on schedule using Azure Scheduler.
$location = "North Europe";
$site = New-AzureWebsite -Location $location `
-Name "amido-test-website";
$job = New-AzureWebsiteJob -Name $site.Name `
-JobName "amido-test-job" `
-JobType Triggered `
-JobFile ~\Desktop\test.zip;
$jobCollection = New-AzureSchedulerJobCollection `
-Location $location `
-JobCollectionName "amido-test-job-collection";
$authPair = "$($site.PublishingUsername):$($site.PublishingPassword)";
$pairBytes = [System.Text.Encoding]::UTF8.GetBytes($authPair);
$encodedPair = [System.Convert]::ToBase64String($pairBytes);
New-AzureSchedulerHttpJob `
-JobCollectionName $jobCollection[0].JobCollectionName `
-JobName "test" `
-Method POST `
-URI "$($job.Url)\run" `
-Location $location `
-StartTime "2014-01-01" `
-Interval 1 `
-Frequency Minute `
-EndTime "2015-01-01" `
-Headers @{ `
"Content-Type" = "text/plain"; `
"Authorization" = "Basic $encodedPair"; `
};
It's a little long winded so in plain english the above script does the following:
- Creates a new Azure Website.
- Creates and Uploads a new WebJob.
- Creates a new Azure Scheduler Job Collection.
- Generates the HTTP Basic Authentication header value.
- Creates a new Azure Scheduler HTTP Job that makes an authenticated request to the *.scm.azurewebsites.net API.
Hope this saves a few other developers from scratching their head trying to figure this one out.
Richard Slater
I started writing code on a second hand ZX Spectrum before I was 10, then over the years, migrated from the Spectrum to visual-basic on Windows. Though college I picked up turbopascal and delphi; University gave me the chance to work with ada and java, had the time to pick up some c, php, sql, html, css and a few others along the way. To make a living I am Principal Engineer with Amido, we are relatively small London-based Start-up working on identity and microservices projects for some of the largest companies in the UK. I live and breathe devops which means I am a vocal proponent of best practices for our software development teams focusing on continuous-deployment, Systems Thinking, Reducing Feedback Cycles, configuration-management, infrastructure-as-a-code and Cyber Security. I have commercial experience with a wide range of technologies from coffeescript to powershell and from c# to html5 in azure, google-cloud-platform and amazon-web-services. If you think you might recognize my name, then it is likely we have interacted through the atom-editor tag, one of my open source projects or through my time as Lead Developer on EVEMon: A character manager for the MMORPG Game EVE Online. I'm a firm believer in open-source and wherever possible I look to release code under free and open source licenses, you can take a look at some of my work on my GitHub, Amido's GitHub and atom.io.
Updated on June 04, 2022Comments
-
Richard Slater almost 2 years
I am trying to create an Azure WebJob to send a
BrokeredMessage
to an Azure ServiceBus Topic, the actual act of creating and sending the message is trivial however I have been unable to find a way to automate the creation of the Scheduled WebJob.The automated workflow should work as follows:
- Create a new Azure Website [Done]
- Create a new triggered Azure WebJob uploading a PS1 file [Done]
- Create a new Azure Scheduler Job Collection [Proven Concept]
- Create a new Azure Scheduler Job that triggers the WebJob
The Azure Management Portal provides a nice UI for this functionality, which under the covers creates an Azure WebJob in the selected WebSite, an Azure Scheduler Job Collection and an Azure Scheduler Job:
There doesn't appear to be an analogous mechanism for creating a scheduled Azure WebJob with the Azure Service Management PowerShell Module. It is certainly possible to create new WebJobs, Azure Scheduler Collections and Jobs - however I have no idea what URL or Storage Queue the Azure Scheduler is posting to to schedule Azure WebJobs.
-
Schenz almost 9 yearsOk, I made use of your PowerShell (thank you for the head start), but have a question. I now see a WebJob and Scheduler Job in the Azure Portal, but the WebJob says that it is On-Demand. The Job is running via the Scheduler, but would like for it to be linked like a manually created Job.
-
makhdumi over 8 years@Schenz although it's been over a year since your question, I'm adding the answer for others finding this. To make it look linked like a manually created job, name the scheduler "<sitename>-<jobname>". Azure does the linking purely by checking the job name.
-
Haymaker87 over 8 yearsHi @Richard-Slater, I'm trying to replicate what you are doing here. Where do you get your jobfile from? Do you mind sharing with me your process for deploying/generating
-JobFile ~\Desktop\test.zip;
?