diff --git a/app.js b/app.js index ab46997..abd9be0 100644 --- a/app.js +++ b/app.js @@ -30,6 +30,7 @@ module.exports = app => { method: 'SCHEDULE', url: `/__schedule?path=${schedulePath}&${qs.stringify(schedule.schedule)}`, }); + return schedule.task(ctx); }; diff --git a/lib/load_schedule.js b/lib/load_schedule.js index 970af0b..1b4d7a3 100644 --- a/lib/load_schedule.js +++ b/lib/load_schedule.js @@ -26,10 +26,21 @@ function getScheduleLoader(app) { const schedule = item.exports; const fullpath = item.fullpath; assert(schedule.schedule, `schedule(${fullpath}): must have schedule and task properties`); - assert(is.function(schedule.task), `schedule(${fullpath}: task must be function`); + assert(is.class(schedule) || is.function(schedule.task), `schedule(${fullpath}: schedule.task should be function or schedule should be class`); + + let task; + if (is.class(schedule)) { + task = co.wrap(function* (ctx) { + const s = new schedule(ctx); + yield s.subscribe(); + }); + } else { + task = co.wrap(schedule.task); + } + target[fullpath] = { schedule: schedule.schedule, - task: co.wrap(schedule.task), + task, key: fullpath, }; } diff --git a/package.json b/package.json index aa409ca..4089298 100644 --- a/package.json +++ b/package.json @@ -24,13 +24,13 @@ ], "dependencies": { "co": "^4.6.0", - "cron-parser": "^2.4.2", + "cron-parser": "^2.4.3", "humanize-ms": "^1.2.1", "is-type-of": "^1.2.0", "safe-timers": "^1.0.1" }, "devDependencies": { - "autod": "^2.9.0", + "autod": "^2.10.1", "egg": "^1.9.0", "egg-bin": "^4.3.5", "egg-ci": "^1.8.0", diff --git a/test/fixtures/subscription/app/schedule/interval.js b/test/fixtures/subscription/app/schedule/interval.js new file mode 100644 index 0000000..e25e219 --- /dev/null +++ b/test/fixtures/subscription/app/schedule/interval.js @@ -0,0 +1,18 @@ +'use strict'; + +const Subscription = require('egg').BaseContextClass; + +class Interval extends Subscription { + static get schedule() { + return { + type: 'worker', + interval: 4000, + }; + } + + * subscribe() { + this.ctx.logger.info('interval'); + } +} + +module.exports = Interval; diff --git a/test/fixtures/subscription/app/schedule/sub/cron.js b/test/fixtures/subscription/app/schedule/sub/cron.js new file mode 100644 index 0000000..7ad9017 --- /dev/null +++ b/test/fixtures/subscription/app/schedule/sub/cron.js @@ -0,0 +1,18 @@ +'use strict'; + +const Subscription = require('egg').BaseContextClass; + +class Interval extends Subscription { + static get schedule() { + return { + type: 'worker', + cron: '*/5 * * * * *', + }; + } + + * subscribe() { + this.ctx.logger.info('cron'); + } +} + +module.exports = Interval; diff --git a/test/fixtures/subscription/config/plugin.js b/test/fixtures/subscription/config/plugin.js new file mode 100644 index 0000000..e54d182 --- /dev/null +++ b/test/fixtures/subscription/config/plugin.js @@ -0,0 +1,3 @@ +'use strict'; + +exports.logrotator = true; diff --git a/test/fixtures/subscription/package.json b/test/fixtures/subscription/package.json new file mode 100644 index 0000000..c502936 --- /dev/null +++ b/test/fixtures/subscription/package.json @@ -0,0 +1,3 @@ +{ + "name": "subscription" +} diff --git a/test/schedule.test.js b/test/schedule.test.js index a6ff58c..45fef53 100644 --- a/test/schedule.test.js +++ b/test/schedule.test.js @@ -269,6 +269,19 @@ describe('test/schedule.test.js', () => { assert(contains(agentLog, 'reschedule') >= 4); }); }); + + describe('Subscription', () => { + it('should support interval and cron', function* () { + app = mm.cluster({ baseDir: 'subscription', workers: 2, cache: false }); + // app.debug(); + yield app.ready(); + yield sleep(5000); + const log = getLogContent('subscription'); + // console.log(log); + assert(contains(log, 'interval') === 1); + assert(contains(log, 'cron') === 1); + }); + }); }); function sleep(time) {