Skip to content

Hot Reloading (Updating Ship & Equipment Data)

Saya edited this page Feb 7, 2020 · 1 revision

Hot Reloading (Updating Ship & Equipment Data)

Saratoga has the ability to Hot Reload her cache data, hence letting you update your data without any downtime.

Saratoga's hot reloading is made as async as possible to prevent it blocking your Node.js Event loop.

Hot Reloading can be as easy as a cake (For single process Node.js Programs), or a bit complicated (For Sharded/Clusterized Node.js programs).

Easy: Hot Reloading a single process program

In single process programs, or non clustered programs, or non sharded programs, whatever you call it, we don't need to worry about processes accessing the same data, hence we can just do the easiest way to hot reload the data, which is by invoking Saratoga.updater.updateDataAndCache()

const { Saratoga } = require('saratoga');

const client = new Saratoga();

// hot reloads after 10 seconds if there is any updates
setTimeout(() => {
  client.updater.checkForUpdate()
    .then(res => {
      if (res.shipUpdateAvailable || res.equipmentUpdateAvailable) return client.updater.updateDataAndCache();
    });
}, 10000);

Bit Complicated: Hot Reloading a sharded, clustered program, multiple process program.

In multiple cluster, process, or shard or whatever you call it program, there are additional considerations you need to consider. First is you cannot just use Saratoga.updater.updateDataAndCache(), as it may interfere with other process, causing problems. In this case, you need to let a single process from your program to update the local data first, which is done through Saratoga.updater.updateLocalData() then after that, invoke Saratoga.updater.updateCache() from all your cluster, process or shards, including the one you used the updateLocalData() on to update their caches.

Note 1: Saratoga.updater.updateLocalData() will only update the locally saved data.

Note 2: Saratoga.updater.updateCache() will only update the cache based from the locally saved data.

Sounds complicated? probably my explanation made it, but here is an example;

  • Main Process
const { Saratoga } = require('saratoga');

const client = new Saratoga();

// update local data 60s after the boot of main process
setTimeout(() => {
  client.updater.checkForUpdate()
    .then(res => {
      if (res.shipUpdateAvailable || res.equipmentUpdateAvailable) return client.updater.updateLocalData();
    });
}, 60000);

// update caches 30s after the local data update
setTimeout(() => client.updater.updateCache(), 90000);
  • Child Process
const { Saratoga } = require('saratoga');

const client = new Saratoga();

// since we already know master process updated the local data 60s after it booted up, 
// we can just update our child data cache after the master process does the local update,
// to sync caches across process
setTimeout(() => client.updater.updateCache(), 90000);
  • Notes you just need to remember in here:
  1. Update Local Data from a single process via the updateLocalData() method
  2. After Local Data Update is done from single process, run the updateCache() method on all shards, cluster or process