From f9c7236bef38db2ad9a911bff4699e5c42da575d Mon Sep 17 00:00:00 2001 From: klaucode <103204675+klaucode@users.noreply.github.com> Date: Mon, 25 Nov 2024 09:27:37 +0100 Subject: [PATCH] Meteor3 update - please check and fix it --- .npm/package/npm-shrinkwrap.json | 8 +-- package.js | 6 +- synced-cron-server.js | 103 ++++++++++++++++--------------- 3 files changed, 60 insertions(+), 57 deletions(-) diff --git a/.npm/package/npm-shrinkwrap.json b/.npm/package/npm-shrinkwrap.json index a734887..4bf9d39 100644 --- a/.npm/package/npm-shrinkwrap.json +++ b/.npm/package/npm-shrinkwrap.json @@ -1,10 +1,10 @@ { "lockfileVersion": 1, "dependencies": { - "later": { - "version": "1.1.6", - "resolved": "https://registry.npmjs.org/later/-/later-1.1.6.tgz", - "integrity": "sha1-Wvg61IJjk8VvEO4u4ekQlkRb5Os=" + "@breejs/later": { + "version": "4.2.0", + "resolved": "https://registry.npmjs.org/@breejs/later/-/later-4.2.0.tgz", + "integrity": "sha512-EVMD0SgJtOuFeg0lAVbCwa+qeTKILb87jqvLyUtQswGD9+ce2nB52Y5zbTF1Hc0MDFfbydcMcxb47jSdhikVHA==" } } } diff --git a/package.js b/package.js index 293bbff..870f6b8 100644 --- a/package.js +++ b/package.js @@ -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('METEOR@1.3'); + api.versionsFrom("3.0"); api.use(['underscore', 'check', 'mongo', 'logging'], 'server'); api.addFiles(['synced-cron-server.js'], "server"); api.export('SyncedCron', 'server'); diff --git a/synced-cron-server.js b/synced-cron-server.js index 7b80f09..dff3755 100644 --- a/synced-cron-server.js +++ b/synced-cron-server.js @@ -24,7 +24,7 @@ SyncedCron = { } } -Later = Npm.require('later'); +Later = Npm.require('@breejs/later'); /* Logger factory function. Takes a prefix string and options object @@ -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); @@ -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(); }; }