Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a Scheduled Azure WebJob with PowerShell

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:

  1. Create a new Azure Website [Done]
  2. Create a new triggered Azure WebJob uploading a PS1 file [Done]
  3. Create a new Azure Scheduler Job Collection [Proven Concept]
  4. 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:

Screenshot: Step 2 of New Scheduled Web Job task in Azure Management Portal

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.

like image 792
Richard Slater Avatar asked Aug 14 '14 11:08

Richard Slater


People also ask

How do you add a WebJob to Azure?

Deploy to Azure App ServiceIn Solution Explorer, right-click the project and select Publish. In the Publish dialog box, select Azure for Target, and then select Next. Select Azure WebJobs for Specific target, and then select Next. Above App Service instances select the plus (+) button to Create a new Azure WebJob.

Are Azure WebJobs deprecated?

Azure WebJobs are deprecated, but still in use. They are being phased out in favor of Azure Functions. Azure Functions is a more up-to-date and feature rich service which offers a greater degree of flexibility and control.


1 Answers

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. Relationship between Azure WebSites, WebJobs and Scheduler

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:

  1. Creates a new Azure Website.
  2. Creates and Uploads a new WebJob.
  3. Creates a new Azure Scheduler Job Collection.
  4. Generates the HTTP Basic Authentication header value.
  5. 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.

like image 97
Richard Slater Avatar answered Oct 03 '22 00:10

Richard Slater