import * as appservice from "@pulumi/azure/appservice" ; import { timer } from "@pulumi/cloud" ; import * as pulumi from "@pulumi/pulumi" ; import { RunError } from "@pulumi/pulumi/errors" ; import * as shared from "./shared" ; const secondsInOneMinute = 60 ; const secondsInOneHour = secondsInOneMinute * 60 ; const minutesInOneHour = 60 ; const minutesInOneDay = minutesInOneHour * 24 ; const minutesIn24Days = 24 * minutesInOneDay ; export function interval ( name , options , handler , opts ? ) { let minutes = 0 ; if ( options . minutes ) { minutes += options . minutes ; } if ( options . hours ) { minutes += options . hours * minutesInOneHour ; } if ( options . days ) { minutes += options . days * minutesInOneDay ; } if ( minutes < 1 ) { throw new Error ( "Interval must be at least 1 minute" ) ; } let timeSpan ; if ( minutes >= minutesIn24Days ) { timeSpan = ConvertMinutesToDD_HH_MM ( minutes ) ; } else if ( minutes < minutesInOneDay ) { timeSpan = ConvertSecondsToHH_MM_SS ( minutes * secondsInOneMinute ) ; } else { throw new Error ( "Azure only supports intervals less than 24 hours, or intervals greater than 24 days." ) ; } const t = new Timer ( name , timeSpan , true , handler , opts ) ; } function ConvertMinutesToDD_HH_MM ( minutes ) { minutes = Math . floor ( minutes ) ; const days = Math . floor ( minutes / minutesInOneDay ) ; minutes = minutes % minutesInOneDay ; const hours = Math . floor ( minutes / minutesInOneHour ) ; minutes = minutes % minutesInOneHour ; return `${ pad ( days ) }:${ pad ( hours ) }:${ pad ( minutes ) }` ; } function ConvertSecondsToHH_MM_SS ( seconds ) { seconds = Math . floor ( seconds ) ; const hours = Math . floor ( seconds / secondsInOneHour ) ; seconds = seconds % secondsInOneHour ; const minutes = Math . floor ( seconds / secondsInOneMinute ) ; seconds = seconds % secondsInOneMinute ; return `${ pad ( hours ) }:${ pad ( minutes ) }:${ pad ( seconds ) }` ; } function pad ( val ) { if ( val === undefined ) { return "00" ; } if ( val < 10 ) { return "0" + val ; } return val . toString ( ) ; } export function daily ( name , scheduleOrHandler , handlerOrOptions ? , opts ? ) { let hour ; let minute ; let handler ; if ( typeof scheduleOrHandler === "function" ) { hour = 0 ; minute = 0 ; handler = scheduleOrHandler as timer . Action ; opts = handlerOrOptions as pulumi . ResourceOptions | undefined ; } else if ( ! scheduleOrHandler ) { throw new RunError ( "Missing required timer handler function" ) ; } else { hour = scheduleOrHandler . hourUTC || 0 ; minute = scheduleOrHandler . minuteUTC || 0 ; handler = handlerOrOptions as timer . Action ; } cron ( name , `${ minute } ${ hour } * * ? *` , handler , opts ) ; } export function hourly ( name , scheduleOrHandler , handlerOrOptions ? , opts ? ) { let minute ; let handler ; if ( typeof scheduleOrHandler === "function" ) { minute = 0 ; handler = scheduleOrHandler as timer . Action ; opts = handlerOrOptions as pulumi . ResourceOptions | undefined ; } else if ( ! scheduleOrHandler ) { throw new RunError ( "Missing required timer handler function" ) ; } else { minute = scheduleOrHandler . minuteUTC || 0 ; handler = handlerOrOptions as timer . Action ; } cron ( name , `${ minute } * * * ? *` , handler , opts ) ; } export function cron ( name , cronTab , handler , opts ? ) { const t = new Timer ( name , cronTab , false , handler , opts ) ; } class Timer extends pulumi . ComponentResource { public readonly subscription : appservice . TimerSubscription ; constructor ( name , scheduleExpression , isTimeSpan , handler , opts ? ) { super ( "cloud:timer:Timer" , name , { } , opts ) ; let appServicePlan = shared . getGlobalFunctionAppServicePlan ( ) ; let siteConfig ; if ( isTimeSpan ) { appServicePlan = shared . getGlobalAppServicePlan ( ) ; siteConfig = { alwaysOn : true , } ; } this . subscription = new appservice . TimerSubscription ( name , { ... shared . defaultSubscriptionArgs , account : shared . getGlobalStorageAccount ( ) , schedule : scheduleExpression , plan : appServicePlan , siteConfig : siteConfig , callback : context => { handler ( ) . then ( ( ) => context . done ( ) ) ; } , } , { parent : this } ) ; this . registerOutputs ( { subscription : this . subscription } ) ; } }