2

Apparently there used to be a way to create custom timers for Azure Functions, by creating a class that inherits from TimerSchedule and defining the custom logic in its GetNextOccurrence method.

We've got an MSDN post about it,, we've got a StackOverflow answer about it, and even some official sample code that uses custom timers.

None of it works.

None of these samples have a FunctionName attribute on their functions, which is needed (in the current SDK version, at least; not sure how long this has been the case) to make the Azure Function job actually run. And in the current SDK, any function that has both a FunctionName and a TimerTrigger that uses a custom timer type will fail to build with an error like this:

System.NotImplementedException: Property 'ScheduleType' on attribute 'TimerTriggerAttribute' is not supported in Azure Functions.

Apparently this used to work, but it doesn't now, and I'm having a heck of a time finding any relevant information on what the correct way is to do it now. Does anyone know what the new way is to set up a custom timer?

Mason Wheeler
  • 77,748
  • 42
  • 247
  • 453

2 Answers2

2

This can't be done with timers directly. Queues might be the right choice here: The previous run would have to create a message and send it to the queue. Make sure that the message becomes visible only after a specified time.

An alternative is to use Durable Functions, in particular an "eternal orchestration". Check out the example in the official docs here, where a job gets kicked off after a specified amount of time after the previous one has finished.

Thomas Schreiter
  • 700
  • 8
  • 24
0

Those are all in relation to web jobs - Azure Functions are not web jobs.

Azure Webjobs vs Azure Functions : How to choose

That said Azure Functions do have similiar timer functionaility - it even uses web job Nuget packages.

Timer trigger for Azure Functions

The following example shows a C# function that is executed each time the minutes have a value divisible by five (eg if the function starts at 18:57:00, the next performance will be at 19:00:00):

[FunctionName("TimerTriggerCSharp")]
public static void Run([TimerTrigger("0 */5 * * * *")]TimerInfo myTimer, ILogger log)
James Wood
  • 16,370
  • 2
  • 39
  • 80
  • This example is a cron expression. It's exactly what I'm trying to figure out how to not use, because the logic I need can't be expressed as a cron expression and requires a custom timer that can compute any arbitrary time. – Mason Wheeler Feb 25 '19 at 00:39
  • @masonwheeler looked at using a `TimeSpan` instead? What are you trying to achieve? – James Wood Feb 25 '19 at 08:23
  • I'm trying to achieve a programatically-derived schedule that is not fixed because the amount of time before the next iteration runs depends on the results of previous iterations. Anything with a fixed value, whether it be a `TimeSpan` or a cron expression, will not work. – Mason Wheeler Feb 25 '19 at 11:41
  • (And before you respond with any specific details about how serverless works that would make that difficult to get right, yes, I'm aware of all of that. I still need arbitrary, programmatic schedule generation.) – Mason Wheeler Feb 25 '19 at 11:48