diff --git a/src/queue.js b/src/queue.js new file mode 100644 index 0000000..8dd7d32 --- /dev/null +++ b/src/queue.js @@ -0,0 +1,41 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.RESULTS_PATH = void 0; +var asyncLib = require("async"); +var fs = require("fs"); +var net = require("net"); +// will just write to wherever the process is running, but the server needs to be launched from the same directory so we use an abs path +exports.RESULTS_PATH = "".concat(__dirname, "/results.json"); +function launchQueueServer() { + // Create a write to result.json queue with a concurrency of 1 + // Possibly the simplest fix would be to run this as a separate process, then we can enforce messages sent to this queue are processed in order + var queue = asyncLib.queue(function (task, callback) { + console.log('received task', task.analysisKey); + // return new Promise((resolve, reject) => { + var results = task.results, analysisKey = task.analysisKey; + try { + fs.writeFileSync(exports.RESULTS_PATH, JSON.stringify(results, null, 2)); + console.log("Analysis \"".concat(analysisKey, "\" has been written to ").concat(exports.RESULTS_PATH)); + } + catch (err) { + console.error('Error writing to results.json', err); + } + // }); + }, 1); + queue.drain(function () { + console.log('all items have been processed'); + }); + // this event listener receives tasks from the parallel processes + var server = net.createServer(function (socket) { + socket.on('data', function (data) { + var task = JSON.parse(data.toString()); + queue.push(task); + }); + }); + console.log('Queue server listening on port 8000'); + server.listen(8000); +} +// for use with zst_decompresser.js +if (require.main === module) { + launchQueueServer(); +}