Skip to content

Commit

Permalink
feat: support class (#25)
Browse files Browse the repository at this point in the history
* feat: support class

schedule can extend from Subscription which is context class

Ref eggjs/egg#1468
  • Loading branch information
popomore authored and atian25 committed Oct 13, 2017
1 parent 39bbf60 commit c7816f2
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 4 deletions.
1 change: 1 addition & 0 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ module.exports = app => {
method: 'SCHEDULE',
url: `/__schedule?path=${schedulePath}&${qs.stringify(schedule.schedule)}`,
});

return schedule.task(ctx);
};

Expand Down
15 changes: 13 additions & 2 deletions lib/load_schedule.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
}
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
18 changes: 18 additions & 0 deletions test/fixtures/subscription/app/schedule/interval.js
Original file line number Diff line number Diff line change
@@ -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;
18 changes: 18 additions & 0 deletions test/fixtures/subscription/app/schedule/sub/cron.js
Original file line number Diff line number Diff line change
@@ -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;
3 changes: 3 additions & 0 deletions test/fixtures/subscription/config/plugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

exports.logrotator = true;
3 changes: 3 additions & 0 deletions test/fixtures/subscription/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "subscription"
}
13 changes: 13 additions & 0 deletions test/schedule.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit c7816f2

Please sign in to comment.