Global Data Object

Viewed 170

When loading my Data Object from my second file, the object is empty. Is there any way to get the object children on all files?

index.js:

const {ShardingManager} = require('discord.js');
require('dotenv').config();
const chalk = require('chalk');
const storage = require('./Modules/GlobalStorage');

storage.setDataObject('test', 'test_value');

console.log(chalk.yellow(`Shard Manager Data: ${JSON.stringify(storage.listDataObjects())}`));

let shardManager = new ShardingManager('./Shard/index.js', {
    totalShards: 2,
    token: process.env.CLIENT_TOKEN
});
shardManager.spawn();
shardManager.on('shardCreate', (shard) => {
    console.log(chalk.rgb(75,0,130)(`[SHARD] Created Shard (ID: ${shard.id})`));
});

Modules/GlobalStorage.js:

let DataObject = {};
exports.listDataObjects = () => {
   return DataObject;
};
exports.setDataObject = (key, property) => {
   DataObject[key] = property;
   if(DataObject[key]) return true;
};
exports.getDataObject = (key) => {
   return DataObject[key];
};

Shard/index.js:

const {listDataObjects, getDataObject} = require("../Modules/GlobalStorage");

console.log(chalk.yellow(`Shard Data: ${JSON.stringify(listDataObjects())}`));

The reason I tagged discord.js is that this is my discord bot, it is sharded which is (I believe) the issue.

1 Answers

A spawned shard is a new process that doesn't share memory with your master process. They can't access the same globals or any other variables - the only communication between them must be through some third party (like a file on disk or a cache server) or via IPC (process.send etc).

Which approach is most fitting for you depends on why you actually need this global storage. I personally would recommend something like redis or memcached, but you may also consider simply writing an IPC driver yourself to fetch whatever information you're looking for. Look for subprocess.send and process.send for master and worker respectively.

An absolute last resort that I wouldn't recommend is to have your master process write the file to disk when it's modified and have worker processes read from disk every time they need to get some info. If this global object will be highly used (discord bot - is this a global member cache or something?), this is very not advisable as your I/O load will be high and corruption prone. Use one of the solutions suggested above instead.

Related