Comparing version
{ | ||
"name": "node-cron", | ||
"version": "4.0.0-beta.1", | ||
"version": "4.0.0-beta.2", | ||
"description": "A simple cron-like task scheduler for Node.js", | ||
@@ -5,0 +5,0 @@ "author": "Lucas Merencia", |
@@ -33,16 +33,2 @@ import { assert } from 'chai'; | ||
}); | ||
it('should schedule a task on start', async function() { | ||
let executed = 0; | ||
const task = cron.schedule('* * * * *', () => { | ||
executed += 1; | ||
}, { runOnScheduling: true}); | ||
clock.tick(1000); | ||
// adds a delay after tick | ||
await new Promise(r=>{setTimeout(r, 200)}) | ||
assert.equal(1, executed); | ||
task.stop(); | ||
}); | ||
@@ -84,32 +70,2 @@ it('should schedule a task with America/Sao_Paulo timezone', function(done) { | ||
it('should schedule a task stopped', function() { | ||
let executed = 0; | ||
const task = cron.schedule('* * * * * *', () => { | ||
executed += 1; | ||
}, { scheduled: false }); | ||
clock.tick(2000); | ||
assert.equal(0, executed); | ||
task.stop(); | ||
}); | ||
it('should start a stopped task', async function() { | ||
let executed = 0; | ||
let task = cron.schedule('* * * * * *', () => { | ||
executed += 1; | ||
}, { scheduled: false }); | ||
clock.tick(2000); | ||
// adds a delay after tick | ||
await new Promise(r=>{setTimeout(r, 200)}) | ||
assert.equal(0, executed); | ||
task.start(); | ||
clock.tick(2000); | ||
// adds a delay after tick | ||
await new Promise(r=>{setTimeout(r, 200)}) | ||
assert.equal(2, executed); | ||
task.stop(); | ||
}); | ||
it('should schedule a background task', async function() { | ||
@@ -140,2 +96,18 @@ let task = cron.schedule('* * * * *', '../test-assets/dummy-task'); | ||
}); | ||
describe('createTask', function(){ | ||
it('creates a inline task', function(){ | ||
const task = cron.createTask('* * * * *', ()=>{}); | ||
assert.isDefined(task); | ||
assert.isDefined(task.id); | ||
assert.equal(task.getStatus(), 'stopped'); | ||
}); | ||
it('creates a background task', function(){ | ||
const task = cron.createTask('* * * * *', '../test-assets/dummy-task'); | ||
assert.isDefined(task); | ||
assert.isDefined(task.id); | ||
assert.equal(task.getStatus(), 'stopped'); | ||
}); | ||
}) | ||
}); | ||
@@ -142,0 +114,0 @@ |
@@ -33,5 +33,3 @@ /** | ||
name?: string; | ||
scheduled?: boolean; | ||
timezone?: string; | ||
runOnScheduling?: boolean; | ||
noOverlap?: boolean; | ||
@@ -75,15 +73,5 @@ maxExecutions?: number; | ||
const task = createTask(expression, func, taskOptions); | ||
registry.add(task); | ||
async function init(){ | ||
if(options?.scheduled){ | ||
await task.start(); | ||
if(options && options.runOnScheduling){ | ||
await task.execute(); | ||
} | ||
} | ||
}; | ||
task.start(); | ||
init(); | ||
return task; | ||
@@ -93,3 +81,3 @@ } | ||
/** | ||
* Creates a task instance based on the provided parameters without adding it to the registry. | ||
* Creates a task instance based on the provided parameters adding it to the registry. | ||
* | ||
@@ -106,9 +94,14 @@ * @param expression - A cron expression that determines when the task executes | ||
} | ||
let task: ScheduledTask; | ||
if(func instanceof Function){ | ||
return new InlineScheduledTask(expression, func, taskOptions); | ||
task = new InlineScheduledTask(expression, func, taskOptions); | ||
} else { | ||
const taskPath = solvePath(func); | ||
task = new BackgroundScheduledTask(expression, taskPath, taskOptions); | ||
} | ||
const taskPath = solvePath(func); | ||
registry.add(task); | ||
return new BackgroundScheduledTask(expression, taskPath, taskOptions); | ||
return task; | ||
} | ||
@@ -175,2 +168,7 @@ | ||
schedule, | ||
/** | ||
* Creates a task instance based on the provided parameters. | ||
*/ | ||
createTask, | ||
@@ -177,0 +175,0 @@ /** |
@@ -37,2 +37,21 @@ import { assert } from 'chai'; | ||
describe('getNextRun', function(){ | ||
it('returns next run', async function(){ | ||
const task = new BackgroundScheduledTask('* * * * *', './test-assets/dummy-task.js'); | ||
fakeChildProcess.send.callsFake(()=>{ | ||
task.emitter.emit('task:started'); | ||
}); | ||
await task.start(); | ||
const nextMinute = new Date(); | ||
nextMinute.setMilliseconds(0); | ||
nextMinute.setSeconds(0); | ||
nextMinute.setMinutes(nextMinute.getMinutes() + 1); | ||
assert.equal(task.getNextRun()?.getTime(), nextMinute.getTime()); | ||
task.destroy(); | ||
}); | ||
}); | ||
describe('start', () => { | ||
@@ -143,3 +162,3 @@ it('do not fail if already started', async function(){ | ||
assert.equal(event.execution?.error.stack, 'fake stack'); | ||
assert.equal(event.task.stateMachine.state, 'stopped') | ||
assert.equal(event.task.stateMachine.state, 'idle') | ||
}); | ||
@@ -146,0 +165,0 @@ |
@@ -10,2 +10,3 @@ import { resolve as resolvePath } from 'path'; | ||
import logger from '../../logger'; | ||
import { TimeMatcher } from 'src/time/time-matcher'; | ||
@@ -48,2 +49,10 @@ const daemonPath = resolvePath(__dirname, 'daemon.js'); | ||
getNextRun(): Date | null { | ||
if ( this.stateMachine.state !== 'stopped'){ | ||
const timeMatcher = new TimeMatcher(this.cronExpression, this.options?.timezone); | ||
return timeMatcher.getNextMatch(new Date()); | ||
} | ||
return null; | ||
} | ||
start(): Promise<void> { | ||
@@ -99,2 +108,3 @@ return new Promise((resolve, reject) => { | ||
this.once('task:started', () => { | ||
this.stateMachine.changeState('idle'); | ||
clearTimeout(timeout); | ||
@@ -101,0 +111,0 @@ resolve(undefined); |
@@ -22,2 +22,15 @@ import { assert } from 'chai'; | ||
it('returns next run', function(){ | ||
const task = new InlineScheduledTask('* * * * *', ()=> {}); | ||
task.start(); | ||
const nextMinute = new Date(); | ||
nextMinute.setMilliseconds(0); | ||
nextMinute.setSeconds(0); | ||
nextMinute.setMinutes(nextMinute.getMinutes() + 1); | ||
assert.equal(task.getNextRun()?.getTime(), nextMinute.getTime()); | ||
task.destroy(); | ||
}); | ||
it('stops', function(){ | ||
@@ -24,0 +37,0 @@ const task = new InlineScheduledTask('* * * * * *', ()=> {}); |
@@ -73,2 +73,9 @@ import EventEmitter from "events"; | ||
getNextRun(): Date | null{ | ||
if ( this.stateMachine.state !== 'stopped'){ | ||
return this.runner.nextRun(); | ||
} | ||
return null; | ||
} | ||
private changeState(state){ | ||
@@ -75,0 +82,0 @@ if(this.runner.isStarted()){ |
@@ -60,2 +60,3 @@ | ||
execute(): Promise<any>; | ||
getNextRun(): Date | null; | ||
@@ -62,0 +63,0 @@ on(event: TaskEvent, fun: (context: TaskContext) => Promise<void> | void): void |
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
140931
0.37%3362
0.48%0
-100%