Skip to content

Commit

Permalink
feat: support class
Browse files Browse the repository at this point in the history
schedule can extend from Subscription which is context class

Ref eggjs/egg#1468
  • Loading branch information
popomore committed Oct 12, 2017
1 parent 39bbf60 commit de643da
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 3 deletions.
7 changes: 6 additions & 1 deletion app.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const loadSchedule = require('./lib/load_schedule');
const qs = require('querystring');
const path = require('path');
const is = require('is-type-of');

module.exports = app => {
const schedules = loadSchedule(app);
Expand Down Expand Up @@ -30,7 +31,11 @@ module.exports = app => {
method: 'SCHEDULE',
url: `/__schedule?path=${schedulePath}&${qs.stringify(schedule.schedule)}`,
});
return schedule.task(ctx);

if (!is.class(schedule)) return schedule.task(ctx);

const s = new schedule(ctx);
return s.subscribe();
};

// log schedule list
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
16 changes: 16 additions & 0 deletions test/fixtures/subscription/app/schedule/interval.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict';

const Subscription = require('egg').BaseContextClass;

class Interval extends Subscription {
static get schedule() {
type: 'worker',
interval: 4000,
}

* subscribe() {
this.ctx.logger.info('interval');
}
}

module.exports = Interval;
16 changes: 16 additions & 0 deletions test/fixtures/subscription/app/schedule/sub/cron.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict';

const Subscription = require('egg').BaseContextClass;

class Interval extends Subscription {
static get schedule() {
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: 'worker', workers: 2, cache: false });
// app.debug();
yield app.ready();
yield sleep(5000);
const log = getLogContent('worker');
// console.log(log);
assert(contains(log, 'interval') === 1);
assert(contains(log, 'cron') === 1);
});
});
});

function sleep(time) {
Expand Down

0 comments on commit de643da

Please sign in to comment.