Crontask feature !

Posted on
  • Hello,
    I wonder if adding a "cron" feature in Espruino or just in Bangle would be a good idea ?

    For my case, i want start a buzz at every hour. I can use setTimeout or setInterval.
    But if many app use setTimeout/Intervale, in internal we check at all loop turn if this timeout is done and if is true, we start the callback.
    It can be memory and cpu intensive.

    If we add a simply cron (just hours and minutes) we have one interval (one minutes) and at all minutes check if many tasks match with this time.

    In memory it would be enough to store the pointer of callback and one string (e.g.: "* 0" all hours at zero minutes).

    What's do you think ?

    Good day,

  • There is actually a module for this already in

    So for normal Espruino you could use that - but yes, it'd be nice if something was built in to Bangle.js - it's been asked before - it's just hard thinking of a 'nice' way to do it.

  • For bangle,
    I have think at two methods in Bangle object, Bangle.createCron and Bangle.removeCron or Bangle.cron.create/remove.

    Create method have two parameters, first is a time string ("* * ...") and second is name of file ("myapp.[CRON_NAME].cron.js").
    This data is writing in file or in config to not lose their after reboot.

    Remove method have same parameters, and we remove data in file/config.

    And regularly (e.g. at all minutes) the cron engine, read cron file (or config file). And check if this now match with time string.
    If it is true we load file script.

    The cron engine can be writing in C or in JS.
    Surely you know which languages it is best for ?

    We need to define what we want to allow as a time value?
    Support just minimal (hours and minutes) or full cron support or another ?

    If cron engine is good, we can use engine for alarm and calendar event.

  • Yes, I think that makes a lot of sense. It does need a little thought though - RAM on Bangle.js 1 is quite scarce, so do we store the whole cron table in RAM, or keep it in flash and load it every minute?

    But once I've figured those things out I think it would make a good addition.

  • Hello,
    I think save crontab in flash and load file at every minutes.

    Good day,

  • As it is a watch, how about having Bangle emit time events?

    Bangle.on('hour', function(time) { 
      // time is a Date object
      // fires exactly once per hour, with minutes/seconds/milliseconds = 0
    Bangle.on('minute', function(time) { 
      // time is a Date object
      // fires exactly once per minute, with seconds/milliseconds = 0
    Bangle.on('second', function(time) { 
      // time is a Date object
      // fires exactly once per second, with milliseconds = 0

    At least for buzzing every hour or redrawing clocks this should be sufficient to get rid of manually setting timeouts/intervals.

    No idea if this would have a serious impact on performance/battery life though (especially for the V1, where clocks tend to clear their timeouts when the LCD is off)

  • I was wondering about that. I think the second handler could actually end up using some power, but minute/hour would effectively be free, and for the new 'health' event we're already doing something similar (just every 10 minutes)

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview

Crontask feature !

Posted by Avatar for VSerain @VSerain