【问题标题】:Azure Function Cron expression for excluding a defined time用于排除定义时间的 Azure Function Cron 表达式
【发布时间】:2020-12-02 11:31:26
【问题描述】:

我想在 Azure Function 上做非常基本的 cron 工作。 我在 Visual Studio 上编写函数,这些函数将被 dockerized 工作。 我的解决方案有两种不同的天蓝色功能。

  1. 每月运行一次。 (刷新整个表格)。
  2. 每五分钟运行一次。 (仅刷新特征记录。不是历史记录。)

我的期望是函数不应该互相阻塞。因此,它们不应该同时工作。 我的函数的 cron 表达式是;

  • Function1:0 0 0 1 * *(每月 1 号运行。)
  • Function2:0 */5 * * * *(每五分钟运行一次)

我不想像 01/01/2021 00:00:00 这样的每个月都运行 function2。 如何从 function2 中排除时间?

【问题讨论】:

    标签: azure cron azure-functions


    【解决方案1】:

    没有直接的方法可以做到这一点。

    作为一种解决方法,您可以在 Function2 中添加 if else 代码块。比如if block,可以判断是不是像01/01/2021 00:00:00这样的时间。如果是,那么什么也不做。如果没有,那就去else block执行你的逻辑。

    如下:

    public static void Run([TimerTrigger("0 */5 * * * *")]TimerInfo myTimer, ILogger log)
    {
        var the_time = DateTime.Now;
    
        //if the date is the first day of a month, and time is 00:00:00
        if(the_time.Day==1 && the_time.ToLongTimeString().Contains("00:00:00"))
        {
          //do nothing, don't write any code here.
        }
        else
       {
          //write your code logic here
       }
    
    }
    

    【讨论】:

    • 是的,这是一种处理方式。由于无法直接定义为cron表达式,所以我必须这样做。谢谢。
    猜你喜欢
    • 2012-06-04
    • 1970-01-01
    • 2020-11-17
    • 1970-01-01
    • 1970-01-01
    • 2018-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多