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

[Meteor 3] Update package to async #149

Open
wants to merge 4 commits 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
2 changes: 1 addition & 1 deletion .npm/package/npm-shrinkwrap.json

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

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

Npm.depends({later: "1.1.6"});

Package.onUse(function (api) {
api.versionsFrom('[email protected]');
api.versionsFrom(['[email protected]', '3.0-alpha.19']);
api.use("ecmascript");
api.use(['underscore', 'check', 'mongo', 'logging'], 'server');
api.addFiles(['synced-cron-server.js'], "server");
api.export('SyncedCron', 'server');
});

Package.onTest(function (api) {
api.use(['check', 'mongo'], 'server');
api.use("ecmascript");
api.use(['tinytest', 'underscore', 'logging']);
api.addFiles(['synced-cron-server.js', 'synced-cron-tests.js'], ['server']);
});
24 changes: 12 additions & 12 deletions synced-cron-server.js
Original file line number Diff line number Diff line change
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 @@ -140,7 +140,7 @@ SyncedCron.start = function() {

Meteor.startup(function() {
// Schedule each job with later.js
_.each(self._entries, function(entry) {
_.each(self._entries, entry => {
scheduleEntry(entry);
});
self.running = true;
Expand Down Expand Up @@ -192,7 +192,7 @@ SyncedCron.stop = function() {
SyncedCron._entryWrapper = function(entry) {
var self = this;

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

Expand All @@ -208,7 +208,7 @@ SyncedCron._entryWrapper = function(entry) {
// If we have a dup key error, another instance has already tried to run
// this job.
try {
jobHistory._id = self._collection.insert(jobHistory);
jobHistory._id = await self._collection.insertAsync(jobHistory);
} catch(e) {
// http://www.mongodb.org/about/contributors/error-codes/
// 11000 == duplicate key error
Expand All @@ -224,11 +224,11 @@ SyncedCron._entryWrapper = function(entry) {
// run and record the job
try {
log.info('Starting "' + entry.name + '".');
var output = entry.job(intendedAt,entry.name); // <- Run the actual job
var output = await entry.job(intendedAt,entry.name); // <- Run the actual job

log.info('Finished "' + entry.name + '".');
if(entry.persist) {
self._collection.update({_id: jobHistory._id}, {
await self._collection.updateAsync({_id: jobHistory._id}, {
$set: {
finishedAt: new Date(),
result: output
Expand All @@ -238,7 +238,7 @@ SyncedCron._entryWrapper = function(entry) {
} catch(e) {
log.info('Exception "' + entry.name +'" ' + ((e && e.stack) ? e.stack : e));
if(entry.persist) {
self._collection.update({_id: jobHistory._id}, {
await self._collection.updateAsync({_id: jobHistory._id}, {
$set: {
finishedAt: new Date(),
error: (e && e.stack) ? e.stack : e
Expand All @@ -250,9 +250,9 @@ SyncedCron._entryWrapper = function(entry) {
}

// for tests
SyncedCron._reset = function() {
SyncedCron._reset = async function() {
this._entries = {};
this._collection.remove({});
await this._collection.removeAsync({});
this.running = false;
}

Expand All @@ -274,10 +274,10 @@ SyncedCron._laterSetInterval = function(fn, sched) {
* Executes the specified function and then sets the timeout for the next
* interval.
*/
function scheduleTimeout(intendedAt) {
async function scheduleTimeout(intendedAt) {
if(!done) {
try {
fn(intendedAt);
await fn(intendedAt);
} catch(e) {
log.info('Exception running scheduled job ' + ((e && e.stack) ? e.stack : e));
}
Expand Down
78 changes: 41 additions & 37 deletions synced-cron-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ var TestEntry = {
}
};

Tinytest.add('Syncing works', function(test) {
SyncedCron._reset();
test.equal(SyncedCron._collection.find().count(), 0);
Tinytest.addAsync('Syncing works', async function(test) {
await SyncedCron._reset();
const count = await SyncedCron._collection.find().countAsync()
test.equal(count, 0);

// added the entry ok
SyncedCron.add(TestEntry);
Expand All @@ -24,20 +25,22 @@ Tinytest.add('Syncing works', function(test) {
var intendedAt = new Date(); //whatever

// first run
SyncedCron._entryWrapper(entry)(intendedAt);
test.equal(SyncedCron._collection.find().count(), 1);
var jobHistory1 = SyncedCron._collection.findOne();
await SyncedCron._entryWrapper(entry)(intendedAt);
const count2 = await SyncedCron._collection.find().countAsync();
test.equal(count2, 1);
var jobHistory1 = await SyncedCron._collection.findOneAsync();
test.equal(jobHistory1.result, 'ran');

// second run
SyncedCron._entryWrapper(entry)(intendedAt);
test.equal(SyncedCron._collection.find().count(), 1); // should still be 1
var jobHistory2 = SyncedCron._collection.findOne();
await SyncedCron._entryWrapper(entry)(intendedAt);
const count3 = await SyncedCron._collection.find().countAsync();
test.equal(count3, 1); // should still be 1
var jobHistory2 = await SyncedCron._collection.findOneAsync();
test.equal(jobHistory1._id, jobHistory2._id);
});

Tinytest.add('Exceptions work', function(test) {
SyncedCron._reset();
Tinytest.addAsync('Exceptions work', async function(test) {
await SyncedCron._reset();
SyncedCron.add(_.extend({}, TestEntry, {
job: function() {
throw new Meteor.Error('Haha, gotcha!');
Expand All @@ -49,16 +52,16 @@ Tinytest.add('Exceptions work', function(test) {
var intendedAt = new Date(); //whatever

// error without result
SyncedCron._entryWrapper(entry)(intendedAt);
test.equal(SyncedCron._collection.find().count(), 1);
var jobHistory1 = SyncedCron._collection.findOne();
await SyncedCron._entryWrapper(entry)(intendedAt);
test.equal(await SyncedCron._collection.find().countAsync(), 1);
var jobHistory1 = await SyncedCron._collection.findOneAsync();
test.equal(jobHistory1.result, undefined);
test.matches(jobHistory1.error, /Haha, gotcha/);
});

Tinytest.add('SyncedCron.nextScheduledAtDate works', function(test) {
SyncedCron._reset();
test.equal(SyncedCron._collection.find().count(), 0);
Tinytest.addAsync('SyncedCron.nextScheduledAtDate works', async function(test) {
await SyncedCron._reset();
test.equal(await SyncedCron._collection.find().countAsync(), 0);

// addd 2 entries
SyncedCron.add(TestEntry);
Expand All @@ -82,9 +85,10 @@ Tinytest.add('SyncedCron.nextScheduledAtDate works', function(test) {
});

// Tests SyncedCron.remove in the process
Tinytest.add('SyncedCron.stop works', function(test) {
SyncedCron._reset();
test.equal(SyncedCron._collection.find().count(), 0);
Tinytest.addAsync('SyncedCron.stop works', async function(test) {
await SyncedCron._reset();
const count = await SyncedCron._collection.find().countAsync();
test.equal(count, 0);

// addd 2 entries
SyncedCron.add(TestEntry);
Expand All @@ -106,9 +110,9 @@ Tinytest.add('SyncedCron.stop works', function(test) {
test.equal(_.keys(SyncedCron._entries).length, 0);
});

Tinytest.add('SyncedCron.pause works', function(test) {
SyncedCron._reset();
test.equal(SyncedCron._collection.find().count(), 0);
Tinytest.addAsync('SyncedCron.pause works', async function(test) {
await SyncedCron._reset();
test.equal(await SyncedCron._collection.find().countAsync(), 0);

// addd 2 entries
SyncedCron.add(TestEntry);
Expand Down Expand Up @@ -138,10 +142,10 @@ Tinytest.add('SyncedCron.pause works', function(test) {
});

// Tests SyncedCron.remove in the process
Tinytest.add('SyncedCron.add starts by it self when running', function(test) {
SyncedCron._reset();
Tinytest.addAsync('SyncedCron.add starts by it self when running', async function(test) {
await SyncedCron._reset();

test.equal(SyncedCron._collection.find().count(), 0);
test.equal(await SyncedCron._collection.find().countAsync(), 0);
test.equal(SyncedCron.running, false);
Log._intercept(2);

Expand All @@ -163,8 +167,8 @@ Tinytest.add('SyncedCron.add starts by it self when running', function(test) {
test.equal(_.keys(SyncedCron._entries).length, 0);
});

Tinytest.add('SyncedCron.config can customize the options object', function(test) {
SyncedCron._reset();
Tinytest.addAsync('SyncedCron.config can customize the options object', async function(test) {
await SyncedCron._reset();

SyncedCron.config({
log: false,
Expand All @@ -179,8 +183,8 @@ Tinytest.add('SyncedCron.config can customize the options object', function(test
test.equal(SyncedCron.options.collectionTTL, 0);
});

Tinytest.addAsync('SyncedCron can log to injected logger', function(test, done) {
SyncedCron._reset();
Tinytest.addAsync('SyncedCron can log to injected logger', async function(test, done) {
await SyncedCron._reset();

var logger = function() {
test.isTrue(true);
Expand All @@ -197,8 +201,8 @@ Tinytest.addAsync('SyncedCron can log to injected logger', function(test, done)
SyncedCron.options.logger = null;
});

Tinytest.addAsync('SyncedCron should pass correct arguments to logger', function(test, done) {
SyncedCron._reset();
Tinytest.addAsync('SyncedCron should pass correct arguments to logger', async function(test, done) {
await SyncedCron._reset();

var logger = function(opts) {
test.include(opts, 'level');
Expand Down Expand Up @@ -229,14 +233,14 @@ Tinytest.add('Single time schedules don\'t break', function(test) {
});


Tinytest.add('Do not persist when flag is set to false', function (test) {
SyncedCron._reset();
Tinytest.addAsync('Do not persist when flag is set to false', async function (test) {
await SyncedCron._reset();

var testEntryNoPersist = _.extend({}, TestEntry, {persist: false});

SyncedCron.add(testEntryNoPersist);

const now = new Date();
SyncedCron._entryWrapper(testEntryNoPersist)(now);
test.equal(SyncedCron._collection.find().count(), 0);
});
await SyncedCron._entryWrapper(testEntryNoPersist)(now);
test.equal(await SyncedCron._collection.find().countAsync(), 0);
});