Skip to content

Commit

Permalink
feat: add Subscription base class on app instance (#3058)
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 authored and dead-horse committed Oct 8, 2018
1 parent 4c6fb2a commit 886d9ad
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 2 deletions.
7 changes: 7 additions & 0 deletions lib/egg.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
17 changes: 17 additions & 0 deletions test/fixtures/apps/schedule/app/schedule/hello.js
Original file line number Diff line number Diff line change
@@ -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');
}
}
};
2 changes: 1 addition & 1 deletion test/fixtures/apps/schedule/app/schedule/sub/cron.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ exports.schedule = {
};

exports.task = function* (ctx) {
ctx.logger.warn('cron');
ctx.logger.warn('cron wow');
};
5 changes: 4 additions & 1 deletion test/lib/plugins/schedule.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});

Expand Down

0 comments on commit 886d9ad

Please sign in to comment.