im using agenda library (https://github.com/rschmukler/agenda/)
I can create and run jobs correctly, but i need to start jobs when i start the server.
I do it this in a module:
var startAlerts = function(){
log.debug('alerts', 'launching alerts');
console.log('alerts', 'launching alerts');
var agenda = new require('agenda')({
db: {
address: db.serverConfig.name + "/" + db.databaseName,
collection: 'alerts'
},
processEvery: '24 hours',
maxConcurrency: 250
});
agenda.start();
};
startAlerts();
But the jobs dont start..What im doing wrong?
Thanks.
Related
I am trying to use agenda to schedule jobs at a certain date and time, but the jobs are not running when the specified date is reached.
This is how I'm creating the job:
agenda.create(type, data)
.schedule(new Date(startDate))
.repeatEvery('11 21 * * *', {timezone: 'Europe/Bucharest'})
.save();
This is how I start agenda:
const Agenda = require('agenda');
const mongoDB = process.env.DB_PATH;
const mongoConnectionString = mongoDB;
let agenda = new Agenda({db: {address: mongoConnectionString, collection: 'jobs'}});
let jobTypes = process.env.JOB_TYPES ? process.env.JOB_TYPES.split(',') : [];
jobTypes.forEach(function(type) {
require('./jobs/' + type)(agenda);
});
if(jobTypes.length) {
agenda.on('ready', function() {
console.log('agenda start')
agenda.start();
});
}
function graceful() {
agenda.stop(function() {
process.exit(0);
});
}
process.on('SIGTERM', graceful);
process.on('SIGINT' , graceful);
export default agenda;
This is an example with a job that didn't start on the scheduled date:
Is there something I'm doing wrong?
EDIT:
The job is triggered if I do schedule(new Date()), but it won't using a defined date.
I'm trying to add in Agenda into my node application so I can run some background tasks on a daily basis, e.g. Deactivating users who have not logged in for 60 days.
I've tried following the example on the GitHub associated with the module, but seem to be running into a problem with my app hanging whenever I try to load the site, and ultimately getting an "Error 504: Gateway Server Timeout". I'm also seeing undefined in the console.
I know Agenda is up and working correctly, as I have a simple job at the moment that just does a console.log every minute.
In my app.js I require my worker.js file:
var agenda = require('./worker.js');
My worker.js is just a simple 1 line:
require('./lib/agenda.js');
agenda.js:
var Agenda = require('agenda');
var connectionString = "mongodb://" + process.env.MONGODB_USER + ":" +
process.env.MONGODB_PASSWORD + "#" +
process.env.DATABASE_SERVICE_NAME + ':' +
process.env.MONGODB_PORT + '/' +
process.env.MONGODB_DATABASE;
var agenda = new Agenda({db: {address: connectionString}});
var jobTypes = process.env.JOB_TYPES ? process.env.JOB_TYPES.split(',') : [];
jobTypes.forEach(function(type){
require('./jobs/' + type)(agenda);
});
if (jobTypes.length) {
agenda.on('ready', function() {
agenda.every('* * * * *', 'test job') //Run job at 0030 every day
agenda.start();
})
}
module.exports = agenda
and the test job is defined in a job file like so:
agenda.define('test job', function(job, done) {
console.log ('Agenda job executed');
done();
});
I feel like I am missing something really obvious!
It turns out that I needed to add an agenda.processEvery in the start command section of my agenda.js:
var Agenda = require('agenda');
var connectionString = "mongodb://" + process.env.MONGODB_USER + ":" +
process.env.MONGODB_PASSWORD + "#" +
process.env.DATABASE_SERVICE_NAME + ':' +
process.env.MONGODB_PORT + '/' +
process.env.MONGODB_DATABASE;
var agenda = new Agenda({db: {address: connectionString}});
var jobTypes = process.env.JOB_TYPES ? process.env.JOB_TYPES.split(',') : [];
jobTypes.forEach(function(type){
require('./jobs/' + type)(agenda);
});
if (jobTypes.length) {
agenda.on('ready', function() {
agenda.every('* * * * *', 'test job') //Run job at 0030 every day
agenda.processEvery('one minute'); //<====== This is the new line
agenda.start();
})
}
module.exports = agenda
I have the Hazelcast cluster server running in the 172.30.56.60, 61, 62
(i.e)
[ Member {
address: Address { host: '172.30.56.60', port: 5701, type: 4 },
uuid: 'bd6428f0-e888-453f-872f-6fe8296d751d',
isLiteMember: false,
attributes: {} },
Member {
address: Address { host: '172.30.56.61', port: 5701, type: 4 },
uuid: 'e0cd795a-0ca5-41ab-907a-492b61690a18',
isLiteMember: false,
attributes: {} },
Member {
address: Address { host: '172.30.56.62', port: 5701, type: 4 },
uuid: '0a834ae8-e707-4b5b-945b-362bfea08cf5',
isLiteMember: false,
attributes: {} } ]
I try to implement the Hazelcast distributed locking using nodejs using the following code,
// Initialize the hazelcast client instance.
var HazelcastClient = require('hazelcast-client').Client;
var Config = require('hazelcast-client').Config;
var config = new Config.ClientConfig();
config.networkConfig.addresses = [{host: '172.30.56.60', port: '5701'},{host: '172.30.56.61', port: '5701'}, {host: '172.30.56.62', port: '5701'}];
var lock = {};
var sleep = require('sleep');
HazelcastClient
.newHazelcastClient(config)
.then(function (hazelcastClient) {
lock = hazelcastClient.getLock("lock1");
// do stuff with lock
lock.lock();
console.log('Am locked in node with lock1...will be locked for 20 seconds');
sleep.sleep(20);
console.log('Unlocked now...');
lock.unlock();
process.exit();
});
I started the script node by node, I expected to establish the lock node by node, but instead it locks all the nodes in the same time. So it is not working as a distributed lock, so all the script started and ending in the same time (NOTE : For testing I provided 20 seconds sleep)
Please let me know, How to establish the distributed lock using node js in Hazelcast.
I found the answer myself, I didn't realize the return of promise, my bad (new to nodejs)
// Initialize the hazelcast client instance.
var HazelcastClient = require('hazelcast-client').Client;
var Config = require('hazelcast-client').Config;
var config = new Config.ClientConfig();
config.networkConfig.addresses = [{host: '172.30.56.60', port: '5701'},{host: '172.30.56.61', port: '5701'}, {host: '172.30.56.62', port: '5701'}];
var sleep = require('sleep');
// Test process
HazelcastClient
.newHazelcastClient(config)
.then(function (hazelcastClient) {
var lock = hazelcastClient.getLock('rowId_tablename');
// lock the code here
lock.lock().then(function() {
console.log('Am locked in node with lock3...will be locked for 20 seconds');
sleep.sleep(20);
// unlock after process
return lock.unlock();
}).then(function() {
console.log('unlocked now');
});
});
I am using agenda to run jobs in my nodejs application, below is my agenda.js file for agenda config.
var Agenda = require('agenda');
var connectionString = 'localhost:27017/csgo';
var agenda = new Agenda({db: { address: connectionString, collection: 'jobs' }});
require('./jobs/game_play')(agenda);
module.exports = agenda;
Below is my script to run a game play in every 5 seconds,
module.exports = function(agenda) {
agenda.define('start new play', function(job, done) {
console.log('new play agenda');
});
agenda.on('ready', function() {
agenda.every('5 seconds', 'start new play');
agenda.start();
});
}
After running my agenda.js script, below is my job which gets saved in the database,
{ "_id" : ObjectId("59423d0e4b9581f728af1b6a"), "name" : "start new play", "type" : "single", "data" : null, "priority" : 0, "repeatInterval" : "5 seconds", "repeatTimezone" : null, "lastModifiedBy" : null, "nextRunAt" : ISODate("2017-06-15T07:53:55.794Z"), "lockedAt" : ISODate("2017-06-15T07:53:50.789Z"), "lastRunAt" : ISODate("2017-06-15T07:53:50.794Z") }
Instead of 5 seconds,my job is running after every 5 minutes, what can be the problem.
add done() to finish the process. Hope it help!
module.exports = function(agenda) {
agenda.define('start new play', function(job, done) {
console.log('new play agenda');
done();
});
agenda.on('ready', function() {
agenda.every('5 seconds', 'start new play');
agenda.start();
});
}
Agenda module is based on human interval module(https://github.com/rschmukler/human-interval).
On the documentation you can see that seconds are supported but the min interval you can set is 1 minute.
They say that seconds are supported because you can set the interval as '1 minute and 30 seconds'.
You can try to pass interval as cron format:
module.exports = function(agenda) {
agenda.define('start new play', function(job, done) {
console.log('new play agenda');
});
agenda.on('ready', function() {
agenda.every('*/5 * * * * *', 'start new play');
agenda.start();
});
}
It it's not supported you would need to consider to use a different module like https://www.npmjs.com/package/node-cron or https://www.npmjs.com/package/node-schedule
I'm running gulp as build tool for my nodejs server. I have one issue with nodemon (Nodemon is a utility that will monitor for any changes in your source and automatically restart your server. ), since i'm not able to execute a gulp task to transform my ecmascript 6 code before the server restarts.
This is my code. Nodemon documentation says that syncronous task execution is possible with node version 0.12.0. My node version is 0.12.6. Anyone noticed the same problem or knows how to fix?
https://github.com/JacksonGariety/gulp-nodemon
var gulp = require('gulp');
var noDemon = require('nodemon');
var babel = require('gulp-babel');
var plumber = require('gulp-plumber');
gulp.task('server', ['transformES6'], function () {
noDemon({
script : 'server/dist/server.js',
ext : 'js',
env : {
PORT : 8000
},
ignore : [
'./node_modules/**',
'./server/dist/**'
],
tasks : ['transformES6']
}).on('restart', function () {
var now = new Date();
console.log('server is restarting: ' + now.getHours() + ' Hours, ' + now.getMinutes() + ' Minutes, ' + now.getSeconds() + ' Seconds');
});
});
gulp.task('transformES6', function () {
return gulp.src(['./server/**/**/**/**/*.js', '!./server/dist/'])
.pipe(plumber())
.pipe(babel())
.pipe(gulp.dest('./server/dist/'));
})
Thanks in advance!
Greetings Mauro