For some reason, this setup isn't working. All I'm trying to do is run the processing of the job not on the main thread, because it's blocking my event loop. Procfile looks like:
web: node app.js
worker: node worker.js
package.json:
{
"name": "server-testing",
"version": "1.0.0",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "npm start"
},
"author": "",
"license": "ISC",
"description": "",
"dependencies": {
"bull": "^3.22.6",
"express": "^4.17.1",
"throng": "^5.0.0"
},
"engines": {
"node": "14.17.6"
}
}
app.js:
const express = require('express');
const app = express();
const http = require('http').Server(app);
const Queue = require('bull');
const workQueue = new Queue('work', process.env.REDIS_URL)
app.post('/addJob', async function(request, response) {
let job = await workQueue.add({request: request.body});
console.log("Logging job as " + job.id)
response.json({ id: job.id });
});
worker.js:
let throng = require('throng');
let Queue = require("bull");
let REDIS_URL = process.env.REDIS_URL
let workers = process.env.WEB_CONCURRENCY || 1;
let maxJobsPerWorker = 3;
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
function start() {
console.log("Started processing")
let workQueue = new Queue('work', REDIS_URL);
workQueue.process(maxJobsPerWorker, async (job) => {
let progress = 0;
if (Math.random() < 0.05) {
throw new Error("This job failed!")
}
while (progress < 100) {
await sleep(50);
progress += 1;
job.progress(progress)
}
return { value: "This will be stored" };
console.log("Finished processing")
});
}
throng({ workers, start });
Output of 'Heroku ps' command:
=== web (Free): node app.js (1)
web.1: up 2021/10/01 20:57:34 -0400 (~ 11m ago)
=== worker (Free): node worker.js (1)
worker.1: up 2021/10/01 20:57:31 -0400 (~ 11m ago)
Essentially, the console.log() statements inside the process function are never printing, indicating the jobs are never being processed. Not sure what I'm missing here...