Last active
January 19, 2019 04:12
-
-
Save trylovetom/3cf20864f901fc1eadb3f7d4c2d385d0 to your computer and use it in GitHub Desktop.
The Job Schedule Base On setInterval.
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
import { | |
job as homeJob, | |
time as homeTime, | |
enable as homeEnable, | |
runFirstTime as homeRunFirstTime | |
} from './home' | |
/** | |
* 建立 Schedule | |
* @version 1.1.0 | |
* @param {Function} job 要執行的函數 | |
* @param {Number} time ms 間隔 | |
*/ | |
export function createSchedule (job, time) { | |
return setInterval(job, time) | |
} | |
/** | |
* 刪除 Schedule | |
* @version 1.1.0 | |
* @param {Object} schedule 要刪除的 schedule | |
*/ | |
export function clearSchedule (schedule) { | |
clearInterval(schedule) | |
} | |
/** | |
* 開始執行 | |
* @returns {Array} 所有的 Schedule Instance | |
*/ | |
export function start () { | |
const list = [] | |
if (homeEnable === 'true' && process.env.NODE_APP_INSTANCE === '0') { // 只要 PM2 Cluster Master 執行 | |
if (homeRunFirstTime === 'true') { | |
setImmediate(homeJob) | |
} | |
list.push(createSchedule(homeJob, homeTime)) | |
} | |
return list | |
} | |
/** | |
* 停止執行 | |
* @param {Array} schedules 所有的 Schedule Instance | |
*/ | |
export function stop (schedules) { | |
for (let schedule of schedules) { | |
clearSchedule(schedule) | |
} | |
} |
Author
trylovetom
commented
Jan 19, 2019
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment