-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(schedule): Added interval scheduling capability
- Loading branch information
Wes
committed
Oct 20, 2017
1 parent
761a55f
commit 690037b
Showing
5 changed files
with
266 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
const Scheduler = require('tnt-scheduler'); | ||
let sch = new Scheduler(); | ||
|
||
// This is only necessary if you are using scheduleJob | ||
sch.on('error', error => { | ||
console.log(error.message); | ||
}); | ||
|
||
try { | ||
sch.createJob('my_job', myJob); | ||
} catch (error) { | ||
console.log(error.message); | ||
} | ||
|
||
sch.on('running::my_job', promise => { | ||
console.log('running my_job'); | ||
promise.then(resolution=>{ | ||
console.log('resolved with', resolution); | ||
}, error=>{ | ||
console.log('rejected with', error); | ||
}) | ||
}); | ||
|
||
try { | ||
sch.scheduleJob('my_job', 1000, 'arg1', 2, {arg: 3}); | ||
} catch (error) { | ||
console.log(error.message); | ||
} | ||
|
||
// myJob is a simple function that returns a promise that randomly | ||
//resolves or rejects after 3000 ms | ||
function myJob(...args) { | ||
// returns our promise | ||
return new Promise(function (resolve, reject) { | ||
// Waits 3 seconds | ||
setTimeout(() => { | ||
// Roll the dice | ||
let odds = Math.round(Math.random()); | ||
if (odds) { | ||
// Resolves if 1; | ||
resolve(args); | ||
} else { | ||
// Rejects if 0 | ||
reject('Boom!'); | ||
} | ||
}, 3000); | ||
}) | ||
} | ||
|
||
// Clears the schedule after 10 seconds | ||
setTimeout(() => { | ||
sch.clearSchedule('my_job'); | ||
}, 10000) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
const Scheduler = require('./index'); | ||
let sch = new Scheduler(); | ||
|
||
sch.on('error', error => { | ||
console.log(error.message); | ||
}); | ||
|
||
try { | ||
sch.createJob('my_job', myJob); | ||
} catch (error) { | ||
console.log(error.message); | ||
} | ||
|
||
sch.on('running::my_job', promise => { | ||
console.log('running my_job'); | ||
promise.then(resolution=>{ | ||
console.log('resolved with', resolution); | ||
}, error=>{ | ||
console.log('rejected with', error); | ||
}) | ||
}); | ||
|
||
try { | ||
sch.scheduleJob('my_job', 1000, 'arg1', 2, {arg: 3}); | ||
} catch (error) { | ||
console.log(error.message); | ||
} | ||
|
||
// myJob is a simple function that returns a promise that randomly | ||
//resolves or rejects after 3000 ms | ||
function myJob(...args) { | ||
// returns our promise | ||
return new Promise(function (resolve, reject) { | ||
// Waits 3 seconds | ||
setTimeout(() => { | ||
// Roll the dice | ||
let odds = Math.round(Math.random()); | ||
if (odds) { | ||
// Resolves if 1; | ||
resolve(args); | ||
} else { | ||
// Rejects if 0 | ||
reject('Boom!'); | ||
} | ||
}, 3000); | ||
}) | ||
} | ||
|
||
// Clears the schedule after 10 seconds | ||
setTimeout(() => { | ||
sch.clearSchedule('my_job'); | ||
}, 10000) |