Skip to content

Commit

Permalink
fix: schedule case (#660)
Browse files Browse the repository at this point in the history
  • Loading branch information
czy88840616 committed Oct 5, 2020
1 parent 6dc0af3 commit c9fb3fb
Show file tree
Hide file tree
Showing 18 changed files with 99 additions and 59 deletions.
8 changes: 8 additions & 0 deletions packages/midway-schedule/jest.config.js
@@ -0,0 +1,8 @@
const path = require('path');

module.exports = {
preset: 'ts-jest',
testPathIgnorePatterns: ['<rootDir>/test/fixtures'],
coveragePathIgnorePatterns: ['<rootDir>/test/'],
setupFilesAfterEnv: [path.join(__dirname, 'test/.setup.js')],
};
4 changes: 4 additions & 0 deletions packages/midway-schedule/test/.setup.js
@@ -0,0 +1,4 @@
const path = require('path');
process.env.MIDWAY_TS_MODE = 'true';
process.env.MIDWAY_EGG_PLUGIN_PATH = path.join(__dirname, '../../../');
jest.setTimeout(30000);
3 changes: 0 additions & 3 deletions packages/midway-schedule/test/.setup.ts

This file was deleted.

@@ -0,0 +1,6 @@
{
"name": "worker-other",
"scripts": {
"build": "midway-bin build -c"
}
}
@@ -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,
};
@@ -0,0 +1 @@
exports.logrotator = true;
@@ -0,0 +1,12 @@
import { Provide, Schedule, CommonSchedule } from '@midwayjs/decorator';

@Provide()
@Schedule({
type: 'worker',
interval: 1000,
})
export default class IntervalCron implements CommonSchedule {
async exec(ctx) {
ctx.logger.info(process.pid, 'hello decorator');
}
}
42 changes: 11 additions & 31 deletions packages/midway-schedule/test/schedule.test.ts
@@ -1,71 +1,51 @@
'use strict';

import { clearAllModule } from '@midwayjs/decorator';
import { mm } from 'midway-mock';

import { closeApp, create } from './utils';
import * as path from 'path';
const fs = require('fs');
const assert = require('assert');

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

describe('schedule type worker', () => {
it('should load schedules', async () => {
application = mm.app({
baseDir: 'app-load-schedule',
typescript: true,
});
await application.ready();
const application: any = await create('app-load-schedule', {},);
const list = Object.keys(application.schedules).filter((key) =>
key.includes('HelloCron'),
);
assert(list.length === 1);
const item = application.schedules[list[0]];
assert.deepEqual(item.schedule, {type: 'worker', interval: 2333});
await closeApp(application);
});

it('should support interval with @schedule decorator (both app/schedule & lib/schedule)', async () => {
const name = 'worker';
application = mm.cluster({
baseDir: name,
typescript: true,
worker: 2,
});
await application.ready();
const application = await create('worker', {});
await sleep(5000);
const log = getLogContent(name);
assert(contains(log, 'hello decorator') === 4, '未正确执行 4 次');
await closeApp(application);
});

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

describe('app.runSchedule', () => {
it('should run schedule not exist throw error', async () => {
application = mm.app({ baseDir: 'worker', typescript: true, });
await application.ready();
const application = await create('worker-other', {});
await application.runSchedule('intervalCron#IntervalCron');
await sleep(1000);
const log = getLogContent('worker');
const log = getLogContent('worker-other');
// console.log(log);
assert(contains(log, 'hello decorator') === 1);
expect(contains(log, 'hello decorator')).toEqual(1);
await closeApp(application);
});
});
});
Expand Down
12 changes: 12 additions & 0 deletions packages/midway-schedule/test/utils.ts
@@ -0,0 +1,12 @@
import { IMidwayWebConfigurationOptions, Framework } from '../../web/src';
import { join } from 'path';
import { createApp, close } from '@midwayjs/mock';

export async function create(name, options: IMidwayWebConfigurationOptions = {}) {
const baseDir = join(__dirname, 'fixtures', name);
return createApp<Framework>(baseDir, options, Framework)
}

export async function closeApp(app) {
await close(app);
}
3 changes: 3 additions & 0 deletions packages/mock/src/utils.ts
Expand Up @@ -54,6 +54,9 @@ export async function create<
if (DefaultFramework) {
framework = new DefaultFramework();
if (framework.getFrameworkType() === MidwayFrameworkType.WEB) {
// clean first
await remove(join(baseDir, 'logs'));
await remove(join(baseDir, 'run'));
// add egg-mock plugin for @midwayjs/web test, provide mock method
options = Object.assign(options || {}, {
plugins: {
Expand Down
10 changes: 4 additions & 6 deletions packages/web/src/agent.ts → packages/web/agent.js
@@ -1,11 +1,9 @@
import { BootstrapStarter } from '@midwayjs/bootstrap';
import { Framework } from './index';
'use strict';

class AppBootHook {
app;
bootstrap;
framework;
const { BootstrapStarter } = require('@midwayjs/bootstrap');
const { Framework } = require('./dist/index');

class AppBootHook {
constructor(app) {
this.app = app;
}
Expand Down
12 changes: 5 additions & 7 deletions packages/web/src/app.ts → packages/web/app.js
@@ -1,14 +1,12 @@
import { Bootstrap } from '@midwayjs/bootstrap';
import { Framework } from './index';
'use strict';

class AppBootHook {
app;
bootstrap;
framework;
appMiddleware = [];
const { Bootstrap } = require('@midwayjs/bootstrap');
const { Framework } = require('./dist/index');

class AppBootHook {
constructor(app) {
this.app = app;
this.appMiddleware = [];
}

configDidLoad() {
Expand Down
@@ -1,3 +1,4 @@
'use strict';
const rc = Symbol('Context#RequestContext');
const { MidwayRequestContainer } = require('@midwayjs/core');

Expand Down
@@ -1,12 +1,12 @@
'use strict';

const path = require('path');
const mkdirp = require('mkdirp');
const os = require('os');
const fs = require('fs');

import { EggAppConfig, EggAppInfo, PowerPartial } from 'egg';

module.exports = (appInfo: EggAppInfo) => {
const exports = {} as PowerPartial<EggAppConfig>;
module.exports = appInfo => {
const exports = {};

exports.rundir = path.join(appInfo.appDir, 'run');

Expand Down
11 changes: 11 additions & 0 deletions packages/web/config/plugin.js
@@ -0,0 +1,11 @@
'use strict';

module.exports = {
static: false,
development: false,
watcher: false,
schedulePlus: {
enable: true,
package: 'midway-schedule',
},
};
1 change: 1 addition & 0 deletions packages/web/package.json
Expand Up @@ -26,6 +26,7 @@
"@midwayjs/cli": "^1.0.0",
"@midwayjs/mock": "^2.3.7",
"egg-view-nunjucks": "^2.2.0",
"midway-schedule": "^2.3.6",
"fs-extra": "^8.0.1",
"pedding": "^1.1.0",
"react": "^16.13.1",
Expand Down
8 changes: 5 additions & 3 deletions packages/web/src/application.ts
Expand Up @@ -4,6 +4,7 @@ import { parseNormalDir } from './utils';
import * as extend from 'extend2';
import { EggAppInfo } from 'egg';
import { IMidwayWebApplication } from './interface';
import { join } from 'path';

const {
AppWorkerLoader,
Expand Down Expand Up @@ -42,7 +43,8 @@ export const createAppWorkerLoader = AppWorkerLoader => {
}

if (process.env.MIDWAY_EGG_PLUGIN_PATH) {
return super.getEggPaths().concat(process.env.MIDWAY_EGG_PLUGIN_PATH);
const result = super.getEggPaths();
return result.concat(process.env.MIDWAY_EGG_PLUGIN_PATH);
}
return super.getEggPaths();
}
Expand Down Expand Up @@ -215,7 +217,7 @@ export class EggApplication extends BaseEggApplication {
}

get [EGG_PATH]() {
return __dirname;
return join(__dirname, '../');
}
}

Expand All @@ -225,7 +227,7 @@ export class EggAgent extends BaseEggAgent {
}

get [EGG_PATH]() {
return __dirname;
return join(__dirname, '../');
}
}

Expand Down
5 changes: 0 additions & 5 deletions packages/web/src/config/plugin.ts

This file was deleted.

0 comments on commit c9fb3fb

Please sign in to comment.