CancellationToken doesn't get triggered in the Azure Functions -
i've got simple azure function:
public static class mycounter { public static int _timerround = 0; public static bool _isfirst = true; [functionname("counter")] //[timeoutattribute("00:00:05")] public async static task run([timertrigger("*/10 * * * * *")]timerinfo mytimer, tracewriter log, cancellationtoken token) { try { log.info($"c# timer trigger function executed at: {datetime.utcnow}"); if (_isfirst) { log.info("cancellation token registered"); token.register(async () => { log.info("cancellation token requested"); return; }); _isfirst = false; } interlocked.increment(ref _timerround); (int = 0; < 10; i++) { log.info($"round: {_timerround}, step: {i}, cancel request:{token.iscancellationrequested}"); await task.delay(500, token).configureawait(false); } } catch (exception ex) { log.error("hold on, exception!", ex); } } }
what i'm trying capturing cancellationtoken request event when app stops or code redeploy happened (host shutdown event).
btw, i've tried check iscancellationrequested property in loop well. never turns true.
the main requirement not loose operation/data during function deployments, want know app being stopped persist data processed once host started again after update.
based on code, tested on side, here test result:
from above screenshots, find subsequent rounds not handle cancellation callback except first round. fabio cavalcante commented, removed _isfirst
logical checking , found work rounds follows:
note: simulated shutdown of host disabling function when timertrigger triggered.
Comments
Post a Comment