Created
July 12, 2021 03:56
-
-
Save bipinstha7/3e39d4028a0975314129907fff185d71 to your computer and use it in GitHub Desktop.
Bree test setup for maintainer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Bree = require('bree'); | |
const { Worker } = require('worker_threads'); | |
class App { | |
constructor() { | |
this.app = express(); | |
this.routes(); | |
this.scheduler(); | |
} | |
routes() { | |
let router = new Routers(); | |
this.app.get('/health-check', (req, res, next) => { | |
res.status(200).json({ | |
App: 'Health Check', | |
message: 'Okay' | |
}); | |
}); | |
} | |
scheduler() { | |
// breejs configuration. | |
const bree = new Bree({ | |
workerMessageHandler: data => { | |
console.log({ data }); | |
}, | |
jobs: [ | |
{ | |
name: 'long_task' | |
hasSeconds: true, | |
cron: '35 06 11 * * *', | |
// outputWorkerMetadata: true | |
// worker: { | |
// workerData: { | |
// foo: 'bar', | |
// beep: 'boop' | |
// } | |
// } | |
} | |
] | |
}); | |
bree.start(); | |
bree.on('worker created', name => { | |
console.log('worker created', name); | |
// console.log(bree.workers[name]); | |
}); | |
bree.on('worker deleted', name => { | |
console.log('worker deleted', name); | |
console.log(typeof bree.workers[name] === 'undefined'); | |
}); | |
} | |
} | |
new App(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// jobs/long_task.js | |
const { parentPort } = require('worker_threads'); | |
let isCancelled = false; | |
if (parentPort) { | |
parentPort.once('message', message => { | |
if (message === 'cancel') isCancelled = true; | |
}); | |
} | |
(async () => { | |
try { | |
console.log('STARTING') | |
let a = 0; | |
for (let i = 0; i < 1e99; i++) { | |
if (parentPort) { | |
parentPort.postMessage(i); | |
} | |
a += i; | |
} | |
console.log({ a }); | |
} catch (error) { | |
console.log({ jobspickererror: error }); | |
} | |
if (parentPort) parentPort.postMessage('done'); | |
else process.exit(0); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment