adonis5-queue
Version:
Adonis JS 5 queue package based backed by Kue and Kue-scheduler
81 lines (80 loc) • 3.4 kB
JavaScript
"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const path_1 = __importDefault(require("path"));
const change_case_1 = require("change-case");
const ace_1 = require("@adonisjs/ace");
/**
* Generate producer/consumer pair for new jobs
*
* @version 1.0.0
* @adonis-version 5.0+
*/
class QueueJob extends ace_1.BaseCommand {
constructor(app, kernel) {
super(app, kernel);
this.config = app.container.use('Adonis/Core/Config');
}
/**
* Execute command
*/
async handle() {
const jobId = this.jobId ? (0, change_case_1.paramCase)(this.jobId) : (0, change_case_1.paramCase)(this.jobName);
const jobName = (0, change_case_1.pascalCase)(this.jobName);
try {
// parse respective templates
const producerTmpl = path_1.default.join(__dirname, '../templates/producer.txt');
const consumerTmpl = path_1.default.join(__dirname, '../templates/consumer.txt');
// save into selected directory
const producerPath = this.config.get('queue.producerPath');
const consumerPath = this.config.get('queue.consumerPath');
this.generator
.addFile(this.jobName, { pattern: 'pascalcase', form: 'singular' })
.apply({
jobName,
jobId,
})
.stub(producerTmpl)
.destinationDir(producerPath)
.useMustache()
.appRoot(this.application.cliCwd || this.application.appRoot);
await this.generator.run();
this.generator.clear();
this.generator
.addFile(this.jobName, { pattern: 'pascalcase', form: 'singular' })
.apply({
jobName,
jobId,
})
.stub(consumerTmpl)
.destinationDir(consumerPath)
.useMustache()
.appRoot(this.application.cliCwd || this.application.appRoot);
await this.generator.run();
}
catch (e) {
console.error(e);
this.logger.error('Failed to generate job classes with error ' + e.message);
}
}
}
QueueJob.commandName = 'queue:job';
QueueJob.description = 'Generate producer/consumer pair for new jobs';
QueueJob.settings = {
loadApp: true,
};
exports.default = QueueJob;
__decorate([
ace_1.args.string({ description: 'Name of job to process', required: true })
], QueueJob.prototype, "jobName", void 0);
__decorate([
ace_1.flags.string({ description: 'Run seeders in interactive mode', alias: 'jobId' })
], QueueJob.prototype, "jobId", void 0);