[NEW][APPS] New Scheduler API (#19290)

Co-authored-by: Douglas Gubert <douglas.gubert@gmail.com>
pull/19379/head
Thassio Victor 5 years ago committed by GitHub
parent 96c3c75624
commit 97bdfb56ae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 6
      app/apps/server/bridges/bridges.js
  2. 2
      app/apps/server/bridges/index.js
  3. 62
      app/apps/server/bridges/scheduler.js
  4. 102
      package-lock.json
  5. 4
      package.json

@ -16,6 +16,7 @@ import { AppUserBridge } from './users';
import { AppLivechatBridge } from './livechat';
import { AppUploadBridge } from './uploads';
import { UiInteractionBridge } from './uiInteraction';
import { AppSchedulerBridge } from './scheduler';
export class RealAppBridges extends AppBridges {
constructor(orch) {
@ -37,6 +38,7 @@ export class RealAppBridges extends AppBridges {
this._livechatBridge = new AppLivechatBridge(orch);
this._uploadBridge = new AppUploadBridge(orch);
this._uiInteractionBridge = new UiInteractionBridge(orch);
this._schedulerBridge = new AppSchedulerBridge(orch);
}
getCommandBridge() {
@ -102,4 +104,8 @@ export class RealAppBridges extends AppBridges {
getUiInteractionBridge() {
return this._uiInteractionBridge;
}
getSchedulerBridge() {
return this._schedulerBridge;
}
}

@ -10,6 +10,7 @@ import { AppRoomBridge } from './rooms';
import { AppInternalBridge } from './internal';
import { AppSettingBridge } from './settings';
import { AppUserBridge } from './users';
import { AppSchedulerBridge } from './scheduler';
export {
RealAppBridges,
@ -24,4 +25,5 @@ export {
AppSettingBridge,
AppUserBridge,
AppInternalBridge,
AppSchedulerBridge,
};

@ -0,0 +1,62 @@
import Agenda from 'agenda';
import { MongoInternals } from 'meteor/mongo';
function _callProcessor(processor) {
return (job) => processor(job.attrs.data);
}
export class AppSchedulerBridge {
constructor(orch) {
this.orch = orch;
this.scheduler = new Agenda({
mongo: MongoInternals.defaultRemoteCollectionDriver().mongo.client.db(),
collection: 'rocketchat_agenda_jobs',
});
this.isConnected = false;
}
async registerProcessors(processors = [], appId) {
this.orch.debugLog(`The App ${ appId } is registering job processors`, processors);
processors.forEach(({ id, processor }) => this.scheduler.define(id, _callProcessor(processor)));
}
async scheduleOnce(job, appId) {
this.orch.debugLog(`The App ${ appId } is scheduling an onetime job`, job);
await this.startScheduler();
await this.scheduler.schedule(job.when, job.id, job.data || {});
}
async scheduleRecurring(job, appId) {
this.orch.debugLog(`The App ${ appId } is scheduling a recurring job`, job);
await this.startScheduler();
await this.scheduler.every(job.cron, job.id, job.data || {});
}
async cancelJob(jobId, appId) {
this.orch.debugLog(`The App ${ appId } is canceling a job`, jobId);
await this.startScheduler();
try {
await this.scheduler.cancel({ name: jobId });
} catch (e) {
console.error(e);
}
}
async cancelAllJobs(appId) {
this.orch.debugLog(`Canceling all jobs of App ${ appId }`);
await this.startScheduler();
const matcher = new RegExp(`^_${ appId }`);
try {
await this.scheduler.cancel({ name: { $regex: matcher } });
} catch (e) {
console.error(e);
}
}
async startScheduler() {
if (!this.isConnected) {
await this.scheduler.start();
this.isConnected = true;
}
}
}

102
package-lock.json generated

@ -5028,9 +5028,9 @@
}
},
"@rocket.chat/apps-engine": {
"version": "1.19.0-alpha.3984",
"resolved": "https://registry.npmjs.org/@rocket.chat/apps-engine/-/apps-engine-1.19.0-alpha.3984.tgz",
"integrity": "sha512-w6sDKF5PNuYHS5MDbAs5TSsGFin6YsHjHiJ+ZQKsz6jkrZDOTP3T64OkfZdr3m+6XqUexBMevOIJ8QZ8QNQPvA==",
"version": "1.19.0-alpha.4006",
"resolved": "https://registry.npmjs.org/@rocket.chat/apps-engine/-/apps-engine-1.19.0-alpha.4006.tgz",
"integrity": "sha512-0OczVTVa9ZGqIlCoKSYhT3AuPwlnRPkOsPApk5L3Dx667Ch6VSpsjur8WPbRDfLR8T/XNIs026RMVIu0DxA9kQ==",
"requires": {
"adm-zip": "^0.4.9",
"cryptiles": "^4.1.3",
@ -8123,6 +8123,16 @@
}
}
},
"@types/agenda": {
"version": "2.0.9",
"resolved": "https://registry.npmjs.org/@types/agenda/-/agenda-2.0.9.tgz",
"integrity": "sha512-uSQmxC9/OWvXCprY5+xzDUt95NCAffijeODNM4qscWnl9TGXIxyeA59MKF1Wq0OUPpURiWylVuNGUzBku1KrIA==",
"dev": true,
"requires": {
"@types/mongodb": "*",
"@types/node": "*"
}
},
"@types/anymatch": {
"version": "1.3.1",
"resolved": "https://registry.npmjs.org/@types/anymatch/-/anymatch-1.3.1.tgz",
@ -9144,6 +9154,61 @@
"version": "github:RocketChat/adm-zip#34ac787ce7f45c6cea99df049deb17d0c476a3b5",
"from": "github:RocketChat/adm-zip"
},
"agenda": {
"version": "3.1.0",
"resolved": "https://registry.npmjs.org/agenda/-/agenda-3.1.0.tgz",
"integrity": "sha512-UtxV/37gkjDYl0H2Lr4hPrBqOhAgtxYeGSYooSd1eyOmXlK1wFkbs77nItOykufFRv6tR6fskWP2RkyBndXYtg==",
"requires": {
"cron": "~1.8.0",
"date.js": "~0.3.3",
"debug": "~4.1.1",
"human-interval": "~1.0.0",
"moment-timezone": "~0.5.27",
"mongodb": "~3.5.0"
},
"dependencies": {
"bl": {
"version": "2.2.1",
"resolved": "https://registry.npmjs.org/bl/-/bl-2.2.1.tgz",
"integrity": "sha512-6Pesp1w0DEX1N550i/uGV/TqucVL4AM/pgThFSN/Qq9si1/DF9aIHs1BxD8V/QU0HoeHO6cQRTAuYnLPKq1e4g==",
"requires": {
"readable-stream": "^2.3.5",
"safe-buffer": "^5.1.1"
}
},
"bson": {
"version": "1.1.5",
"resolved": "https://registry.npmjs.org/bson/-/bson-1.1.5.tgz",
"integrity": "sha512-kDuEzldR21lHciPQAIulLs1LZlCXdLziXI6Mb/TDkwXhb//UORJNPXgcRs2CuO4H0DcMkpfT3/ySsP3unoZjBg=="
},
"debug": {
"version": "4.1.1",
"resolved": "https://registry.npmjs.org/debug/-/debug-4.1.1.tgz",
"integrity": "sha512-pYAIzeRo8J6KPEaJ0VWOh5Pzkbw/RetuzehGM7QRRX5he4fPHx2rdKMB256ehJCkX+XRQm16eZLqLNS8RSZXZw==",
"requires": {
"ms": "^2.1.1"
}
},
"mongodb": {
"version": "3.5.11",
"resolved": "https://registry.npmjs.org/mongodb/-/mongodb-3.5.11.tgz",
"integrity": "sha512-0a9XI0BbgcUEmB+gykqiUGijUkVflR5B46ZWxTshTQB8yrQlByVSq/5968ojY6iXQ+sDojnuKHnpLBInkZq+6Q==",
"requires": {
"bl": "^2.2.1",
"bson": "^1.1.4",
"denque": "^1.4.1",
"require_optional": "^1.0.1",
"safe-buffer": "^5.1.2",
"saslprep": "^1.0.0"
}
},
"ms": {
"version": "2.1.2",
"resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz",
"integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w=="
}
}
},
"agent-base": {
"version": "6.0.1",
"resolved": "https://registry.npmjs.org/agent-base/-/agent-base-6.0.1.tgz",
@ -14662,6 +14727,14 @@
"warning": "^4.0.3"
}
},
"cron": {
"version": "1.8.2",
"resolved": "https://registry.npmjs.org/cron/-/cron-1.8.2.tgz",
"integrity": "sha512-Gk2c4y6xKEO8FSAUTklqtfSr7oTq0CiPQeLBG5Fl0qoXpZyMcj1SG59YL+hqq04bu6/IuEA7lMkYDAplQNKkyg==",
"requires": {
"moment-timezone": "^0.5.x"
}
},
"cross-fetch": {
"version": "2.2.3",
"resolved": "https://registry.npmjs.org/cross-fetch/-/cross-fetch-2.2.3.tgz",
@ -15382,6 +15455,24 @@
"integrity": "sha1-6vQ5/U1ISK105cx9vvIAZyueNFs=",
"dev": true
},
"date.js": {
"version": "0.3.3",
"resolved": "https://registry.npmjs.org/date.js/-/date.js-0.3.3.tgz",
"integrity": "sha512-HgigOS3h3k6HnW011nAb43c5xx5rBXk8P2v/WIT9Zv4koIaVXiH2BURguI78VVp+5Qc076T7OR378JViCnZtBw==",
"requires": {
"debug": "~3.1.0"
},
"dependencies": {
"debug": {
"version": "3.1.0",
"resolved": "https://registry.npmjs.org/debug/-/debug-3.1.0.tgz",
"integrity": "sha512-OX8XqP7/1a9cqkxYw2yXss15f26NKWBpDXQd0/uK/KPqdQhxbPa994hnzjcE2VqQpDslf55723cKPUOGSmMY3g==",
"requires": {
"ms": "2.0.0"
}
}
}
},
"dayjs": {
"version": "1.8.34",
"resolved": "https://registry.npmjs.org/dayjs/-/dayjs-1.8.34.tgz",
@ -20400,6 +20491,11 @@
}
}
},
"human-interval": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/human-interval/-/human-interval-1.0.0.tgz",
"integrity": "sha512-SWPw3rD6/ocA0JnGePoXp5Zf5eILzsoL5vdWdLwtTuyrElyCpfQb0whIcxMdK/gAKNl2rFDGkPAbwI2KGZCvNA=="
},
"husky": {
"version": "1.2.0",
"resolved": "https://registry.npmjs.org/husky/-/husky-1.2.0.tgz",

@ -62,6 +62,7 @@
"@storybook/addon-viewport": "^5.3.19",
"@storybook/addons": "^5.3.19",
"@storybook/react": "^5.3.19",
"@types/agenda": "^2.0.9",
"@types/bcrypt": "^3.0.0",
"@types/chai": "^4.2.12",
"@types/chai-spies": "^1.0.1",
@ -133,7 +134,7 @@
"@nivo/heatmap": "^0.61.0",
"@nivo/line": "^0.61.1",
"@nivo/pie": "^0.61.1",
"@rocket.chat/apps-engine": "1.19.0-alpha.3984",
"@rocket.chat/apps-engine": "1.19.0-alpha.4006",
"@rocket.chat/css-in-js": "^0.17.0",
"@rocket.chat/fuselage": "^0.17.0",
"@rocket.chat/fuselage-hooks": "^0.17.0",
@ -151,6 +152,7 @@
"@types/xml-crypto": "^1.4.1",
"@types/xmldom": "^0.1.30",
"adm-zip": "RocketChat/adm-zip",
"agenda": "^3.1.0",
"apn": "2.2.0",
"archiver": "^3.1.1",
"arraybuffer-to-string": "^1.0.2",

Loading…
Cancel
Save