0

I've an HTTP triggered azure function (Java) that performs a series of action. In tmy application UI, I've a button which triggers this function to initiate the task. Everything works as expected.

Now I've to perform this operation in user defined schedules. That is from the UI, user can specify the interval (say every 3 Hrs) at which the function need to be executed. As this schedule will be custom and dynamic I cannot rely on the timer-triggered azure functions. Also the same function need to be executed at different intervals with different input parameters.

How can I dynamically create schedules and invoke the azure function on the scheduled time? Does Azure have an option to run the function with specific events something like (AWS cloud watch rule + lambda invocation)?

EDIT: Its different from the suggested question as it changes the schedule of an existing function.And I think configuring a new schedule will break the previously configured schedules for the function. I want to run the same function in different schedules as per the user configuration and should not break any of the previous schedules set for the function.

Master Po
  • 1,497
  • 1
  • 23
  • 42
  • Does this answer your question? [Dynamically set schedule in Azure Function](https://stackoverflow.com/questions/45564848/dynamically-set-schedule-in-azure-function) – Murray Foxcroft Jan 03 '20 at 10:35
  • @master Po I am also having similar situation. May I know the solution that you went further with? – Garuda Apr 26 '21 at 11:01
  • @Garuda That feature with user defined schedules removed from our solution. Its running with fixed(hard-coded) schedules defined inside the functions – Master Po May 05 '21 at 14:54
  • Thank you @master Po. For others who are looking out for similar solution, we ended up using azure service bus feature to deliver messages on a scheduled time. We compute the next execution time and as azure service bus to make the message come alive on the queue at the specified time. Then we have an azure function that picks up the message and processes it. – Garuda May 10 '21 at 14:08

2 Answers2

0

You can have a try to modify the function.json, change the cron expression in function.json. Please refer to the steps below:

  1. Use Kudu API to change function.json https://github.com/projectkudu/kudu/wiki/REST-API

    PUT https://{functionAppName}.scm.azurewebsites.net/api/vfs/{pathToFunction.json}, Headers: If-Match: "*", Body: new function.json content.

  2. Then send request to apply changes.

    POST https://{functionAppName}.scm.azurewebsites.net/api/functions/synctriggers

Hury Shen
  • 14,948
  • 1
  • 9
  • 18
  • But The user may again schedule the function to work in a different interval say every 5 hrs. So I need to run this thing in what are the schedules the user wants. Without affecting the previous schedules that the user defined. If I modify the function.json, i think it will broke the previous schedules of the function. – Master Po Jan 03 '20 at 09:54
  • Hi @MasterPo I'm not so clear about your concern, but per my understanding, why not develop a button for the user and let them to input the cron expression, and then you can do the updating of function.json by the input cron expression from users in the backend. – Hury Shen Jan 04 '20 at 04:06
  • That is what exactly I have in front end. User specifies the schedule. But in the back end I've only one azure-function (That receives a recordId and after the function execution the result is updated in DB). Every time the user configures a schedule he provides the recordId and the cron. I've to invoke the azure function with that recordId on the specified schedule. I think if I update the function.json, the function will only get executed on the modified schedule. What I expect is if the user define 3 crons the same function should invoke in all the 3 schedules without fail. – Master Po Jan 06 '20 at 06:17
  • Hi @MasterPo, for your requirements, when the user submit the first schedule request, you can modify the cron expression with 0 0 */3 * * * in function.json. When the user submit the second schedule request, you can modify the cron expression with 0 0 */3,*/5 * * * in function.json. Please have a try~ – Hury Shen Jan 06 '20 at 06:44
  • Hi @MasterPo, may I know if your problem was solved ? If the solution helps your problem, could you please mark my answer as "accepted", thanks. – Hury Shen Jan 06 '20 at 09:23
  • that solution wont fix my problem.. The user may set a schedule like "7.00 PM every tuesday and monday" or "every 5 hrs". How can I work with this? modifying the timer cron expression won't help me here :( – Master Po Jan 06 '20 at 18:30
  • Hi @MasterPo I'm sorry that azure function time trigger can't implement this requirement. In my opinion, if you want to schedule multiple cron expression which are complex, you can just create another time trigger function but not in one function. Or maybe you can reduce the functionality of your function, for example: only allow the user to modify the interval "hour" of the cron expression. – Hury Shen Jan 07 '20 at 01:50
0

You can use a durable function for this, applying the Monitor Pattern (shamelessly copied from this MSDN documentation). This orchestration function sets a dynamic timer trigger using context.CreateTimer.

Code is in C#, but hopefully there is something you can use here.

[FunctionName("MonitorJobStatus")]
public static async Task Run(
    [OrchestrationTrigger] IDurableOrchestrationContext context)
{
    int jobId = GetJobId();
    int pollingInterval = context.GetInput<int>();
    DateTime expiryTime = GetExpiryTime();

    while (context.CurrentUtcDateTime < expiryTime)
    {
        var jobStatus = await context.CallActivityAsync<string>("GetJobStatus", jobId);
        if (jobStatus == "Completed")
        {
            // Perform an action when a condition is met.
            await context.CallActivityAsync("SendAlert", machineId);
            break;
        }

        // Orchestration sleeps until this time.
        var nextCheck = context.CurrentUtcDateTime.AddSeconds(pollingInterval);
        await context.CreateTimer(nextCheck, CancellationToken.None);
    }

    // Perform more work here, or let the orchestration end.
}
sjokkogutten
  • 2,005
  • 2
  • 21
  • 24