Azure函数正在本地执行,即使RunOnStartup = false

时间:2018-12-19 06:41:58

标签: azure azure-functions azure-functions-runtime azure-functions-core-tools

嗨,据我了解,Azure函数将执行

如果 RunOnStartup = true

   1. on startup 
   2. if a host changed 
   3. a new deployment happen
   4. on schedule time

并且,如果 RunOnStartup = false或未定义

   1. on schedule time only

但是当我使用 RunOnStartup = false 在本地运行时,它也可以在启动时执行,并且在Azure门户上也可以正常运行。谁能提出原因呢?

更新:-  功能代码:-

    public static void Run([TimerTrigger("0 30 3 * * *", RunOnStartup = false)]TimerInfo myTimer, TraceWriter log, ExecutionContext executionContext)
    {
        log.Info($"Function1- Timer trigger function executed at: {DateTime.Now}");
        try
        {
            //main work
        }
        catch (Exception ex)
        {
            log.Error(ex.Message, ex);
        }
        finally
        {
            log.Info($"Function1 - Timer trigger function ENDED at: {DateTime.Now}");
        }
    }

控制台输出:-

   your worker runtime is not set. As of 2.0.1-beta.26 a worker runtime setting is

必填。 请运行func settings add FUNCTIONS_WORKER_RUNTIME <option>或添加功能 S_WORKER_RUNTIME到您的local.settings.json 可用选项:dotnet,node,python

              %%%%%%
             %%%%%%
        @   %%%%%%    @
      @@   %%%%%%      @@
   @@@    %%%%%%%%%%%    @@@
 @@      %%%%%%%%%%        @@
   @@         %%%%       @@
     @@      %%%       @@
       @@    %%      @@
            %%
            %

  Azure Functions Core Tools (2.3.148 Commit hash:     f9b3db04f9833b431f1b001efb3e5783a169ebfc)
  Function Runtime Version: 2.0.12210.0
  [19-Dec-18 8:22:47 AM] Building host: startup suppressed:False, configuration suppressed: False
  [19-Dec-18 8:22:47 AM] Reading host configuration file 'D:\path****\host.json'
  [19-Dec-18 8:22:47 AM] Host configuration file read:
  [19-Dec-18 8:22:47 AM] {
  [19-Dec-18 8:22:47 AM]   "version": "2.0"
  [19-Dec-18 8:22:47 AM] }
  [19-Dec-18 8:22:47 AM] Initializing Host.
  [19-Dec-18 8:22:47 AM] Host initialization: ConsecutiveErrors=0, StartupCount=1
  [19-Dec-18 8:22:47 AM] Starting JobHost
  [19-Dec-18 8:22:47 AM] Starting Host (HostId=boldsombirk-85417686, InstanceId=4f41b83d-022e-4e75-b75b-528890f62058, Version=2.0.12210.0, ProcessId=7012, AppDomainId=1, InDebugMode=False, InDiagnosticMode=False, FunctionsExtensionVersion=)
  [19-Dec-18 8:22:47 AM] Loading functions metadata
  [19-Dec-18 8:22:48 AM] 1 functions loaded
  [19-Dec-18 8:22:48 AM] Generating 1 job function(s)
  [19-Dec-18 8:22:48 AM] Found the following functions:
  [19-Dec-18 8:22:48 AM] Function1.Run
  [19-Dec-18 8:22:48 AM]
  [19-Dec-18 8:22:48 AM] Host initialized (673ms)
  [19-Dec-18 8:22:49 AM] Executing 'Function1' (Reason='Timer fired at 2018-12-19T13:52:49.5718606+05:30', Id=e1603f8f-41d0-492e-8674-d5771813422d)
  [19-Dec-18 8:23:41 AM] Function1 - Timer trigger function executed at: 19-Dec-18 1:53:41 PM

谢谢

2 个答案:

答案 0 :(得分:1)

让我们以一个示例开始,计时器触发器以0 */5 * * * *的时间表12/19/2018 4:01:00 PM开始,我们可以看到时间表打印

12/19/2018 4:05:00 PM
12/19/2018 4:10:00 PM
12/19/2018 4:15:00 PM
12/19/2018 4:20:00 PM
12/19/2018 4:25:00 PM

但是,我们在4:02 PM停止(调试)该项目。然后在4:08 PM再次运行它,我们可以看到计时器触发器在启动时执行。

之所以会这样,是因为ScheduleStatus(如下所示)已在第一次启动触发器时存储在blob存储中。当我们再次运行项目时,计时器触发器将读取现有的Next计划时间并将其与当前时间进行比较。如果预定的Next时刻过去了,计时器触发器将在启动后立即执行。

 // I am in UTC+8:00 time zone
{
    "Last":"0001-01-01T00:00:00",
    "Next":"2018-12-19T16:05:00+08:00",
    "LastUpdated":"2018-12-19T16:02:12.7071566+08:00"
}

按照the sample in doc处理过时的计划的执行。如果当前函数调用晚于计划时间,则IsPastDue属性为true。

[FunctionName("TimerTriggerCSharp")]
public static void Run([TimerTrigger("0 */5 * * * *")]TimerInfo myTimer, ILogger log)
{
    if(myTimer.IsPastDue)
    {
        log.LogInformation("Timer is running late!");
    }
    log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
}

答案 1 :(得分:0)

Jerry的答案是正确的,但是如果您想在使用RunOnStartup测试代码时快速解决问题,则可以将当前的Timer触发器更改为将来的时间。

示例:根据您的情况,将其从0 30 3 * * *更改为0 30 23 * * *

注意:这不是推荐的做法。必须处理过时的日程表。