Goal: Create a Simple Timer that triggers once per mutation.
-
This function simpleTimer merely demonstrates a basic Evening Timer.
-
Requires a metadata bucket and a source bucket.
-
Will operate on any mutation where the KEY or meta.id starts with "simpletimer:".
-
The result of processing the mutation is a Timer that will be executed approximately 30 seconds in the future.
-
For more details on Timers refer to Timers.
-
For an example of a recurring Timer refer to: Recurring Timer.
function TimerCallback(context) {
log('From TimerCallback: timer fired with context', context);
// do any sort of recurring work here, just update a date_stamp in a doc
}
function OnUpdate(doc, meta) {
// You would typically filter to mutations of interest
if (! meta.id.startsWith("simpletimer:")) return;
// Make a Date 30 seconds in the future
var thirtySecFromNow = new Date(); // Get current time & add 30 sec. to it.
thirtySecFromNow.setSeconds(thirtySecFromNow.getSeconds() + 30);
// Timers require an id
var timer_id = meta.id;
// Timers can access a context when they fire
var context = {"id": meta.id, "random": Math.random(), "sched": thirtySecFromNow, doc: doc}
// crate the Timer
createTimer(TimerCallback, thirtySecFromNow, timer_id, context);
log("From OnUpdate createTimer(TimerCallback, "+thirtySecFromNow+","+ timer_id+",", context);
}
INPUT: KEY simpletimer:1
{
"id": 1,
"type": "simpletimer",
"data": "ABCDEFG"
}
2021-04-10T11:00:33.034-07:00 [INFO] "From TimerCallback: timer fired with context"
{"id":"simpletimer:1","random":0.8167888118053206,"sched":"2021-04-10T18:00:26.955Z","doc":{"id":1,"type":"simpletimer","data":"ABCDEFG"}}
2021-04-10T10:59:56.960-07:00 [INFO] "From OnUpdate createTimer(TimerCallback, Sat Apr 10 2021 11:00:26 GMT-0700 (Pacific Daylight Time),
simpletimer:1," {"id":"simpletimer:1","random":0.8167888118053206,"sched":"2021-04-10T18:00:26.955Z","doc":{"id":1,"type":"simpletimer","data":"ABCDEFG"}}