Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Meteor3 update - please check and fix it #153

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .npm/package/npm-shrinkwrap.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
Package.describe({
summary: "Define and run scheduled jobs across multiple servers.",
version: "1.5.2",
version: "3.0.0",
name: "percolate:synced-cron",
git: "https://github.com/percolatestudio/meteor-synced-cron.git"
});

Npm.depends({later: "1.1.6"});
Npm.depends({'@breejs/later': "4.2.0"});

Package.onUse(function (api) {
api.versionsFrom('[email protected]');
api.versionsFrom("3.0");
api.use(['underscore', 'check', 'mongo', 'logging'], 'server');
api.addFiles(['synced-cron-server.js'], "server");
api.export('SyncedCron', 'server');
Expand Down
103 changes: 53 additions & 50 deletions synced-cron-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ SyncedCron = {
}
}

Later = Npm.require('later');
Later = Npm.require('@breejs/later');

/*
Logger factory function. Takes a prefix string and options object
Expand Down Expand Up @@ -87,11 +87,11 @@ Meteor.startup(function() {

// collection holding the job history records
SyncedCron._collection = new Mongo.Collection(options.collectionName);
SyncedCron._collection._ensureIndex({intendedAt: 1, name: 1}, {unique: true});
SyncedCron._collection.createIndex({intendedAt: 1, name: 1}, {unique: true});

if (options.collectionTTL) {
if (options.collectionTTL > minTTL)
SyncedCron._collection._ensureIndex({startedAt: 1 },
SyncedCron._collection.createIndex({startedAt: 1 },
{ expireAfterSeconds: options.collectionTTL } );
else
log.warn('Not going to use a TTL that is shorter than:' + minTTL);
Expand Down Expand Up @@ -193,59 +193,62 @@ SyncedCron._entryWrapper = function(entry) {
var self = this;

return function(intendedAt) {
intendedAt = new Date(intendedAt.getTime());
intendedAt.setMilliseconds(0);

var jobHistory;
const syncFunction = Meteor.wrapAsync(async () => {
intendedAt = new Date(intendedAt.getTime());
intendedAt.setMilliseconds(0);

var jobHistory;

if (entry.persist) {
jobHistory = {
intendedAt: intendedAt,
name: entry.name,
startedAt: new Date()
};

// If we have a dup key error, another instance has already tried to run
// this job.
try {
jobHistory._id = await self._collection.insertAsync(jobHistory);
} catch(e) {
// http://www.mongodb.org/about/contributors/error-codes/
// 11000 == duplicate key error
if (e.code === 11000) {
log.info('Not running "' + entry.name + '" again.');
return;
}

if (entry.persist) {
jobHistory = {
intendedAt: intendedAt,
name: entry.name,
startedAt: new Date()
};
throw e;
};
}

// If we have a dup key error, another instance has already tried to run
// this job.
// run and record the job
try {
jobHistory._id = self._collection.insert(jobHistory);
log.info('Starting "' + entry.name + '".');
var output = entry.job(intendedAt,entry.name); // <- Run the actual job

log.info('Finished "' + entry.name + '".');
if(entry.persist) {
await self._collection.updateAsync({_id: jobHistory._id}, {
$set: {
finishedAt: new Date(),
result: output
}
});
}
} catch(e) {
// http://www.mongodb.org/about/contributors/error-codes/
// 11000 == duplicate key error
if (e.code === 11000) {
log.info('Not running "' + entry.name + '" again.');
return;
log.info('Exception "' + entry.name +'" ' + ((e && e.stack) ? e.stack : e));
if(entry.persist) {
await self._collection.updateAsync({_id: jobHistory._id}, {
$set: {
finishedAt: new Date(),
error: (e && e.stack) ? e.stack : e
}
});
}

throw e;
};
}

// run and record the job
try {
log.info('Starting "' + entry.name + '".');
var output = entry.job(intendedAt,entry.name); // <- Run the actual job

log.info('Finished "' + entry.name + '".');
if(entry.persist) {
self._collection.update({_id: jobHistory._id}, {
$set: {
finishedAt: new Date(),
result: output
}
});
}
} catch(e) {
log.info('Exception "' + entry.name +'" ' + ((e && e.stack) ? e.stack : e));
if(entry.persist) {
self._collection.update({_id: jobHistory._id}, {
$set: {
finishedAt: new Date(),
error: (e && e.stack) ? e.stack : e
}
});
}
}
});
syncFunction();
};
}

Expand Down