From 69a588e5ffbb5a01ed3084bfb9f6c2a792963db4 Mon Sep 17 00:00:00 2001 From: zhennann Date: Thu, 16 Nov 2017 10:26:05 +0800 Subject: [PATCH] feat: run a scheduler only once at startup (#33) --- lib/timer.js | 5 ++--- .../app/schedule/immediate-onlyonce.js | 10 ++++++++++ test/fixtures/immediate-onlyonce/package.json | 3 +++ test/schedule.test.js | 11 ++++++++++- 4 files changed, 25 insertions(+), 4 deletions(-) create mode 100644 test/fixtures/immediate-onlyonce/app/schedule/immediate-onlyonce.js create mode 100644 test/fixtures/immediate-onlyonce/package.json diff --git a/lib/timer.js b/lib/timer.js index 4acc693..a9ef97e 100644 --- a/lib/timer.js +++ b/lib/timer.js @@ -3,6 +3,7 @@ const parser = require('cron-parser'); const ms = require('humanize-ms'); const safetimers = require('safe-timers'); +const assert = require('assert'); class Timer { constructor(agent) { @@ -19,9 +20,7 @@ class Timer { */ handler(key, schedule, listener) { const { interval, cron, cronOptions, immediate } = schedule; - if (!interval && !cron) { - throw new Error('[egg-schedule] schedule.interval or schedule.cron must be present'); - } + assert(interval || cron || immediate, '[egg-schedule] schedule.interval or schedule.cron or schedule.immediate must be present'); if (interval) { const tid = this.safeInterval(listener, ms(interval)); diff --git a/test/fixtures/immediate-onlyonce/app/schedule/immediate-onlyonce.js b/test/fixtures/immediate-onlyonce/app/schedule/immediate-onlyonce.js new file mode 100644 index 0000000..c1d9f3d --- /dev/null +++ b/test/fixtures/immediate-onlyonce/app/schedule/immediate-onlyonce.js @@ -0,0 +1,10 @@ +'use strict'; + +exports.schedule = { + type: 'worker', + immediate: true, +}; + +exports.task = async function(ctx) { + ctx.logger.info('immediate-onlyonce'); +}; diff --git a/test/fixtures/immediate-onlyonce/package.json b/test/fixtures/immediate-onlyonce/package.json new file mode 100644 index 0000000..087fd4f --- /dev/null +++ b/test/fixtures/immediate-onlyonce/package.json @@ -0,0 +1,3 @@ +{ + "name": "immediate-onlyonce" +} \ No newline at end of file diff --git a/test/schedule.test.js b/test/schedule.test.js index fade162..fb3ac4a 100644 --- a/test/schedule.test.js +++ b/test/schedule.test.js @@ -85,6 +85,15 @@ describe('test/schedule.test.js', () => { assert(contains(log, 'immediate-interval') >= 2); assert(contains(log, 'immediate-cron') >= 2); }); + + it('should support immediate-onlyonce', async () => { + app = mm.cluster({ baseDir: 'immediate-onlyonce', workers: 2 }); + await app.ready(); + await sleep(1000); + const log = getLogContent('immediate-onlyonce'); + // console.log(log); + assert(contains(log, 'immediate-onlyonce') === 1); + }); }); describe('schedule type all', () => { @@ -157,7 +166,7 @@ describe('test/schedule.test.js', () => { app = mm.cluster({ baseDir: 'scheduleError', workers: 2 }); await app.ready(); await sleep(1000); - assert(/\[egg-schedule\] schedule\.interval or schedule\.cron must be present/.test(getErrorLogContent('scheduleError'))); + assert(/\[egg-schedule\] schedule\.interval or schedule\.cron or schedule\.immediate must be present/.test(getErrorLogContent('scheduleError'))); }); });