adonis5-queue
Version:
Adonis JS 5 queue package based backed by Kue and Kue-scheduler
61 lines (60 loc) • 2 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const ace_1 = require("@adonisjs/ace");
const utils_1 = require("../src/utils");
/**
* Launch queue workers to start processing
*
* @version 2.0.0
* @adonis-version 5.0+
*/
class QueueWork extends ace_1.BaseCommand {
constructor(app, kernel) {
super(app, kernel);
this.config = app.container.use('Adonis/Core/Config');
this.queue = app.container.use('Adonis5/Queue');
}
/**
* Execute command
*/
async handle() {
if (!(await this.hasJobs())) {
this.logger.error('No jobs to watch for. Please use `node ace queue:job` to create jobs!');
return;
}
try {
const success = await this.queue.processing();
console.log(success.message);
}
catch (e) {
console.error(e.message);
process.exit(1);
}
this.logger.success('Worker are running...');
// prevent the main process from exiting...
await new Promise(() => () => { });
}
async hasJobs() {
// load from defined job path
const consumerPath = this.application.makePath(this.config.get('queue.consumerPath'));
const producerPath = this.application.makePath(this.config.get('queue.producerPath'));
if (!(0, utils_1.dirExistsSync)(consumerPath) || !(0, utils_1.dirExistsSync)(producerPath)) {
return false;
}
let consumerFiles = await (0, utils_1.listFiles)(consumerPath);
if (consumerFiles && consumerFiles.length) {
return true;
}
const producerFiles = await (0, utils_1.listFiles)(producerPath);
if (producerFiles && producerFiles.length) {
return true;
}
return true;
}
}
QueueWork.commandName = 'queue:work';
QueueWork.description = 'Start one or more workers';
QueueWork.settings = {
loadApp: true,
};
exports.default = QueueWork;