Skip to content

Commit

Permalink
refactor: use FileLoader to load schedule files
Browse files Browse the repository at this point in the history
  • Loading branch information
popomore committed Aug 18, 2016
1 parent a139f00 commit 0dc6f1a
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 36 deletions.
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;
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

0 comments on commit 0dc6f1a

Please sign in to comment.