Continuous Azure WebJob with Http Triggered Functi

2019-06-24 07:29发布

问题:

I currently have an azure webjob that performs a daily sync from one database to another, but would like to add the ability to manually trigger the sync as well. I have set up the functions as follows in the webjob project:

public static void SyncData([TimerTrigger("0 0 5 * * *", RunOnStartup = false)] TimerInfo timerInfo) { }

[NoAutomaticTrigger]
public static Task SyncAll(TraceWriter log){ }
[NoAutomaticTrigger]
public static Task SyncBranches(TraceWriter log){ }
[NoAutomaticTrigger]
public static Task SyncCustomers(TraceWriter log){ }
[NoAutomaticTrigger]
public static Task SyncInventory(TraceWriter log){ }

I can see the functions in the Kudu dashboard under the webjob, but am not sure how I can trigger the functions with an http request as listed on the MS documentation (here):

http://<yourapp>.azurewebsites.net/api/<funcname> 

When I make a request to that endpoint I get a 404 response - what do I need to do in order to trigger those functions manually via http request?

Thanks, Ryan

回答1:

We can use the Kudu WebJob API to start or stop a continuous job

POST https://{user}:{password}@{sitename}.scm.azurewebsites.net/api/continuouswebjobs/{job name}/start

We can get user and password info from the Azure WebApp publishSetting file. And We can download the file from the portal

Test the Rest API using the fiddler

Note: How to create Azure function please refer to the document.

Update:

is there a way I can trigger a specific function in that continuous job?

Based on my experience, I can't find a way or Rest API to trigger a sepcific function in continuous job directly.

My work around is that we can use Webjob QueueTrigger. According the queue message info to trigger the sepcific function. We can create a WebJob with QueueTrigger

The following is my detail steps according to your code.

1.Create an Azure storage queue for trigger

2.Create a Webjob project and add the following code

 public static void SyncData([QueueTrigger("backup")] string logMessage, TextWriter logger)
    {
        Console.WriteLine($"Start time:{DateTime.Now}");
        switch (logMessage.ToLower())
        {
            case "syncall":
                SyncAll(logger);
                break;
            case "syncbranches":
                SyncBranches(logger);
                break;
            case "synccustomers":
                SyncCustomers(logger);
                break;
            case "syncinventory":
                SyncInventory(logger);
                break;
            default:
                Console.WriteLine("Default case");
                break;
        }
        Console.Write($"Endtime:{DateTime.Now}");

    }
    [NoAutomaticTrigger]
    public static Task SyncAll(TextWriter log)
    {

        Console.WriteLine("SyncAll :"+DateTime.Now);
        return null;
        //await Task.Delay(10);
    }

    [NoAutomaticTrigger]
    public static Task SyncBranches(TextWriter log)
    {
        Console.WriteLine("SyncBranches :" + DateTime.Now);
        return null;
    }

    [NoAutomaticTrigger]
    public static Task SyncCustomers(TextWriter log)
    {
        Console.WriteLine("SyncCustomers :" + DateTime.Now);
        return null;
    }

    [NoAutomaticTrigger]
    public static Task SyncInventory(TextWriter log)
    {
        Console.WriteLine("SyncInventory :" + DateTime.Now);
        return null;
    }

3.Use Azure Storage Queue REST API to create a queue message.

4.Check result the from the console.



回答2:

You're looking at the doc for Azure Functions but it looks like you're using WebJobs (which is different from Azure Functions).

I suggest using Azure Functions instead, in which case your scenario becomes more straightforward.