From 1e8cc84e3b014fa5f4edbfc6d84105703fb7318f Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Fri, 28 Sep 2018 21:44:10 +0800 Subject: [PATCH 1/3] feat: add Subscription base class on app instance --- lib/egg.js | 7 +++++++ .../apps/schedule/app/schedule/hello.js | 17 +++++++++++++++++ test/lib/plugins/schedule.test.js | 3 +++ 3 files changed, 27 insertions(+) create mode 100644 test/fixtures/apps/schedule/app/schedule/hello.js diff --git a/lib/egg.js b/lib/egg.js index 76aa1d5ce0..f10b70af06 100644 --- a/lib/egg.js +++ b/lib/egg.js @@ -142,6 +142,13 @@ class EggApplication extends EggCore { * @since 1.0.0 */ this.Service = BaseContextClass; + + /** + * Retreive base subscription + * @member {Subscription} Subscription + * @since 2.12.0 + */ + this.Subscription = BaseContextClass; } /** diff --git a/test/fixtures/apps/schedule/app/schedule/hello.js b/test/fixtures/apps/schedule/app/schedule/hello.js new file mode 100644 index 0000000000..18e165d244 --- /dev/null +++ b/test/fixtures/apps/schedule/app/schedule/hello.js @@ -0,0 +1,17 @@ +'use strict'; + +module.exports = app => { + return class LoggerExample extends app.Subscription { + static get schedule() { + return { + type: 'worker', + cron: '0 0 3 * * *', + immediate: true, + }; + } + + async subscribe() { + this.ctx.logger.info('Info about your task'); + } + } +}; diff --git a/test/lib/plugins/schedule.test.js b/test/lib/plugins/schedule.test.js index 3afc773472..1d6b46b75e 100644 --- a/test/lib/plugins/schedule.test.js +++ b/test/lib/plugins/schedule.test.js @@ -20,6 +20,9 @@ describe('test/lib/plugins/schedule.test.js', () => { const count = contains(log, 'cron'); assert(count >= 1); assert(count <= 2); + + // should support Subscription class on app.Subscription + assert(contains(log, 'Info about your task') === 1); }); }); From 5c8970a5221712331b4a890994357ec5565bdb79 Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Fri, 28 Sep 2018 21:45:44 +0800 Subject: [PATCH 2/3] f --- test/fixtures/apps/schedule/app/schedule/hello.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/test/fixtures/apps/schedule/app/schedule/hello.js b/test/fixtures/apps/schedule/app/schedule/hello.js index 18e165d244..4da550842d 100644 --- a/test/fixtures/apps/schedule/app/schedule/hello.js +++ b/test/fixtures/apps/schedule/app/schedule/hello.js @@ -3,12 +3,12 @@ module.exports = app => { return class LoggerExample extends app.Subscription { static get schedule() { - return { - type: 'worker', - cron: '0 0 3 * * *', - immediate: true, - }; - } + return { + type: 'worker', + cron: '0 0 3 * * *', + immediate: true, + }; + } async subscribe() { this.ctx.logger.info('Info about your task'); From 8ca951665377bfdc71aaa14055506c86fa68b20c Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Fri, 28 Sep 2018 23:38:23 +0800 Subject: [PATCH 3/3] f --- test/fixtures/apps/schedule/app/schedule/sub/cron.js | 2 +- test/lib/plugins/schedule.test.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/test/fixtures/apps/schedule/app/schedule/sub/cron.js b/test/fixtures/apps/schedule/app/schedule/sub/cron.js index 04f0b1e35b..2405eb9183 100644 --- a/test/fixtures/apps/schedule/app/schedule/sub/cron.js +++ b/test/fixtures/apps/schedule/app/schedule/sub/cron.js @@ -6,5 +6,5 @@ exports.schedule = { }; exports.task = function* (ctx) { - ctx.logger.warn('cron'); + ctx.logger.warn('cron wow'); }; diff --git a/test/lib/plugins/schedule.test.js b/test/lib/plugins/schedule.test.js index 1d6b46b75e..9d6763df3a 100644 --- a/test/lib/plugins/schedule.test.js +++ b/test/lib/plugins/schedule.test.js @@ -17,7 +17,7 @@ describe('test/lib/plugins/schedule.test.js', () => { await sleep(7000); await app.close(); const log = getLogContent('schedule'); - const count = contains(log, 'cron'); + const count = contains(log, 'cron wow'); assert(count >= 1); assert(count <= 2);