简体   繁体   English

如何在计时器触发的函数上强制执行单例行为?

[英]how to enforce singleton behavior on a timer-triggered function?

I want my Azure Function to look at an SFTP folder, and move new files to their parent folders. 我希望Azure函数查看SFTP文件夹,然后将新文件移动到其父文件夹。

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}");
    }
}

How do we enforce that there will be one and only one instance of this function running at any given time? 我们如何强制在任何给定时间运行此函数的一个且只有一个实例?

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. 根据设计,TimerTrigger使用WebJobs SDK的Singleton功能来确保在给定的时间仅运行触发函数的单个实例。 Hence we don't have to do anything else to enforce it. 因此,我们无需执行其他任何操作即可执行它。

Check the wiki for details. 查看Wiki了解详细信息。

When the JobHost starts up, for each of your TimerTrigger functions a blob lease (the Singleton Lock) is taken. 当JobHost启动时,对于您的每个TimerTrigger函数,都会使用一个Blob租约(单例锁)。 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. 如果当前未租用该函数的Blob,则该函数将获取租约并立即按计划开始运行。 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. 如果无法获取Blob租约,则通常意味着该函数的另一个实例正在运行,因此该函数不会在当前主机中启动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 定时器触发的 function 在 Azure function 应用程序(C#) - Timer-triggered function in Azure function app (C#) 计时器触发的Azure功能未在计划的时间触发,并且因Azure存储超时异常而失败 - Timer-triggered Azure Function not triggered on scheduled time and failed with Azure storage timeout exception 如果 function 的实例已经在执行,定时器触发的 Azure function 是否会执行? - Does a timer-triggered Azure function execute if an instance of the function is already executing? 具有定时器触发的 Azure 功能的令牌缓存 - Token Cache with Timer-Triggered Azure Functions 在本地运行一次计时器触发的 Azure 函数的最简单方法是什么? - What is the simplest way to run a timer-triggered Azure Function locally once? 如何配置 singleton - 队列触发 Azure Function - How to configure for singleton - Queue Triggered Azure Function 如何在附加行为中使用单例计时器来更改 ListViewItems 上的背景颜色? - How to use a singleton timer in an attached behavior to change the background color on ListViewItems? 参数名称s:计时器触发功能的异常 - Parameter name s: exception with Timer Triggered Function 定时器触发单元测试 Azure Function:提供测试数据 - Unit Tests for timer triggered Azure Function: provide test data System.Timer 作为单例 - System.Timer as a Singleton
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM