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