How I schedule jobs for my pet project with Agenda JS

// mailer.jsconst config = require(‘../config’);
const mailer = require(‘@sendgrid/mail’);
mailer.setApiKey(config.sendgrid);module.exports = {
sendActiveEmail: async ({ username, email, url }) => {
try {
const msg = {
to: email,
from: ‘learn-svelte@example.com’,
subject: ‘Your active link’,
text: `Hi ${username}, please click the following url to activate your account: ${url}.\nThank you!`,
html: `<p>Hi <strong>${username}</strong>,</p>
<p>Please click <a href=${url}>the following url</a> to activate your account.</p>
<p>Thank you!</p>`,
};
const send = await mailer.send(msg);
console.log(‘Active email is sent to ‘ + email);
return send;
} catch (err) {
console.error(‘Cannot send email!\n’, err);
}
},
};
// userService.jsmodule.exports = {

createUser: async ({ username, email, password }) => {
const payload = { username, email, password };
await validateRegister(payload);
payload.password = await bcrypt.hash(password, 10);
let newUser = await UserModel.create(payload);
const send = myEmitter.emit(‘register’, {
username,
email,
url: newUser.activeLink,
});
if (!send) myEmitter.emit(‘error’);
return newUser;
},
}
,
// event.jsconst EventEmitter = require(‘events’);
const myEmitter = new EventEmitter();
const mailer = require(‘../services/mailer’);myEmitter.on(‘register’, async ({ username, email, url }) => {
await mailer.sendActiveEmail({ username, email, url });
});
module.exports = myEmitter;
// agenda.jsconst Agenda = require(‘agenda’);
const config = require(‘../config’);
const mailer = require(‘../services/mailer’);const agenda = new Agenda({
db: { address: config.database, collection: ‘agendaJobs’ },
});
agenda
.on(‘ready’, () => console.log(‘Agenda started!’))
.on(‘error’, () => console.log(‘Agenda connection error!’));
agenda.define(‘register’, async job => {
const { username, email, url } = job.attrs.data;
await mailer.sendActiveEmail({ username, email, url });
});
agenda.define(‘welcome’, async job => {
const { username, email } = job.attrs.data;
await mailer.sendWelcomeEmail({ username, email });
});
agenda.start();module.exports = agenda;
// scheduler.jsconst agenda = require(‘./agenda’);module.exports = {
scheduleActiveEmail: async ({ username, email, url }) => {
await agenda.schedule(‘in 1 second’, ‘register’, {
username,
email,
url,
});
},
scheduleResendActiveEmail: async ({ username, email, url }) => {
await agenda.schedule(‘in 1 second’, ‘resend’, {
username,
email,
url,
});
},
scheduleWelcomeEmail: async ({ username, email }) => {
await agenda.schedule(‘in 30 seconds’, ‘welcome’, { username, email });
},
};
// userService.jsmodule.exports = {
createUser: async ({ username, email, password }) => {
const payload = { username, email, password };
await validateRegister(payload);
payload.password = await bcrypt.hash(password, 10);
let newUser = await UserModel.create(payload);
await scheduler.scheduleActiveEmail({
username,
email,
url: newUser.activeLink,
});
return newUser;
},
activeUser: async ({ userId, uuid }) => {
const user = await getUserById(userId);

await scheduler.scheduleWelcomeEmail({
username: user.username,
email: user.email,
});
user.active = true;
user.welcome = true;
await user.save();
return { userId: user._id, active: user.active };
},
};

--

--

From translator -> Manual QC Engineer -> Automation QA Engineer -> NodeJS Developer -> yearning to learn more

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store