From 0dc6f1ae8ff02920b35402c7e69a4ff4c5341231 Mon Sep 17 00:00:00 2001 From: popomore Date: Wed, 17 Aug 2016 22:34:35 +0800 Subject: [PATCH] refactor: use FileLoader to load schedule files Closes eggjs/egg#48 --- lib/load_schedule.js | 59 ++++++++++++--------------- package.json | 2 +- test/fixtures/worker/config/plugin.js | 2 +- test/schedule.test.js | 4 +- 4 files changed, 31 insertions(+), 36 deletions(-) diff --git a/lib/load_schedule.js b/lib/load_schedule.js index bbbf625..989ee3c 100644 --- a/lib/load_schedule.js +++ b/lib/load_schedule.js @@ -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}`, + }; + } + } + }; +} diff --git a/package.json b/package.json index 086fc9d..02130ed 100644 --- a/package.json +++ b/package.json @@ -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" }, @@ -54,3 +53,4 @@ }, "author": "dead_horse" } + diff --git a/test/fixtures/worker/config/plugin.js b/test/fixtures/worker/config/plugin.js index 5a2251c..e54d182 100644 --- a/test/fixtures/worker/config/plugin.js +++ b/test/fixtures/worker/config/plugin.js @@ -1,3 +1,3 @@ 'use strict'; -exports.logrotater = true; +exports.logrotator = true; diff --git a/test/schedule.test.js b/test/schedule.test.js index a7a6e6b..71e2360 100644 --- a/test/schedule.test.js +++ b/test/schedule.test.js @@ -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(); }); });