John Marsing
John Marsing

Reputation: 111

run a WebJob from Asp.Net MVC azure website

What I want to do is run WebJob from my Asp.Net MVC azure website periodically without having to go through https://portal.azure.com/

Can I get the rest calls using Azure Resource Explorer for Azure Webjobs so that I can run it? I looked but I don't see the one I created

Upvotes: 2

Views: 1764

Answers (3)

Pat
Pat

Reputation: 580

Use the Kudu Api... Here is a rough example...

ASCIIEncoding encoder = new ASCIIEncoding();
byte[] data = encoder.GetBytes(serializedObjHere);

var url = "https://{site}.scm.azurewebsites.net/api/triggeredwebjobs/{jobname}/run";
var request = (HttpWebRequest)WebRequest.Create(url);
request.Credentials = new System.Net.NetworkCredential("{username}", "{password}"); //you can get these in yoru publish profile
request.AllowWriteStreamBuffering = true;
request.Method = "POST";
request.Timeout = 60000; //60 seconds
request.KeepAlive = false;
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
var response = (HttpWebResponse)request.GetResponse();
response.Close();
response.Dispose();

Upvotes: 0

Thomas
Thomas

Reputation: 29746

When you configure a Console Application for WebJobs deployment, Visual Studio installs the Microsoft.Web.WebJobs.Publish NuGet package and stores scheduling information in a webjob-publish-settings.json file in the project Properties folder of the WebJobs project. Here is an example of that file:

{
  "$schema": "http://schemastore.org/schemas/json/webjob-publish-settings.json",
  "webJobName": "WebJob1",
  "startTime": "2014-06-23T00:00:00-08:00",
  "endTime": "2014-06-27T00:00:00-08:00",
  "jobRecurrenceFrequency": "Minute",
  "interval": 5,
  "runMode": "Scheduled"
}
  • Another option TimerTriggerAttribute : using the latest WebJobs SDK, which supports triggering job functions on schedule, based on the same CRON expression format. You can use it to schedule your job :

    public static void TimerJob([TimerTrigger("00:01:00")] TimerInfo timerInfo, TextWriter log)
    {
        log.WriteLine("Scheduled job fired!");
    }
    
  • Other option : use the Webjob API:

    Invoke a triggered job**
    POST /api/triggeredwebjobs/{job name}/run
    
  • Last option: use the Azure Management Libraries from Azure Web Jobs.

Upvotes: 0

David Ebbo
David Ebbo

Reputation: 43203

Resource Explorer doesn't currently show this, but I can give you the API here. To invoke a triggered WebJob, do a POST like this (with empty request body):

POST /subscriptions/{sub}/resourceGroups/{rg}/providers/Microsoft.Web/sites/{site}/triggeredwebjobs/{jobname}/run?api-version=2015-08-01

There is also a way to do it via the Kudu API instead of going through ARM, and is documented here.

The difference between the two is how you authenticate, in the first case using ARM token, and in the second case using basic auth publishing credentials. But they both do the same in the end.

Upvotes: 3

Related Questions