Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
/**
* @file Illustrate concurrency and locking
*/
import Agenda from 'agenda';
function time() {
return new Date().toTimeString().split(' ')[0];
}
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
const agenda = new Agenda({
db: {
address: 'mongodb://agendan:nuam0agenda@ds052408.mlab.com:52408/agenda',
options: { useNewUrlParser: true },
collection: `agendaJobs-${Math.random()}`, // start fresh every time
},
});
let jobRunCount = 1;
agenda.define('long-running job', {
lockLifetime: 5 * 1000, // max amount of time the job should take
concurrency: 3, // max number of job instances to run at the same time
}, async (job, done) => {
const thisJob = jobRunCount++;
console.log(`#${thisJob} started`);
// 3 job instances will be running at the same time, as specified by `concurrency` above
async setup({ isWorker }) {
await this.loadDependencies(['oors.mongodb', 'oors.logger', 'oors.autoloader']);
this.agenda = new Agenda({
mongo: this.deps['oors.mongodb'].getConnectionDb(),
...this.getConfig('agendaConfig'),
});
this.export({
agenda: this.agenda,
defineJob: this.defineJob,
defineJobs: this.defineJobs,
createJob: this.createJob,
});
await this.runHook('loadJobs', this.collectFromModule, {
agenda: this.agenda,
defineJob: this.defineJob,
defineJobs: this.defineJobs,
});
function startAgenda () {
const deferred = defer()
agenda = new Agenda({
mongo: connectionAgenda
})
agenda.on('start', job => logger.info(`starting job: ${job.attrs.name}, Last Ran at: ${job.attrs.lastRunAt}`))
agenda.on('fail', (err, job) => logger.error(`Job ${job.attrs.name} failed with ${err.message}`))
agenda.on('complete', job => logger.info(`Job ${job.attrs.name} has completed`))
agenda.on('ready', () => {
if (config.alerts.enableAlerts) { alerts.setupAgenda(agenda) }
if (config.reports.enableReports) { reports.setupAgenda(agenda) }
if (config.bodyCull.enabled) { bodyCull.setupAgenda(agenda) }
autoRetry.setupAgenda(agenda)
if (config.polling.enabled) {
return polling.setupAgenda(agenda, () =>
constructor(options: RoutinesOptions, Apm?: any) {
let opt = {};
this.Web = options.web;
this.apm = Apm;
if (options.mongoConString) {
opt = {
db: {
address: options.mongoConString || null,
collection: 'agenda',
options: {
useNewUrlParser: true,
}
}
}
}
this.Agenda = new agenda(opt);
this.Agenda.name(`${os.hostname}-${process.pid}`);
this.Agenda.start();
this.Web.app.use('/routines', Agendash(this.Agenda));
}
import {promisify} from 'util';
import Agenda from 'agenda';
import settings from '../settings';
import {bootstrapKoaApp} from './util';
import {defineJob, jobOperations, jobAssertions, promiseJobOperation} from './job';
const {app, router} = bootstrapKoaApp();
const agenda = new Agenda({
db: {
address: settings.agendaMongoUrl,
collection: settings.collection
}
});
const jobsReady = agenda._ready
.then(async () => {
const jobs = agenda._mdb.collection(settings.definitions);
jobs.toArray = () => {
const jobsCursor = jobs.find();
return promisify(jobsCursor.toArray).bind(jobsCursor)();
};
await jobs.toArray()
.then(jobsArray => Promise.all(jobsArray.map(job => defineJob(job, jobs, agenda))));