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

refactor: use FileLoader to load schedule files #7

Merged
merged 1 commit into from
Aug 18, 2016
Merged
Show file tree
Hide file tree
Changes from all 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
59 changes: 27 additions & 32 deletions lib/load_schedule.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,38 @@
'use strict';

const fs = require('fs');
const read = require('fs-readdir-recursive');
const path = require('path');
const assert = require('assert');
const is = require('is-type-of');
const co = require('co');

module.exports = app => {
const dirs = app.loader.getLoadUnits().map(unit => unit.path);

const dirs = app.loader.getLoadUnits().map(unit => path.join(unit.path, 'app/schedule'));
const Loader = getScheduleLoader(app);
const schedules = {};
for (const dir of dirs) {
const schedulePath = path.join(dir, 'app/schedule');
if (!fs.existsSync(schedulePath)) {
continue;
}
if (!fs.lstatSync(schedulePath).isDirectory()) {
continue;
}

read(schedulePath).forEach(s => {
s = path.join(schedulePath, s);
s = require.resolve(s);
let schedule = require(s);

// support dynamic schedule
if (typeof schedule === 'function') {
schedule = schedule(app);
}
assert(schedule.schedule, `schedule(${s}): must have schedule and task properties`);
assert(is.generatorFunction(schedule.task), `schedule(${s}: task must be generator function`);

schedules[s] = {
schedule: schedule.schedule,
task: co.wrap(schedule.task),
key: `egg-schedule:${s}`,
};
});
}
new Loader({
directory: dirs,
target: schedules,
inject: app,
}).load();
return schedules;
};

function getScheduleLoader(app) {
return class ScheduleLoader extends app.loader.FileLoader {
load() {
const target = this.options.target;
const items = this.parse();
for (const item of items) {
const schedule = item.exports;
const fullpath = item.fullpath;
assert(schedule.schedule, `schedule(${fullpath}): must have schedule and task properties`);
assert(is.generatorFunction(schedule.task), `schedule(${fullpath}: task must be generator function`);
target[fullpath] = {
schedule: schedule.schedule,
task: co.wrap(schedule.task),
key: `egg-schedule:${fullpath}`,
};
}
}
};
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
"dependencies": {
"co": "^4.6.0",
"cron-parser": "^2.1.0",
"fs-readdir-recursive": "^1.0.0",
"humanize-ms": "^1.2.0",
"is-type-of": "^1.0.0"
},
Expand Down Expand Up @@ -54,3 +53,4 @@
},
"author": "dead_horse"
}

2 changes: 1 addition & 1 deletion test/fixtures/worker/config/plugin.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
'use strict';

exports.logrotater = true;
exports.logrotator = true;
Copy link
Member

Choose a reason for hiding this comment

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

这个插件是不是不用开了?下面那个用例自己造一个node_modules目录写个假任务。

不然耦合另一个插件来测试在这里不是很必要吧。

Copy link
Member Author

Choose a reason for hiding this comment

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

原来就写着,问问 @dead-horse

Copy link
Member

Choose a reason for hiding this comment

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

应该是可以干掉的, 后面再提个 PR 吧, 这个先合

4 changes: 2 additions & 2 deletions test/schedule.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,8 @@ describe('test/schedule.test.js', () => {
it('should run schedule by absolute package path success', function* () {
const app = mm.app({ baseDir: 'worker', cache: false });
yield app.ready();
console.log(require.resolve('egg/node_modules/egg-logrotater/app/schedule/rotateByFile.js'));
yield app.runSchedule(require.resolve('egg/node_modules/egg-logrotater/app/schedule/rotateByFile.js'));
console.log(require.resolve('egg/node_modules/egg-logrotator/app/schedule/rotate_by_file.js'));
yield app.runSchedule(require.resolve('egg/node_modules/egg-logrotator/app/schedule/rotate_by_file.js'));
app.close();
});
});
Expand Down