cron-diesel-story Pub

cron with time

NOTE that in cloud some of these fail because of built-in delays. So diesel.isLocalhost

This will kick just once based on time

send::  msg diesel.cron.cancel  (name="cronOnce"="cronOnce")

send::  msg diesel.cron.set  (name="cronOnce"="cronOnce", time=(now() + "..., singleton="yes"="yes", cronMsg="$msg ctx.log()"="$msg ctx...., count:Number=1=1)

Wait for it to kick before continuing - it will also dissapear:

send::  msg ctx.sleep  (duration:Number=2000=2000)

create and cancel

send::  msg diesel.cron.cancel  (name="cron1"="cron1")

This will start in 1 sec and tick every 1 sec for at most 2 occurences (counts)

send::  msg diesel.cron.set  (name="cron1"="cron1", schedule="1s"="1s", time=(now() + "..., singleton="yes"="yes", collect:Number=5=5, cronMsg="$msg ctx.log()"="$msg ctx...., count:Number=2=2)

send::  msg diesel.cron.list 

expect::  ( (sizeOf(payload) is 1))$ifc:: diesel[="isLocalhost"]
expect::  ( ((payload as string) contains "cron1"))

send::  msg diesel.cron.cancel  (name="cron1"="cron1")

send::  msg diesel.cron.list 

expect::  ( (sizeOf(payload) is 0))$ifc:: diesel[="isLocalhost"]
expect:: NOT ( ((payload as string) contains "cron1"))

tick

send::  msg ctx.set  (diesel.realm.props.testcron:Number=1=1)

send::  msg diesel.realm.set  (diesel.cron.await:Boolean=false)

This will start in 1 sec and tick every 1 sec for at most 2 occurences (counts)

send::  msg diesel.cron.set  (name="cron2"="cron2", schedule="1s"="1s", time=(now() + "..., singleton="yes"="yes", collect:Number=5=5, cronMsg ="$msg testdiesel.crontick( )"...="$msg test..., doneMsg ="$msg testdiesel.crondone(name="...="$msg test..., count:Number=2=2, inSequence:Boolean=true)

send::  msg diesel.cron.list 

expect::  ( (sizeOf(payload) is 1))$ifc:: diesel[="isLocalhost"]
expect::  ( ((payload as string) contains "cron2"))

send::  msg ctx.sleep  (duration:Number=4000=4000)

send::  msg ctx.set  (p50=diesel[="r...)

expect::  (p50 is "3cron2")$ifc:: diesel[="isLocalhost"]

Timer is now done:

send::  msg diesel.cron.list 

expect:: NOT ( ((payload as string) contains "cron2"))$ifc:: diesel[="isLocalhost"]

cron expr

See http://www.quartz-scheduler.org/api/2.1.7/org/quartz/CronExpression.html

send::  msg ctx.set  (diesel.realm.props.testcronEX:Number=1=1)

Kick it every second and let it tick twice - uses a cron expression instead of schedule

send::  msg diesel.cron.set  (name="cronQ"="cronQ", cronExpr="* * * * * ?"="* * * * *..., time=(now() + "..., singleton="yes"="yes", collect:Number=5=5, cronMsg ="$msg testdiesel.crontickEX( )"...="$msg test..., count:Number=2=2)

Be careful - expressions like "* */5 * * * ?" would still kick every second, you'd probably want "0 */5 * * * ?"!

send::  msg diesel.cron.list 

expect::  ( (sizeOf(payload) is 1))$ifc:: diesel[="isLocalhost"]
expect::  ( ((payload as string) contains "cronQ"))

send::  msg ctx.sleep  (duration:Number=4000=4000)

send::  msg ctx.set  (p50=diesel[="r...)

expect::  (p50 is 3)

Timer is now done:

send::  msg diesel.cron.list 

expect:: NOT ( ((payload as string) contains "cronQ"))

end time

send::  msg ctx.set  (diesel.realm.props.testcron:Number=1=1)

send::  msg diesel.cron.set  (name="cron4"="cron4", schedule="1s"="1s", endTime=(now() + "..., singleton="yes"="yes", collect:Number=5=5, cronMsg ="$msg testdiesel.crontick( )"...="$msg test...)

send::  msg diesel.cron.list 

expect::  ( (sizeOf(payload) is 1))$ifc:: diesel[="isLocalhost"]
expect::  ( ((payload as string) contains "cron4"))

send::  msg ctx.sleep  (duration:Number=4000=4000)

send::  msg ctx.set  (p50=diesel[="r...)

expect::  (p50 > 2)$ifc:: diesel[="isLocalhost"]
expect::  (p50 < 4)$ifc:: diesel[="isLocalhost"]

Timer is now done:

send::  msg diesel.cron.list 

expect:: NOT ( ((payload as string) contains "cron4"))$ifc:: diesel[="isLocalhost"]

Validate and other calls

send::  msg diesel.cron.validate  (cronExpr="haha?"="haha?")

send::  msg diesel.catch 

expect::  (payload is Exception)

send::  msg diesel.cron.validate  (cronExpr="* * * * * ?"="* * * * *...)

expect::  (payload contains "OK")

Get next time

send::  msg diesel.cron.nextTime  (cronExpr="haha?"="haha?")

send::  msg diesel.catch 

expect::  (payload is Exception)

send::  msg diesel.cron.nextTime  (cronExpr="0 0 * * * ?"="0 0 * * *...)

expect::  (payload is Date)


Was this useful?    

By: Razie | 2021-03-29 .. 2021-03-31 | Tags: story , dsl


Viewed 86 times ( | History | Print ) this page.

You need to log in to post a comment!

© Copyright DieselApps, 2012-2022, all rights reserved.