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 #1469

Merged
merged 4 commits into from
Oct 20, 2017
Merged
Show file tree
Hide file tree
Changes from 3 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
16 changes: 16 additions & 0 deletions docs/source/en/basics/objects.md
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,22 @@ We can get it via `ctx.coreLogger`, the difference between the Context Logger is

We can get them via `this.logger` in Controller and Service instance, they are essentially a Context Logger, but additional file path will be added to logs, easy to locate the log print location.

## Subscription

Subscription is the model for subscribing, including consumer in message broker or schedule.

The base class of Subscription is exported by egg.

```js
const Subscription = require('egg').Subscription;
class Schedule extends Subscription {
// This method should be implemented
* subscribe() {}
Copy link
Member

Choose a reason for hiding this comment

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

这里可以是 generator / promise / async 吧?

Copy link
Member Author

Choose a reason for hiding this comment

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

是的

Copy link
Member

Choose a reason for hiding this comment

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

嗯,所以文档要注明下?

Copy link
Member

Choose a reason for hiding this comment

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

subscribe could be generator / async function

}
```

[Schedule](./schedule.md) is implemented using the model,also recommend the plugin for message broker.

[Koa]: http://koajs.com
[Koa.Application]: http://koajs.com/#application
[Koa.Context]: http://koajs.com/#context
Expand Down
16 changes: 16 additions & 0 deletions docs/source/zh-cn/basics/objects.md
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,22 @@ module.exports = {

我们可以在 Controller 和 Service 实例上通过 `this.logger` 获取到它们,它们本质上就是一个 Context Logger,不过在打印日志的时候还会额外的加上文件路径,方便定位日志的打印位置。

## Subscription

Subscription 是一种订阅模型,消息中间件的消费者或调度任务都属于这种模式。
Copy link
Member

@atian25 atian25 Oct 20, 2017

Choose a reason for hiding this comment

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

订阅模型是一种比较常见的开发模式,譬如消息中间件的消费者或调度任务。因此我们提供了 Subscription 基类来规范化这个模式。

使用方式如下:
...
...
...

开发者可以根据自己的需求,基于它定制特定的订阅规范,如 [定时任务](./schedule.md) 就是使用这种模式实现的,建议开发者可以参考来实现自己的消息中间件。


可以通过以下方式来引用 Subscription 基类:

```js
const Subscription = require('egg').Subscription;
class Schedule extends Subscription {
// 是需要实现此方法
* subscribe() {}
}
```

[定时任务](./schedule.md)使用这种模式实现,也建议使用此模型实现消息中间件。

[Koa]: http://koajs.com
[Koa.Application]: http://koajs.com/#application
[Koa.Context]: http://koajs.com/#context
Expand Down
8 changes: 7 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,13 @@ exports.Controller = require('./lib/core/base_context_class');
exports.Service = require('./lib/core/base_context_class');

/**
* @member {Service} Egg#BaseContextClass
* @member {Subscription} Egg#Subscription
* @since 1.10.0
*/
exports.Subscription = require('./lib/core/base_context_class');

/**
* @member {BaseContextClass} Egg#BaseContextClass
* @since 1.2.0
*/
exports.BaseContextClass = require('./lib/core/base_context_class');
1 change: 1 addition & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ describe('test/index.test.js', () => {
'BaseContextClass',
'Controller',
'Service',
'Subscription',
'startCluster',
]);
});
Expand Down
2 changes: 2 additions & 0 deletions test/lib/egg.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,8 @@ describe('test/lib/egg.test.js', () => {
.expect('hello')
.expect(200);

yield sleep(1000);

const logPath = path.join(utils.getFilepath('apps/base-context-class'), 'logs/base-context-class/base-context-class-web.log');
const log = fs.readFileSync(logPath, 'utf8');
assert(log.match(/INFO .*? \[service\.home\] appname: base-context-class/));
Expand Down