Standard Azure Functions (not Durable Functions) have a maximum timeout of 10 minutes on a consumption plan. This is the same timeout for the new Function Runtime ~4 (missing in the link). With the Function Runtime ~3, however, the timeout can be significantly longer (endless).

Perform Time-Consuming Calculation

To keep the function running, I use a modification of the Fibunacci sequence. My variant contains another calculation element, which quickly results in a time consuming load:

đť‘“n = đť‘“n-1 + đť‘“n-2 – đť‘“n-3

It also has the advantage that the input value is the same as the output value, i.e. đť‘“(24) is 24. Therefore, the correctness is easy to check and there are no large numbers that can cause memory overflows. In most examples, the Implementation looks like:

public static int ConsumeCPU(int n){
    if(n<=2) return n;
    return ConsumeCPU(n-1) + ConsumeCPU(n-2) - ConsumeCPU(n-3);
}

In most cases, I work with the number 40, that generates load of ~2 minutes or with 44, that generates load of ~20 minutes.

The implementation takes 2 parameters:

  • value: the input of the time consuming function (e.g. 40 for ~2 minutes)
  • runs: for the iterations, the calculation was called

The runs are iterated in a simple loop:

...
for(int i=0; i<runs; i++){
    var res = ConsumeCPU(value);//.GetAwaiter().GetResult();
    log.LogInformation($"  default function has run round {i+1} with result {res} ({System.DateTime.UtcNow.ToLongTimeString()})");
}
...

Therefore to perform a 30 minutes Azure Function run, you can use the parameters: runs=15 & value=40

Configure functionTimeout in host.json

The key for a long running Azure Function is to deactivate the timeout property. This can be done by setting the property functionTimeout to a value of -1, according to the host.json configuration:

{
    "version": "2.0",
    "logging": {
        "applicationInsights": {
            "samplingSettings": {
                "isEnabled": true,
                "excludedTypes": "Request"
            }
        }
    }
    ,"functionTimeout": "-1"
}

But the property will not be accepted by Azure Function Runtime ~4, therefore it is not possible, to run endless azure functions on runtime ~4.

Forbidden configuration of functionTimeout with “-1”

Results

As a result, different runtimes were tried out. Functions that run for less than 24 hours are just as easy as functions that run for longer than 24 hours. The prerequisite is always the functionTimeout configuration to -1.

If the host.json is not adjusted, the typical 5 minute timeout error occurs:

Config~3~4
runs: 10
value: 40
2021-12-18T17:56:59Z …

2021-12-18T18:01:59Z [Error] Timeout value of 00:05:00 exceeded by function ‘TimeconsumingCalculation’…
2021-12-18T13:54:57Z …

2021-12-18T13:59:57Z [Error] Timeout value of 00:05:00 exceeded by function ‘TimeconsumingCalculation’ …

After setting functionTimeout to -1, more long running functions are possible. But runtime ~4 is not usable.

Config~3~4
runs: 10
value: 40
2021-12-18T16:25:36Z …

2021-12-18T16:44:44Z Succeeded, Duration=1148241ms

👉 ~0,3h
runs: 65
value: 44
2021-12-18T21:43:15Z …

2021-12-19T16:54:38Z Succeeded, Duration=69082817ms

👉 ~19,2h
runs: 84
value: 44
2021-12-19T20:29:03Z …

2021-12-21T01:52:10Z Succeeded, Duration=105786688ms

👉 ~29,4h

You can see the details afterwards in the portal and in the logs:


Sourcecode at GitHub in the folders DefaultFunction-RT3 and DefaultFunction-RT4.