0

I am using Agenda 0.9.0, mongoose 4.7.5, co 4.6.5, for some unknown reason done() is never called in none of the places. The job is timing out and is run every 10 s instead of 2 (if i don't override default 10 minutes, it will run every 10 mins).

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();

Here is mongodb entry

{
   "_id" : ObjectId("58655e78711386ff39e651f1"),
   "name" : "check old entries",
   "type" : "single",
   "data" : null,
   "priority" : 0,
   "repeatInterval" : "2 seconds",
   "repeatTimezone" : null,
   "lastModifiedBy" : null,
   "nextRunAt" : ISODate("2016-12-29T19:05:30.118Z"),
   "lockedAt" : ISODate("2016-12-29T19:05:28.115Z"),
   "lastRunAt" : ISODate("2016-12-29T19:05:28.118Z")
}
gad0lin
  • 101
  • 7

1 Answers1

0

Lesson learned - try executing least code as possible and reduce all additional libraries. I have removed agenda, co and just run simple print of single element.

http://thecodebarbarian.com/cursors-in-mongoose-45.html

const cursor = Token.find({}).cursor();
// Print the first document. Can also use callbacks
cursor.next.then(doc => { console.log(doc); });

This has thrown next is not a function. That hinted that sth is not right with mongoose (didn't have to worry looking into co, agenda...). I have re-read the code and noticed that I am missing mongoose.connect(). That error was not caught by any of the try/catch blocks, so I was completely blind.

gad0lin
  • 101
  • 7