[英]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.