Azure Functions 計時器觸發程序Azure Functions timer trigger

這篇文章說明如何在 Azure Functions 中設定及撰寫計時器觸發程序。This article explains how to configure and code timer triggers in Azure Functions. Azure Functions 具有計時器觸發程序繫結,可讓您根據所定義的排程執行函式程式碼。Azure Functions has a timer trigger binding that lets you run your function code based on a defined schedule.

計時器觸發程序支援多個執行個體向外延展。特定計時器函式的單一執行個體會對所有執行個體執行。The timer trigger supports multi-instance scale-out. A single instance of a particular timer function is run across all instances.

這是 Azure Functions 開發人員的參考資訊。This is reference information for Azure Functions developers. 如果您不熟悉 Azure Functions,請從下列資源開始︰If you're new to Azure Functions, start with the following resources:

計時器觸發程序Timer trigger

函式的計時器觸發程序會使用 function.json bindings 陣列中的下列 JSON 物件︰The timer trigger to a function uses the following JSON object in the bindings array of function.json:

{
    "schedule": "<CRON expression - see below>",
    "name": "<Name of trigger parameter in function signature>",
    "type": "timerTrigger",
    "direction": "in"
}

schedule 的值是包含以下 6 個欄位的 CRON 運算式The value of schedule is a CRON expression that includes these six fields:

{second} {minute} {hour} {day} {month} {day-of-week}

 

注意

您會在線上找到的許多 cron 運算式會省略 {second} 欄位。Many of the cron expressions you find online omit the {second} field. 如果您從中複製一個,您需要對 {second} 欄位進行額外調整。If you copy from one of them, you need to adjust for the extra {second} field. 如需特定範例,請參閱以下的排程範例For specific examples, see Schedule examples below.

CRON 運算式使用的預設時區是國際標準時間 (UTC)。The default time zone used with the CRON expressions is Coordinated Universal Time (UTC). 若要讓 CRON 運算式以另一個時區為基礎,請為名為 WEBSITE_TIME_ZONE 的函式應用程式建立新的應用程式設定。To have your CRON expression based on another time zone, create a new app setting for your function app named WEBSITE_TIME_ZONE. 將值設定為所需的時區名稱,如 Microsoft 時區索引中所示。Set the value to the name of the desired time zone as shown in the Microsoft Time Zone Index.

例如,美加東部標準時間是 UTC-05:00。For example, Eastern Standard Time is UTC-05:00. 若要讓計時器觸發程序在每天上午 10:00 (美加東部標準時間) 觸發,您可以使用說明 UTC 時區的下列 CRON 運算式︰To have your timer trigger fire at 10:00 AM EST every day, use the following CRON expression that accounts for UTC time zone:

"schedule": "0 0 15 * * *",

或者,您可以為名為 WEBSITE_TIME_ZONE 的函式應用程式新增新的應用程式設定,並將值設為美加東部標準時間Alternatively, you could add a new app setting for your function app named WEBSITE_TIME_ZONE and set the value to Eastern Standard Time. 那麼,下列 CRON 運算式即可用於 EST 上午 10:00:Then the following CRON expression could be used for 10:00 AM EST:

"schedule": "0 0 10 * * *",

排程範例Schedule examples

以下是您可以用於 schedule 屬性的 CRON 運算式的一些範例。Here are some samples of CRON expressions you can use for the schedule property.

若要每隔 5 分鐘觸發一次︰To trigger once every five minutes:

"schedule": "0 */5 * * * *"

若要在每小時開始時觸發一次︰To trigger once at the top of every hour:

"schedule": "0 0 * * * *",

若要每隔 2 小時觸發一次:To trigger once every two hours:

"schedule": "0 0 */2 * * *",

若要在上午 9 點到下午 5 點之間每隔一小時觸發一次:To trigger once every hour from 9 AM to 5 PM:

"schedule": "0 0 9-17 * * *",

若要在每天上午 9:30 觸發一次:To trigger At 9:30 AM every day:

"schedule": "0 30 9 * * *",

若要在每個工作天上午 9:30 觸發一次:To trigger At 9:30 AM every weekday:

"schedule": "0 30 9 * * 1-5",

觸發程序使用方式Trigger usage

叫用計時器觸發程序函式時,計時器物件會傳遞至函式。When a timer trigger function is invoked, the timer object is passed into the function. 下列 JSON 是計時器物件的範例表示法。The following JSON is an example representation of the timer object.

{
    "Schedule":{
    },
    "ScheduleStatus": {
        "Last":"2016-10-04T10:15:00.012699+00:00",
        "Next":"2016-10-04T10:20:00+00:00"
    },
    "IsPastDue":false
}

觸發程序範例Trigger sample

假設您的 function.json bindings 陣列中有下列計時器觸發程序︰Suppose you have the following timer trigger in the bindings array of function.json:

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

請參閱可讀取計時器物件,以查看是否晚執行的特定語言範例。See the language-specific sample that reads the timer object to see whether it's running late.

C# 中的觸發程序範例Trigger sample in C#

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

F# 中的觸發程序範例Trigger sample in F#

let Run(myTimer: TimerInfo, log: TraceWriter ) =
    if (myTimer.IsPastDue) then
        log.Info("F# function is running late.")
    let now = DateTime.Now.ToLongTimeString()
    log.Info(sprintf "F# function executed at %s!" now)

Node.js 中的觸發程序範例Trigger sample in Node.js

module.exports = function (context, myTimer) {
    var timeStamp = new Date().toISOString();

    if(myTimer.isPastDue)
    {
        context.log('Node.js is running late!');
    }
    context.log('Node.js timer trigger function ran!', timeStamp);   

    context.done();
};

後續步驟Next steps

如需 Azure Functions 之其他繫結和觸發程序的資訊,請參閱 Azure Functions 觸發程序和繫結開發人員參考For information about other bindings and triggers for Azure Functions, see Azure Functions triggers and bindings developer reference.