Skip to content

Commit

Permalink
fix: runSchedule should pass args (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
atian25 authored and dead-horse committed Apr 29, 2019
1 parent 77fc7d3 commit 98a0cf7
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
4 changes: 2 additions & 2 deletions app.js
Expand Up @@ -68,7 +68,7 @@ module.exports = app => {

// for test purpose
const directory = [].concat(path.join(app.config.baseDir, 'app/schedule'), app.config.schedule.directory || []);
app.runSchedule = schedulePath => {
app.runSchedule = (schedulePath, ...args) => {
// resolve real path
if (path.isAbsolute(schedulePath)) {
schedulePath = require.resolve(schedulePath);
Expand Down Expand Up @@ -101,6 +101,6 @@ module.exports = app => {
url: `/__schedule?path=${schedulePath}&${qs.stringify(schedule.schedule)}`,
});

return schedule.task(ctx);
return schedule.task(ctx, ...args);
};
};
4 changes: 2 additions & 2 deletions test/fixtures/worker/app/schedule/sub/cron.js
Expand Up @@ -5,6 +5,6 @@ exports.schedule = {
cron: '*/5 * * * * *',
};

exports.task = async function (ctx) {
ctx.logger.info('cron');
exports.task = async function (ctx, ...args) {
ctx.logger.info('cron', ...args);
};
10 changes: 10 additions & 0 deletions test/schedule.test.js
Expand Up @@ -269,6 +269,16 @@ describe('test/schedule.test.js', () => {
// console.log(log);
assert(contains(log, 'customDirectory') === 1);
});

it('should run schedule with args', async () => {
app = mm.app({ baseDir: 'worker', cache: false });
await app.ready();
await app.runSchedule('sub/cron', 'test');
await sleep(1000);
const log = getLogContent('worker');
// console.log(log);
assert(contains(log, 'cron test') === 1);
});
});

describe('stop schedule', () => {
Expand Down

0 comments on commit 98a0cf7

Please sign in to comment.