Created
December 29, 2016 19:27
-
-
Save gad0lin/3f88ec20275d5656644bb0de2124a99d to your computer and use it in GitHub Desktop.
no callback nodejs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var co = require('co'), | |
Agenda = require('agenda'); | |
var check = function (queryName, checkCursor, done) { | |
console.log("1. this gets printed ..."); | |
co(function*() { | |
try { | |
console.log("2. this gets printed"); | |
const cursor = checkCursor; | |
for (let token = yield cursor.next(); token != null; token = yield cursor.next()) { | |
console.log("this is not printed"); | |
} | |
} finally { | |
console.log("this is not printed"); | |
} | |
console.log("this is not printed"); | |
done(); | |
}).then(function () { | |
console.log("this is not printed"); | |
done(); | |
}, function () { | |
console.log("this is not printed"); | |
done(); | |
}).catch(function(e) { | |
console.log("this is not printed"); | |
}); | |
}; | |
var mongoose = require("mongoose"); | |
var Schema = mongoose.Schema; | |
var TokenSchema = new Schema({ | |
ts: Number | |
}); | |
var Token = mongoose.model("Token", TokenSchema); | |
var enableChecks = function () { | |
var agenda = new Agenda(); | |
agenda.database("mongodb://127.0.0.1:27017/eis", 'scheduler'); | |
agenda.defaultLockLifetime(10000); | |
agenda.define("check old entries", function (job, done) { | |
console.log("00. this get printed (job is run every 10s - it times out ): " + job.attrs.name); | |
var c = Token.find({}).cursor(); | |
console.log("0. this gets printed (got cursor): " + job.attrs.name); | |
check(job.attrs.name, c, done); // this collection is empty | |
}); | |
agenda.on('ready', function () { | |
agenda.every("2 seconds", "check old entries"); | |
agenda.start(); | |
}); | |
agenda.on('error', function (err) { | |
console.log("Mongo connection failed"); | |
}); | |
}; | |
enableChecks(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment