每n分钟调用一次Azure Continuous Webjob中的函数

时间:2019-08-20 12:08:32

标签: c# azure-webjobs-continuous

我有一个连续的Azure WebJob中的函数,该函数要求每15分钟调用一次。

Program.cs中的代码

    static void Main()
    {
        var config = new JobHostConfiguration();

        if (config.IsDevelopment)
        {
            config.UseDevelopmentSettings();
        }

        var host = new JobHost(config);
        host.Call(typeof(Functions).GetMethod("StartJob"));
        host.RunAndBlock();
    }

Function.cs中的代码

    [NoAutomaticTrigger]
    public static void StartJob()
    {
        checkAgain:
        if (DateTime.Now.Minute % 15 == 0 && DateTime.Now.Second == 0)
        {
            Console.WriteLine("Execution Started on : " + DateTime.Now);
            //Execute some tasks
            goto checkAgain;
        }
        else
        {
            goto checkAgain;
        }
    }

我的方法正确吗? 由于这是一个无限循环,因此此代码块是否会对托管此Webjob的AppService引起任何类型的性能问题??

1 个答案:

答案 0 :(得分:0)

有针对网络作业的计时器触发器: function.json

{
    "schedule": "0 */5 * * * *",
    "name": "myTimer",
    "type": "timerTrigger",
    "direction": "in"
}

c#

public static void Run(TimerInfo myTimer, ILogger log)
{
    if (myTimer.IsPastDue)
    {
        log.LogInformation("Timer is running late!");
    }
    log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}" );  
}

https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-timer