Running your Azure WebJobs with the Kudu API

I have a love-hate relationship with the Azure portals. Some days it's fast and lets me do exactly what I want; other days, like today, I want more control over how my stuff runs. The Stuff I'm referring to here is the many WebJobs I've deployed to Azure since they released the feature and SDK many months ago. I've written in a previous post on how you could automate the deployment and scheduling of your jobs, but now I want to move a few steps beyond that. Notably, I want to run my triggered jobs on-demand from my interfaces, and I want the potential to update them. Luckily this is possible using the Kudu WebJobs REST API. Kudos to the Kudu team (eh??) for creating some excellent documentation to get me started!

In this post, I will focus on simply running my jobs on-demand outside of the Azure portal. In this example, I've already uploaded my WebJob via zip file or as part of my deployment to Azure, so it is ready and waiting to be run.

Get your creds

The simplest way to authenticate against the API is to use my site's WebDeploy deployment credentials. You can download them from your web app's dashboard in either portal; look for: "Get||Download publish profile." The downloaded .publishsettings file should look something like this:

The parts you're interested in are the userName and userPWD on the <publishProfile> element for the Web Deploy credentials. You'll use these to authenticate using basic authentication against the API.

Run your WebJob

In code, I'm using HttpClient to create a new POST request using Basic Auth:

In Postman, I'm building a new POST request to https://mysiteslot.scm.azurewebsites.net/api/triggeredwebjobs/moJobName/run and adding Basic Auth under the Authorization tab using the credentials from my .publishsettings file.

If successful, expect a 202 Accepted response with no Body from the server and your job running.

Get your job's history

Finally, you may want to check the status of your job, especially if it's long-running, or make sure it ran successfully. A simple call to /api/triggeredwebjobs/myJobName/history will return the following JSON: