A newer version of this documentation is available.

View Latest

Function: Simple Timer

      +

      Goal: Create a Simple Timer that triggers in the future on each mutation.

      • This function simpleTimer merely demonstrates a basic Evening Timer.

      • Requires Eventing Storage (or metadata collection) and a "source" collection.

      • 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.

      • simpleTimer

      • Input Data/Mutation

      • Output Data/Logged

      // To run configure the settings for this Function, simpleTimer, as follows:
      //
      // Version 7.1+
      //   "Function Scope"
      //     *.* (or try bulk.data if non-privileged)
      // Version 7.0+
      //   "Listen to Location"
      //     bulk.data.source
      //   "Eventing Storage"
      //     rr100.eventing.metadata
      //   Binding(s) - none
      //
      // Version 6.X
      //   "Source Bucket"
      //     source
      //   "MetaData Bucket"
      //     metadata
      //   Binding(s) - none
      
      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-07-18T19:47:53.842-07:00 [INFO] "From TimerCallback: timer fired with context" {"id":"simpletimer:1","random":0.9470436283584234,"sched":"2021-07-19T02:47:48.405Z","doc":{"id":1,"type":"simpletimer","data":"ABCDEFG"}}
      
      2021-07-18T19:47:18.407-07:00 [INFO] "From OnUpdate createTimer(TimerCallback, Sun Jul 18 2021 19:47:48 GMT-0700 (Pacific Daylight Time),simpletimer:1," {"id":"simpletimer:1","random":0.9470436283584234,"sched":"2021-07-19T02:47:48.405Z","doc":{"id":1,"type":"simpletimer","data":"ABCDEFG"}}