cron-diesel-story Pub

Cron testing

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

This will kick just once based on time

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

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

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

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

create and cancel

send::  msg diesel.cron.cancel  (name="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", schedule="1s", time, singleton="yes", collect:Number=5, cronMsg="$msg ctx.log()", count:Number=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")

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)

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

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

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

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)

send::  msg ctx.set  (p50)

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)

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

send::  msg diesel.cron.set  (name="cronQ", cronExpr="* * * * * ?", singleton="yes", collect:Number=5, cronMsg ="$msg testdiesel.crontickEX( )"..., count:Number=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)

send::  msg ctx.set  (p50)

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)

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

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)

send::  msg ctx.set  (p50)

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?")

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?")

send::  msg diesel.catch 

expect::  (payload is Exception)

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

expect::  (payload is Date)


Was this useful?    

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


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

You need to log in to post a comment!

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