在运行中的Azure函数中检测更新/重新部署 [英] Detecting update/redeployment inside running Azure function

查看:46
本文介绍了在运行中的Azure函数中检测更新/重新部署的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

WebJobs SDK 支持CancellationToken参数传递给SDK用来通知正在运行的主机关闭功能的函数.我注意到,如果将应用程序/功能替换为新版本,则不会通知正在运行的功能.如果功能被设计为可以运行更长的时间,那么这将是一个问题,因为您最终可能会在不知不觉中运行旧的"逻辑.

The WebJobs SDK supports passing in a CancellationToken argument to a function which is used by the SDK to notify running functions of a host shutdown. I have noticed though that running functions are not notified if the app/function is replaced by a new version. This is a problem if functions are designed to run for a longer period of time, since you could end up having "old" logic running without knowing it.

例如,如果您创建一个将永久运行(或直到取消运行)的简单函数:

For example, if you create a simple function that will run forever (or until cancelled):

using System;
using System.Threading;

public static async Task Run(string input, TraceWriter log, CancellationToken token)
{
    while (!token.IsCancellationRequested) 
    {    
        token.ThrowIfCancellationRequested();
        log.Info($"Input: {input}");
        await Task.Delay(2000, token).ConfigureAwait(false);
    }
}

如果运行该代码,然后以某种方式更新代码并重新部署(或保存,如果您在Azure Functions门户中,则可以保存),将会看到该函数已重新编译,但运行中的函数实例并未停止并将继续执行以前的代码版本,直到您手动将其禁用为止.

If you run that, and then update the code in some way and redeploy (or save, if you're in the Azure Functions portal), you'll see that the function is recompiled but the running function instance is not stopped and will continue executing the previous version of the code until you disable it manually.

是否有某种方法可以检测正在运行的函数内部的重新部署?

Is there some way of detecting a redeployment inside a running function?

推荐答案

当Function App主机关闭/重新启动时,Azure Functions会尊重WebJobs SDK CancellationToken模型.但是,在上述仅更新功能代码本身的情况下,不会会触发主机重启,这就是为什么令牌不会被取消的原因.作为一种优化,我们仅在绝对必要时重新启动主机,并且在简单代码更新的情况下,我们可以保持主机运行,并在任何新的调用上使用新代码.

Azure Functions does honor the WebJobs SDK CancellationToken model when the Function App host is shutting down/restarting. However, in the scenario above where you're simply updating the function code itself, that will not trigger a host restart, which is why the token isn't being cancelled. As an optimization, we only restart the host when absolutely necessary, and in the case of simple code updates we can keep the host running, and just use the new code on any new invocations.

这对于正常的功能场景非常有效,在这种情况下,功能调用不会永远"存在.我想说while(true)函数是一种反模式-函数不是针对那种类型的东西而设计的,您可能会以更好的方式完成您的方案.与此相关的是,GA之前的函数执行还将设置一些时间限制(可配置).

That works well for normal Function scenarios where Function invocations don't live "forever". I'd say a while(true) function is an anti-pattern - Functions isn't designed for that type of thing, and you can likely accomplish your scenarios in a better way. On a related note, will also be putting some time limits (configurable) in place for Function executions before GA.

这篇关于在运行中的Azure函数中检测更新/重新部署的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆