Skip to content

Instantly share code, notes, and snippets.

@Hossman333
Last active July 21, 2020 17:14
Show Gist options
  • Save Hossman333/0df575399ea7032b566ec8471ce70b22 to your computer and use it in GitHub Desktop.
Save Hossman333/0df575399ea7032b566ec8471ce70b22 to your computer and use it in GitHub Desktop.
const { run } = require("graphile-worker");
const { Pool } = require("pg");
const cluster = require("cluster");
async function main() {
const pool = new Pool({
user: "postgres",
password: "postgres",
host: "localhost",
database: "postgres",
port: 5432,
});
const runner = await run({
pgPool: pool,
taskList: {
testing: async (payload, helpers) => {
await new Promise((resolve, reject) =>
setTimeout(() => resolve(), 1000)
);
console.log("Handled job PID: %s %s", process.pid, new Date());
},
},
});
setInterval(() => {
console.log("Adding a job PID: %s %s", process.pid, new Date());
runner.addJob("testing", { foo: "bar" }, { jobKey: "pizza" });
}, 100);
}
if (cluster.isMaster) {
console.log(`Master ${process.pid} is running`);
// Fork workers.
for (let i = 0; i < 24; i++) {
cluster.fork();
}
cluster.on("exit", (worker, code, signal) => {
console.log(`worker ${worker.process.pid} died`);
});
} else {
main().catch((err) => {
console.error(err);
process.exit(1);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment