Skip to content

Commit

Permalink
feat: support non-default class for midway-schedule
Browse files Browse the repository at this point in the history
  • Loading branch information
Lellansin committed Nov 29, 2018
1 parent 57e540a commit 74e51e9
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 18 deletions.
4 changes: 3 additions & 1 deletion docs/guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -319,14 +319,16 @@ import { schedule } from 'midway';
interval: 2333, // 2.333s 间隔
type: 'worker', // 指定某一个 worker 执行
})
export default class HelloCron {
export class HelloCron {
// 定时执行的具体任务
async exec(ctx) {
ctx.logger.info(process.pid, 'hello');
}
}
```

PS: 定时任务类需 `export` 导出才会被加载,并且一个 `.ts` 文件可以 `export` 多个定时任务类,但是如果 `export default` 了,则只会读取 `default` 的类。

### 注入日志对象

在原有逻辑中,日志对象也都挂载在 app.loggers 中,通过在 config 中配置的 key 来生成不同的日志实例对象,比如插件的日志,链路的日志等。
Expand Down
40 changes: 24 additions & 16 deletions packages/midway-schedule/lib/load_schedule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,30 @@ function getScheduleLoader(app) {

load() {
const target = this.options.target;
const items = this.parse();
for (const item of items) {
const schedule = item.exports;
const fullpath = item.fullpath;
const files = this.parse();
for (const file of files) {
const item = file.exports;
const fullpath = file.fullpath;
if (is.class(item)) {
collectTask(item, 'default', fullpath);
continue;
}
Object.keys(item)
.filter((key) => !key.startsWith('__'))
.filter((key) => is.class(item[key]))
.map((key) => collectTask(item[key], key, fullpath));
}
return;

assert(is.class(schedule), `schedule(${fullpath}: should be class`);
function collectTask(cls, name, fullpath) {
const key = fullpath + '#' + name;
const opts: SchedueOpts | string = Reflect.getMetadata(
SCHEDULE_CLASS,
schedule,
cls,
);
assert(opts, `schedule(${fullpath}): must use @schedule to setup.`);

assert(opts, `schedule(${key}): must use @schedule to setup.`);
const task = async (ctx, data) => {
const ins = await ctx.requestContext.getAsync(schedule);
// throw new Error('emmmmm ' + ins);
const ins = await ctx.requestContext.getAsync(cls);
ins.exec = app.toAsyncFunction(ins.exec);
return ins.exec(ctx, data);
};
Expand All @@ -54,17 +63,16 @@ function getScheduleLoader(app) {
const envList = (opts as SchedueOpts).env;
if (is.array(envList) && !envList.includes(env)) {
app.coreLogger.info(
`[egg-schedule]: ignore schedule ${fullpath} due to \`schedule.env\` not match`,
`[egg-schedule]: ignore schedule ${key} due to \`schedule.env\` not match`,
);
continue;
return;
}

target[fullpath] = {
target[key] = {
schedule: opts,
task,
key: fullpath,
key,
};
}
}
};
}
}
2 changes: 1 addition & 1 deletion packages/midway-schedule/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"scripts": {
"build": "npm run lint && midway-bin build -c",
"lint": "../../node_modules/.bin/tslint --format prose -c ../../tslint.json src/**/*.ts test/**/*.ts",
"test": "npm run lint && midway-bin clean && NODE_ENV=test midway-bin test --ts"
"test": "npm run lint && midway-bin clean && NODE_ENV=unittest midway-bin test --ts"
},
"keywords": [],
"author": "",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "worker-non-default-class",
"scripts": {
"build": "midway-bin build -c"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use strict';

import { schedule } from '../../../../../../../midway';

@schedule({
type: 'worker',
interval: 1000,
})
export class IntervalCron {
async exec(ctx) {
ctx.logger.info(process.pid, 'hello decorator');
}
}

@schedule({
type: 'worker',
interval: 1000,
})
export class NonDefCron {
async exec(ctx) {
ctx.logger.info(process.pid, 'hello other functions');
}
}
13 changes: 13 additions & 0 deletions packages/midway-schedule/test/schedule.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,19 @@ describe('test/schedule.test.ts', () => {
const log = getLogContent(name);
assert(contains(log, 'hello decorator') === 4, '未正确执行 4 次');
});

it('should support non-default class with @schedule decorator', async () => {
const name = 'worker-non-default-class';
application = cluster(name, {
typescript: true,
worker: 2,
});
await application.ready();
await sleep(5000);
const log = getLogContent(name);
assert(contains(log, 'hello decorator') === 4, '未正确执行 4 次');
assert(contains(log, 'hello other functions') === 4, '未正确执行 4 次');
});
});
});

Expand Down

0 comments on commit 74e51e9

Please sign in to comment.