0

我正在使用 Agenda 0.9.0、mongoose 4.7.5、co 4.6.5,出于某种未知原因,done() 从未在任何地方调用过。该作业超时并且每 10 秒而不是 2 秒运行一次(如果我不覆盖默认的 10 分钟,它将每 10 分钟运行一次)。

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

这是mongodb条目

{
   "_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")
}
4

1 回答 1

0

经验教训 - 尝试尽可能少地执行代码并减少所有额外的库。我已经删除了议程,合作并只运行单个元素的简单打印。

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

这已经抛出next不是一个函数。这暗示猫鼬不适合(不必担心调查合作,议程......)。我重新阅读了代码并注意到我缺少 mongoose.connect()。任何 try/catch 块都没有捕获到该错误,所以我完全失明了。

于 2016-12-30T19:21:29.053 回答