Alternative to node-cron for modifying ./.data/db.json (lowdb)

I’m using lowdb and want to modify the object at ./.data/db.json …
Every minute I want to delete the 0-index(1st value pair) in the object …
Assuming I could Boost the app and use node-cron package to schedule the db.json modify every minute …
Ideally I would rather understand how to setup a cron without needing node-cron package …
This would be coding part of node-cron package myself to run this db.json modify task every minute …
Any guidance for is appreciated…

https://glitch.com/edit/#!/glen-titanium-dilophosaurus

My impression of how a system like cron or node-cron works is that it roughly does this:

  1. Read the schedule about when to do what
  2. Get the current time
  3. Figure out when the next time it’ll have to run something
  4. Wait for a specific amount of time that’ll bring us to the time determined in step 3
  5. Run the task determined in step 3
  6. Repeat from step 2

Some things that might help:

  • For step 2, new Date() gets you an object that lets you look up what the current year/month/date/day of week/hour/minute/second it is now.
  • For step 4, look up setTimeout, which lets you have the engine call a function a given number of milliseconds in the future. You can also look up setInterval which can save a few steps if you would otherwise use setTimeout repeatedly for multiples of the same time.

Then you have the logic stuff in steps 1 and 3, which would be the fun part of coding your own alternative. I haven’t written one before either, so your guess is a good as mine. But let us know if you get stuck.


Tangentially related: since you’ll be modifying files, make sure you have some coordination throughout your app so that parts which need to read the file won’t go looking while you’re in the middle of updating it.

1 Like

And… be aware that there are two reboots of the app server, per day apparently. So it needs to recover from that and adjust something if it missed a cycle or two.

2 Likes

right so the server that’s BoostedTM would stay up and this would mitigate reboot adjust? No eventually the server will restart (is that scheduled for Boosted apps?)
How do I check the reset schedule or to track when it is reset?

According to the docs the servers are reset twice a day. I asked about it in another message for more details but didn’t get a reply that I noticed. I assume it is scheduled but I don’t know how we can determine the schedule.

I think it would be handy if our apps could subscribe to a service that would signal that the reboot is about to occur but I don’t believe that is going to happen. I also see there are multiple servers hosting our boosted apps so I don’t know how that affects things.

Also it seems possible that a cron job wouldn’t necessarily be the only way to obtain whatever output you’re trying to obtain.

You don’t have to explain it all if you’d rather not but it seems like there should be other ways to do it.

1 Like

Whoa cool Hexagon Hexagon (Hexagon) · GitHub responded with idea…

I would recommend https://github.com/hexagon/croner, which has a similar interface to this module, but without any external dependencies (specifically moment-timezone, which require moment, which is quite bulky).

However, THE most compact way of running a task every minute is with vanilla JavaScript, is by using a recursive implementation of setTimeout, with a timeout of 60000ms.

let timer;
let recurse = () => {
   // do stuff, watch out for errors
   try {
      console.log('Yay!');
   } catch (e) {
      console.log('Whoops');
   }

   // Run again after 60 seconds
   timer = setTimeout(()=>{recurse();}, 60*1000);
};

// If you want to abort execution
// clearTimeout(timer);

// Start execution
recurse();

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.