如何在计时器触发的函数上强制执行单例行为? [英] how to enforce singleton behavior on a timer-triggered function?

查看:94
本文介绍了如何在计时器触发的函数上强制执行单例行为?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我的Azure函数查看SFTP文件夹,并将新文件移动到其父文件夹.

I want my Azure Function to look at an SFTP folder, and move new files to their parent folders.

我的脚手架看起来像这样.

My scaffolding looks like this.

public static class OnMoveFilesHttpTriggered
{
    [FunctionName("OnMoveFilesHttpTriggered")]
    public static void Run([TimerTrigger("0 */5 * * * *")]TimerInfo myTimer, ILogger log)
    {
        log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
    }
}

我们如何强制在任何给定时间运行此函数的一个且只有一个实例?

推荐答案

根据设计,TimerTrigger使用

It's by design that TimerTrigger uses the Singleton feature of the WebJobs SDK to ensure that only a single instance of your triggered function is running at any given time. Hence we don't have to do anything else to enforce it.

有关详细信息,请查看 wiki .

Check the wiki for details.

当JobHost启动时,对于您的每个TimerTrigger函数,都会获取一个Blob租约(Singleton Lock).这种分布的锁可确保在任何时候仅运行计划功能的单个实例.如果当前未租用该函数的Blob,则该函数将获取租约并立即按计划开始运行.如果无法获取Blob租约,则通常意味着该函数的另一个实例正在运行,因此该函数不会在当前主机中启动.

When the JobHost starts up, for each of your TimerTrigger functions a blob lease (the Singleton Lock) is taken. This distrubuted lock ensures that only a single instance of your scheduled function is running at any time. If the blob for that function is not currently leased, the function will acquire the lease and start running on schedule immediately. If the blob lease cannot be acquired, it generally means that another instance of that function is running, so the function is not started in the current host.

这篇关于如何在计时器触发的函数上强制执行单例行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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