From 47609366d4b79dca8da6380d30d0dcbea5234057 Mon Sep 17 00:00:00 2001 From: popomore Date: Wed, 27 Sep 2017 14:31:17 +0800 Subject: [PATCH 1/4] feat: add Subscription Ref https://github.com/eggjs/egg/issues/1468 --- index.js | 8 +++++++- test/index.test.js | 1 + 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 32fdbac8da..651c446343 100644 --- a/index.js +++ b/index.js @@ -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'); diff --git a/test/index.test.js b/test/index.test.js index 44a3de9470..1a76149d09 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -13,6 +13,7 @@ describe('test/index.test.js', () => { 'BaseContextClass', 'Controller', 'Service', + 'Subscription', 'startCluster', ]); }); From 13324b749b117c8ec6564418304278a1bc3d982f Mon Sep 17 00:00:00 2001 From: popomore Date: Thu, 19 Oct 2017 14:14:08 +0800 Subject: [PATCH 2/4] f --- test/lib/egg.test.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/lib/egg.test.js b/test/lib/egg.test.js index 6fecf52206..a7146c4166 100644 --- a/test/lib/egg.test.js +++ b/test/lib/egg.test.js @@ -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/)); From a3f6407d70345fbe12c84b0bad454f01966c7b6c Mon Sep 17 00:00:00 2001 From: popomore Date: Thu, 19 Oct 2017 15:54:47 +0800 Subject: [PATCH 3/4] f --- docs/source/en/basics/objects.md | 16 ++++++++++++++++ docs/source/zh-cn/basics/objects.md | 16 ++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/docs/source/en/basics/objects.md b/docs/source/en/basics/objects.md index bbe71048e6..6d326d12e4 100644 --- a/docs/source/en/basics/objects.md +++ b/docs/source/en/basics/objects.md @@ -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() {} +} +``` + +[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 diff --git a/docs/source/zh-cn/basics/objects.md b/docs/source/zh-cn/basics/objects.md index eec9fcb3a9..023a973ca2 100644 --- a/docs/source/zh-cn/basics/objects.md +++ b/docs/source/zh-cn/basics/objects.md @@ -270,6 +270,22 @@ module.exports = { 我们可以在 Controller 和 Service 实例上通过 `this.logger` 获取到它们,它们本质上就是一个 Context Logger,不过在打印日志的时候还会额外的加上文件路径,方便定位日志的打印位置。 +## Subscription + +Subscription 是一种订阅模型,消息中间件的消费者或调度任务都属于这种模式。 + +可以通过以下方式来引用 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 From 9878128c1912cfbcbb29b71a1928269695401bf5 Mon Sep 17 00:00:00 2001 From: popomore Date: Fri, 20 Oct 2017 16:35:48 +0800 Subject: [PATCH 4/4] f --- docs/source/en/basics/objects.md | 4 +++- docs/source/zh-cn/basics/objects.md | 8 +++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/docs/source/en/basics/objects.md b/docs/source/en/basics/objects.md index 6d326d12e4..23c0361253 100644 --- a/docs/source/en/basics/objects.md +++ b/docs/source/en/basics/objects.md @@ -278,13 +278,15 @@ 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 can be generator function or async function * subscribe() {} } ``` -[Schedule](./schedule.md) is implemented using the model,also recommend the plugin for message broker. +We recommend plugin developers to implement based on this model, For example, [Schedule](./schedule.md). [Koa]: http://koajs.com [Koa.Application]: http://koajs.com/#application diff --git a/docs/source/zh-cn/basics/objects.md b/docs/source/zh-cn/basics/objects.md index 023a973ca2..65598ff0e8 100644 --- a/docs/source/zh-cn/basics/objects.md +++ b/docs/source/zh-cn/basics/objects.md @@ -272,19 +272,21 @@ module.exports = { ## Subscription -Subscription 是一种订阅模型,消息中间件的消费者或调度任务都属于这种模式。 +订阅模型是一种比较常见的开发模式,譬如消息中间件的消费者或调度任务。因此我们提供了 Subscription 基类来规范化这个模式。 可以通过以下方式来引用 Subscription 基类: ```js const Subscription = require('egg').Subscription; + class Schedule extends Subscription { - // 是需要实现此方法 + // 需要实现此方法 + // subscribe 可以为 generator function 或 async function * subscribe() {} } ``` -[定时任务](./schedule.md)使用这种模式实现,也建议使用此模型实现消息中间件。 +插件开发者可以根据自己的需求基于它定制订阅规范,如[定时任务](./schedule.md)就是使用这种规范实现的。 [Koa]: http://koajs.com [Koa.Application]: http://koajs.com/#application