Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add Subscription base class on app instance #3058

Merged
merged 3 commits into from
Oct 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

说个题外的,我倒是挺想加个 app.Schedule 的,而不是让定时任务直接继承基类 Subscription

不过插件里面好像没法挂。

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

为什么还要多一个 Schedule 呢?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Schedule 就像 Controller 这样的基类。
Subscription 不是 Schedule 专用的。

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