Skip to content

Commit

Permalink
fix: run task with wront ctx & fill tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Lellansin authored and czy88840616 committed Nov 15, 2018
1 parent f16b9db commit 94d95c3
Show file tree
Hide file tree
Showing 12 changed files with 180 additions and 78 deletions.
9 changes: 5 additions & 4 deletions packages/midway-schedule/lib/load_schedule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default (app) => {
.getLoadUnits()
.map((unit) => require('path').join(unit.path, 'lib/schedule'));
const Loader = getScheduleLoader(app);
const schedules = app.schedules;
const schedules = app.schedules ? app.schedules : app.schedules = {};
new Loader({
directory: dirs,
target: schedules,
Expand Down Expand Up @@ -43,10 +43,11 @@ function getScheduleLoader(app) {
);
assert(opts, `schedule(${fullpath}): must use @schedule to setup.`);

const task = (ctx, data) => {
const ins = ctx.getAsync(schedule);
const task = async (ctx, data) => {
const ins = await ctx.requestContext.getAsync(schedule);
// throw new Error('emmmmm ' + ins);
ins.exec = app.toAsyncFunction(ins.exec);
return ins.exec(data);
return ins.exec(ctx, data);
};

const env = app.config.env;
Expand Down
7 changes: 1 addition & 6 deletions packages/midway-schedule/lib/schedule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,10 @@ module.exports = class Schedule extends BaseSchedule {
* @param {Strategy} clz - Strategy class
*/
use(type, clz) {
super.use(type, clz);
this[M_STRATEGY].set(type, clz);
}

init() {
// load egg-schedule (inside 'app/schedule')
super.init();
// load midway-schedule (inside 'lib/schedule')
this.loadSchedule();
}
Expand All @@ -51,9 +48,7 @@ module.exports = class Schedule extends BaseSchedule {
}

start() {
// start egg-schedule
super.start();
// start midway-schedule
this.closed = false;
for (const instance of this[M_STRATEGY_INSTANCE].values()) {
instance.start();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "app-load-schedule"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict';

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

@schedule({
type: 'worker',
interval: 2333,
})
export default class HelloCron {
async exec(ctx) {
ctx.logger.info(process.pid, 'hello');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
let i = 1;

exports.task = async (ctx) => {
console.log('hasdf');
ctx.logger.info(process.pid, 'hehehehe', i++);
};

exports.schedule = {
type: 'worker',
interval: 1000,
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "egg-schedule"
}
5 changes: 4 additions & 1 deletion packages/midway-schedule/test/fixtures/worker/package.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
{
"name": "worker"
"name": "worker",
"scripts": {
"build": "midway-bin build -c"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
let i = 1;

exports.task = async (ctx) => {
console.log('hasdf');
ctx.logger.info(process.pid, 'hehehehe', i++);
};

exports.schedule = {
type: 'worker',
interval: 1000,
};
Original file line number Diff line number Diff line change
@@ -1,21 +1,13 @@
'use strict';

import { schedule } from '../../../../../../../midway/dist';
// import * as fs from 'fs';
import { schedule } from '../../../../../../../midway';

@schedule({
type: 'worker',
interval: 200,
interval: 1000,
})
export default class IntervalCron {
async exec(ctx) {
throw new Error('hahaha');
// console.log(1234);
// ctx.logger.info('interval');
// ctx.app.coreLogger.info('hello world');
// fs.writeFileSync(
// require('path').join(__dirname, '../../logs/worker/hello.log'),
// 'hello world',
// );
ctx.logger.info(process.pid, 'interval');
}
}
140 changes: 84 additions & 56 deletions packages/midway-schedule/test/schedule.test.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,53 @@
'use strict';

const { mm } = require('../../midway-mock/dist');
// const { mm } = require('../../midway-mock/dist');
const path = require('path');
const fs = require('fs');
const assert = require('assert');

describe('test/schedule.test.js', () => {
let app;
// afterEach(() => app.close());
import { app, cluster } from './utils';

describe('test/schedule.test.ts', () => {
let application;
afterEach(() => application.close());

describe('schedule type worker', () => {
it('should support interval and cron', async () => {
app = mm.app({
baseDir: 'worker',
framework: path.join(__dirname, '../../midway'),
it('should load schedules', async () => {
const name = 'app-load-schedule';
application = app(name, {
typescript: true,
});
await application.ready();
const list = Object.keys(application.schedules).filter((key) =>
key.includes('midway-schedule/test/fixtures/' + name),
);
assert(list.length === 1);
const item = application.schedules[list[0]];
assert.deepEqual(item.schedule, { type: 'worker', interval: 2333 });
});

it('should be compatible with egg-schedule', async () => {
const name = 'egg-schedule';
application = app(name, {});
await application.ready();
const list = Object.keys(application.schedules).filter((key) =>
key.includes('midway-schedule/test/fixtures/' + name),
);
assert(list.length === 1);
const item = application.schedules[list[0]];
assert.deepEqual(item.schedule, { type: 'worker', interval: 1000 });
});

it('should support interval with @schedule decorator', async () => {
const name = 'worker';
application = cluster(name, {
typescript: true,
worker: 2,
});
// app.debug();
await app.ready();
console.log(app.schedules);
await application.ready();
await sleep(5000);
const log = getLogContent(name);
assert(contains(log, 'interval') === 4, '未正确执行 4 次');
});
});
});
Expand All @@ -29,66 +58,65 @@ function sleep(time) {
});
}

function getCoreLogContent(name) {
const logPath = path.join(
__dirname,
'fixtures',
name,
'logs',
name,
'egg-web.log',
);
return fs.readFileSync(logPath, 'utf8');
}
// function getCoreLogContent(name) {
// const logPath = path.join(
// __dirname,
// 'fixtures',
// name,
// 'logs',
// name,
// 'egg-web.log',
// );
// return fs.readFileSync(logPath, 'utf8');
// }

function getLogContent(name) {
const logPath = path.join(
__dirname,
'fixtures',
name,
'dist',
'logs',
name,
`midway-web.log`,
);
return fs.readFileSync(logPath, 'utf8');
}

function getErrorLogContent(name) {
const logPath = path.join(
__dirname,
'fixtures',
name,
'logs',
name,
'common-error.log',
);
return fs.readFileSync(logPath, 'utf8');
}
// function getErrorLogContent(name) {
// const logPath = path.join(
// __dirname,
// 'fixtures',
// name,
// 'logs',
// name,
// 'common-error.log',
// );
// return fs.readFileSync(logPath, 'utf8');
// }

function getAgentLogContent(name) {
const logPath = path.join(
__dirname,
'fixtures',
name,
'logs',
name,
'egg-agent.log',
);
return fs.readFileSync(logPath, 'utf8');
}
// function getAgentLogContent(name) {
// const logPath = path.join(
// __dirname,
// 'fixtures',
// name,
// 'logs',
// name,
// 'egg-agent.log',
// );
// return fs.readFileSync(logPath, 'utf8');
// }

function getScheduleLogContent(name) {
const logPath = path.join(
__dirname,
'fixtures',
name,
'logs',
name,
'egg-schedule.log',
);
return fs.readFileSync(logPath, 'utf8');
}
// function getScheduleLogContent(name) {
// const logPath = path.join(
// __dirname,
// 'fixtures',
// name,
// 'logs',
// name,
// 'egg-schedule.log',
// );
// return fs.readFileSync(logPath, 'utf8');
// }

function contains(content, match) {
return content.split('\n').filter((line) => line.indexOf(match) >= 0).length;
Expand Down
42 changes: 42 additions & 0 deletions packages/midway-schedule/test/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
'use strict';

const fs = require('fs');
const path = require('path');
const mm = require('egg-mock');
const logDir = path.join(__dirname, '../logs');

process.setMaxListeners(0);

if (!fs.existsSync(logDir)) {
fs.mkdirSync(logDir);
}

export function app(name, options) {
options = formatOptions(name, options);
// mm.consoleLevel(options.consoleLevel || 'NONE');
const app = mm.app(options);
app.close = () => {
fs.rmdirSync(path.join(app.baseDir, 'run'));
return app.close;
};
return app;
}

export function cluster(name, options) {
options = formatOptions(name, options);
return mm.cluster(options);
}

export function formatOptions(name, options = {}) {
return Object.assign(
{},
{
baseDir: name,
framework: path.join(__dirname, '../../midway'),
// 默认关闭覆盖率,太慢
coverage: false,
cache: false,
},
options,
);
}

0 comments on commit 94d95c3

Please sign in to comment.