简体   繁体   English

可配置的计时器触发器 - Azure Web作业

[英]Configurable Timer Triggers - Azure Web Jobs

I'm building a job to trigger at some regular interval (say 1 minute). 我正在建立一个工作,以定期间隔(比如1分钟)触发。 I've successfully used triggered web jobs with time-span hard coded in the functions. 我已成功使用触发式Web作业,并在函数中使用时间跨度硬编码。

public void foo([TimerTrigger("00:01:00")] TimerInfo timer)

Now if I ever want to change the trigger time from 1-min to 2-min I've to redeploy the code. 现在,如果我想将触发时间从1分钟更改为2分钟,我将重新部署代码。 Instead is there a way to make the TimeTrigger Configurable, from a config file. 相反,有没有办法从配置文件中使TimeTrigger可配置。

Note that replacing the string with a dynamically read value isn't possible as the TimerTrigger Attribute is a const string expression or a Type. 请注意,无法使用动态读取值替换字符串,因为TimerTrigger属性是常量字符串表达式或类型。

After much digging, I've realized that this can be done with an SDK extension class TimerSchedule . 经过深入挖掘,我意识到这可以通过SDK扩展类TimerSchedule来完成。

For it, you would need a base class that you'll be able to use for multiple triggers. 对于它,您需要一个可以用于多个触发器的基类。

class CustomTimerTriggerBase: TimerSchedule
{
    TimeSpan timer;
    public CustomTimerTriggerBase(string triggerConfigKey)
    {
        timer=TimeSpan.Parse(ConfigurationManager.AppSettings[triggerConfigKey]);
    }

    public override DateTime GetNextOccurrence(DateTime now)
    {
        return now.Add(timer);
    }
}

Use this Base to generate your timers... 使用此Base生成您的计时器......

public sealed class FooTimer : CustomTimerTriggerBase
{
    public FooTimer() : base("FooTimerKey") {}
}

In your, App.config have a key for "FooTimer" 在您的App.config中有一个“FooTimer”键

<add key="FooTimerKey" value="00:02:00" />

Use this FooTimer class in your webjob functions. 在webjob函数中使用此FooTimer类。

public void foo([TimerTrigger(typeof(FooTimer)] TimerInfo timer)

Now you can simply change the value in app config instead of redeploying the code. 现在,您只需更改app config中的值,而不是重新部署代码。 NOTE : Since you are using Timespan to parse, the string can be of any format you need as defined in TimeSpan formats. 注意 :由于您使用Timespan进行解析,因此字符串可以是TimeSpan格式中定义的任何格式。


UPDATE UPDATE

As pointed by l--''''''---------'''''''''''' and Andy Dobedoe now (as of 2019) it is much simpler to achieve this. 正如现在指出的那样 - 安德·多布多 (截至2019年),实现这一目标要简单得多。

public static async Task RunAsync([TimerTrigger("%MYCRON%")]TimerInfo myTimer

Finds the setting called MYCRON and uses the cron expression from there 找到名为MYCRON的设置并从那里使用cron表达式

It turns out, this is pretty easy nowadays. 事实证明,现在这很容易。 Just put the app setting in as your cron schedule expression and it will look it up for you. 只需将应用程序设置作为您的cron计划表达式,它就会为您查找。

eg 例如

public static async Task RunAsync([TimerTrigger("%MYCRON%")]TimerInfo myTimer

Finds the setting called MYCRON and uses the cron expression from there 找到名为MYCRON的设置并从那里使用cron表达式

You can do this like so: 你可以这样做:

public static void Run([TimerTrigger("%MYSCHEDULE%")] TimerInfo myTimer, ILogger log)

where MYSCHEDULE is an environment variable which you can store in your local.settings.json file as well as in your application settings in the portal. 其中MYSCHEDULE是一个环境变量,您可以将其存储在local.settings.json文件中以及门户中的应用程序设置中。

An example value for MYSCHEDULE would be: MYSCHEDULE的示例值是:

"MYSCHEDULE": "0 */2 * * * *"

AFAIK, you need to specific the scheduleExpression parameter for TimerTrigger in your code or implement your WeeklySchedule or DailySchedule described in this sample TimerSamples.cs . 据我所知,你需要特定的scheduleExpression的参数TimerTrigger在你的代码或者实现你WeeklyScheduleDailySchedule在此示例中描述TimerSamples.cs For changing the schedule without re-deploy your code, I assume that you could leverage Azure Scheduler to trigger your webjob on some schedule and you could change the schedule settings as you expected without re-deploy your webjob. 为了在不重新部署代码的情况下更改计划,我假设您可以利用Azure计划程序在某些计划中触发您的webjob,并且可以按预期更改计划设置,而无需重新部署您的webjob。 For more details, you could refer to the section about adding a scheduler job in this tutorial . 有关更多详细信息,请参阅本教程中有关添加调度程序作业的部分。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM