简体   繁体   English

从 Azure 门户安排计时器触发的 Azure function

[英]Schedule a timer-triggered Azure function from the Azure Portal

I have an Azure Function (built in Visual Studio 2019, and running on the .NET Core 3.x stack in Azure) that is supposed to be triggered by a timer to run eg once per night.我有一个 Azure Function(内置于 Visual Studio 2019,并在 .NET Core 3.x 堆栈上运行),它应该由例如每晚运行一次的计时器触发

I can easily create the function and define the schedule as an NCRONTAB expression like this:我可以轻松创建 function 并将计划定义为 NCRONTAB 表达式,如下所示:

[FunctionName("MyFunctionName")]
public void Run([TimerTrigger("0 15 3 * * 1-5")]TimerInfo myTimer, ILogger log)
{
    // Azure function code here ....
}

Works like a charm - BUT I'd like to be able to define the schedule in the Azure Portal - not in my function code - to handle eg DEV vs. TEST vs. PROD situations.像魅力一样工作 -我希望能够在 Azure 门户中定义时间表 - 而不是在我的 function 代码中 - 以处理例如 DEV 与 TEST 与 PROD 情况。 On the DEV and TEST platforms, I might want to run this several times a day - in production maybe only once a week.在 DEV 和 TEST 平台上,我可能想每天运行几次——在生产中可能每周只运行一次。

But with this approach, once the schedule is set - it's set, I cannot change it in the Azure Portal (all the input fields are grayed out / editing is disabled).但是使用这种方法,一旦设置了时间表 - 设置了,我就无法在 Azure 门户中更改它(所有输入字段都显示为灰色/编辑被禁用)。

I was hoping I might be able to just skip the NCRONTAB expression in the declaration of my Azure function - like so:我希望我可以在我的 Azure function 的声明中跳过 NCRONTAB 表达式 - 就像这样:

[FunctionName("MyFunctionName")]
public void Run([TimerTrigger()]TimerInfo myTimer, ILogger log)
{
    // Azure function code here ....
}

and then specify the actual CRON expression in the Azure portal - but no go, the CRON expression is mandatory....然后在 Azure 门户中指定实际的 CRON 表达式 - 但没有 go,CRON 表达式是强制性的....

So am I asking for too much here?所以我在这里要求太多了吗? Or is there a way to schedule this outside of my actual code base?或者有没有办法在我的实际代码库之外安排这个?

As the post provided by silent, we can use ConfigurationManager.AppSettings["key"] .正如静默提供的帖子,我们可以使用ConfigurationManager.AppSettings["key"] But I think you can refer to the solution below which is more simple.但我认为您可以参考下面更简单的解决方案。

1. Edit your function code in local, do not specify a cron expression in the code, use %myTimerCron% instead. 1.在本地编辑您的 function 代码,不要在代码中指定 cron 表达式,使用%myTimerCron%代替。

在此处输入图像描述

2. Publish it from local to azure, and then add a key/value in the "Application settings". 2.从本地发布到azure,然后在“应用设置”中添加一个key/value。 在此处输入图像描述

3. Then you can edit the cron expression by edit the value of myTimerCron in "Application settings" on azure portal. 3.然后您可以通过在 azure 门户的“应用程序设置”中编辑myTimerCron的值来编辑 cron 表达式。

By the way, when you test the code in local in visual studio, you just need to provide a cron expression with the name of "myTimerCron" in "local.setting.json".顺便说一句,当您在 Visual Studio 中本地测试代码时,您只需要在“local.setting.json”中提供一个名称为“myTimerCron”的 cron 表达式。 在此处输入图像描述

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

相关问题 定时器触发的 function 在 Azure function 应用程序(C#) - Timer-triggered function in Azure function app (C#) Azure Function 和 WebJob - 计时器触发时的奇怪行为 - Azure Function and WebJob - strange behavior when timer-triggered 具有定时器触发的 Azure 功能的令牌缓存 - Token Cache with Timer-Triggered Azure Functions 计时器触发的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? 如何在 Mac 本地运行 Timer-triggered Azure 函数? - How to run Timer-triggered Azure Functions locally on Mac? 在本地运行一次计时器触发的 Azure 函数的最简单方法是什么? - What is the simplest way to run a timer-triggered Azure Function locally once? 计时器触发Azure功能未被触发 - Timer triggered Azure Function not getting triggered 如何在计时器触发的函数上强制执行单例行为? - how to enforce singleton behavior on a timer-triggered function? Azure 计时器触发函数默认计时器 - Azure Timer Triggered Function default timer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM