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..4da550842d --- /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/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 3afc773472..9d6763df3a 100644 --- a/test/lib/plugins/schedule.test.js +++ b/test/lib/plugins/schedule.test.js @@ -17,9 +17,12 @@ 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); + + // should support Subscription class on app.Subscription + assert(contains(log, 'Info about your task') === 1); }); });