带有计时器触发器的连续 WebJob
Continuous WebJob with timer trigger
我在连续的 web 作业中编写了以下功能:
public static void fun1([TimerTrigger("24:00:00", RunOnStartup = true, UseMonitor = true)] TimerInfo timerInfo, TextWriter log)
{//Code}
public static void fun2([TimerTrigger("00:01:00", RunOnStartup = true, UseMonitor = true)] TimerInfo timerInfo, TextWriter log)
{//code}
其中,fun1 不再被调用(只有一次,在启动网络作业后)
在每个进程完成后,fun2 被调用并触发 1 分钟。
谁能解释一下为什么?
我做错了什么吗?
你应该看看 TimerTriggerAttribute
:
的文档
- 您指定的第一个参数是调度表达式:
这可以是 6 字段 crontab 表达式或 System.TimeSpan.
Cron 表达式可以这样表示:
* * * * * * command to be executed
┬ ┬ ┬ ┬ ┬ ┬
│ │ │ │ │ │
│ │ │ │ │ │
│ │ │ │ │ └───── day of week (0 - 7) (0 or 7 are Sunday, or use names)
│ │ │ │ └────────── month (1 - 12)
│ │ │ └─────────────── day of month (1 - 31)
│ | └──────────────────── hour (0 - 23)
│ └───────────────────────── min (0 - 59)
└────────────────────────────── second(0 - 59)
在您的例子中,表达式是表示 TimeSpan 的字符串:
"24:00:00"
:此作业每 24 小时 运行ning,RunOnStartup
:这意味着作业将 运行 当 webjob 启动时或即使最后 运行 发生在过去 24 小时内,也会重新启动。
"00:01:00"
:此作业每分钟 运行ning, RunOnStartup
:这意味着作业将 运行 当 webjob 启动或重新启动时即使最后 运行 发生在最后一分钟。
编辑
来自这个回答:
This is due to the way TimeSpan.Parse works. If you pass it "24:00:00" strangely enough it will give you back a TimeSpan of duration 24 days. Not sure if this is their intended behavior or a bug on their side, but we simply pass the expression down to them and inherit their behavior.
Anyhow, for your purposes, to get 24 hours you can use "1.00:00" (specifying 1 day).
我在连续的 web 作业中编写了以下功能:
public static void fun1([TimerTrigger("24:00:00", RunOnStartup = true, UseMonitor = true)] TimerInfo timerInfo, TextWriter log)
{//Code}
public static void fun2([TimerTrigger("00:01:00", RunOnStartup = true, UseMonitor = true)] TimerInfo timerInfo, TextWriter log)
{//code}
其中,fun1 不再被调用(只有一次,在启动网络作业后) 在每个进程完成后,fun2 被调用并触发 1 分钟。
谁能解释一下为什么? 我做错了什么吗?
你应该看看 TimerTriggerAttribute
:
- 您指定的第一个参数是调度表达式: 这可以是 6 字段 crontab 表达式或 System.TimeSpan.
Cron 表达式可以这样表示:
* * * * * * command to be executed
┬ ┬ ┬ ┬ ┬ ┬
│ │ │ │ │ │
│ │ │ │ │ │
│ │ │ │ │ └───── day of week (0 - 7) (0 or 7 are Sunday, or use names)
│ │ │ │ └────────── month (1 - 12)
│ │ │ └─────────────── day of month (1 - 31)
│ | └──────────────────── hour (0 - 23)
│ └───────────────────────── min (0 - 59)
└────────────────────────────── second(0 - 59)
在您的例子中,表达式是表示 TimeSpan 的字符串:
"24:00:00"
:此作业每 24 小时 运行ning,RunOnStartup
:这意味着作业将 运行 当 webjob 启动时或即使最后 运行 发生在过去 24 小时内,也会重新启动。"00:01:00"
:此作业每分钟 运行ning,RunOnStartup
:这意味着作业将 运行 当 webjob 启动或重新启动时即使最后 运行 发生在最后一分钟。
编辑
来自这个回答:
This is due to the way TimeSpan.Parse works. If you pass it "24:00:00" strangely enough it will give you back a TimeSpan of duration 24 days. Not sure if this is their intended behavior or a bug on their side, but we simply pass the expression down to them and inherit their behavior. Anyhow, for your purposes, to get 24 hours you can use "1.00:00" (specifying 1 day).