Skip to content

Commit

Permalink
queue.js
Browse files Browse the repository at this point in the history
  • Loading branch information
bennyrubanov committed Mar 24, 2024
1 parent 828d646 commit 56fb2fc
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions src/queue.js
Original file line number Diff line number Diff line change
@@ -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<void>((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();
}

0 comments on commit 56fb2fc

Please sign in to comment.