Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Continuous Azure WebJob with Http Triggered Functions

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

like image 765
rwdial Avatar asked Mar 15 '17 18:03

rwdial


People also ask

What is difference between WebJob and azure function?

Summary. Azure Functions offers more developer productivity than Azure App Service WebJobs does. It also offers more options for programming languages, development environments, Azure service integration, and pricing. For most scenarios, it's the best choice.

Can a azure function have multiple triggers?

An Azure function can have multiple triggers, but the function must be explicitly configured to use multiple triggers. The Azure function can have a single trigger, or it can have multiple triggers.

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

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

enter image description here

Test the Rest API using the fiddler

enter image description here

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

enter image description here

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.

enter image description here

like image 164
Tom Sun - MSFT Avatar answered Sep 17 '22 15:09

Tom Sun - MSFT