const { scheduleJob } = require('node-schedule'); const heytripService = require('../services/heytripService'); /** * node-schedule * ? * * * * * * ┬ ┬ ┬ ┬ ┬ ┬ │ │ │ │ │ │ │ │ │ │ │ └ day of week (0 - 7) (0 or 7 is Sun) │ │ │ │ └───── month (1 - 12) │ │ │ └────────── day of month (1 - 31) │ │ └─────────────── hour (0 - 23) │ └──────────────────── minute (0 - 59) └───────────────────────── second (0 - 59, OPTIONAL) */ /** * 获取可用的酒店id */ const Aids = () => { return false; // const jobA = scheduleJob('*/2 * * * * *', async function () { // const jobA = scheduleJob('0 0 0 * * *', async function () { // console.log('syncing heytrip, get available accommodation ids.'); // const isRunning = jobA.pendingInvocations[0]?.job?.running == 1; // if (!isRunning) { // const res = await heytripService.syncAids(); // if (res.nextPage !== true) { // console.log('job completed! canceled job!'); // jobA.cancel(); // } // } else { // console.log('pre job running! cancelNext'); // jobA.cancelNext(); // } // }); }; /** * 更新酒店的状态, 是否下架 * 每天启动同步; * * 每次同步需要3000+页 */ const AidsState = () => { const dailyJob = scheduleJob('0 0 0 * * *', async function () { // const dailyJob = scheduleJob('0 20 9 * * *', async function () { const jobAS = scheduleJob('*/2 * * * * *', async function () { console.log('--------------------syncing heytrip, get available accommodation ids.--------------------'); const isRunning = jobAS.pendingInvocations[0]?.job?.running == 1; if (!isRunning) { const res = await heytripService.syncAidState(); // jobAS.cancel(); // debug: 0 if (res.nextPage !== true) { console.log('job completed! canceled job[AidsState]!'); jobAS.cancel(); } } else { console.log('pre job running! cancelNext[AidsState]'); jobAS.cancelNext(true); } }); }); }; /** * 获取新增的酒店详情 * ID同步结束后启动, ID每10W 条大约需要半小时 * 每天03:05:00启动 */ const newHotelDetails = () => { // return false; const dailyJob1 = scheduleJob('0 5 3 * * *', async function () { // const dailyJob1 = scheduleJob('0 5 12 * * *', async function () { const job1 = scheduleJob('*/5 * * * * *', async function () { console.log('-------------------------syncing heytrip[newHotelDetails], get accommodation details.-------------------------'); const isRunning = job1.pendingInvocations[0]?.job?.running == 1; if (!isRunning) { try { const res = await heytripService.newHotels('1'); // job1.cancel(); // debug: 0 if (res.next !== true) { job1.cancel(res.restart); console.log('job completed! canceled job[newHotelDetails]!'); } } catch (error) { job1.cancelNext(true); } } else { console.log('pre job running! cancelNext[newHotelDetails]'); job1.cancelNext(true); } }); }); }; /** * 更新酒店详情, 按语种 * 每周启动一次 * 周五20:05:00启动 */ const hotelLgcDetails = () => { // return false; const dailyJob2 = scheduleJob('0 5 20 * * 5', async function () { const job2 = scheduleJob('*/5 * * * * *', async function () { console.log('-------------------------syncing heytrip[hotelLgcDetails], get accommodation details.-------------------------'); const isRunning = job2.pendingInvocations[0]?.job?.running == 1; if (!isRunning) { try { const res = await heytripService.newHotelsLgc('1'); // job2.cancel(); // debug: 0 if (res.next !== true) { job2.cancel(res.restart); console.log('job completed! canceled job[hotelLgcDetails]!'); } } catch (error) { job2.cancelNext(true); } } else { console.log('pre job running! cancelNext[hotelLgcDetails]'); job2.cancelNext(true); } }); }); }; /** * 更新中国酒店的中文详情. * 每周启动一次 * 周六20:05:00启动 */ const chinaHotelDetails = () => { // return false; const dailyJobCN = scheduleJob('0 5 20 * * 6', async function () { const job3 = scheduleJob('*/4 * * * * *', async function () { console.log('-------------------------syncing heytrip[chinaHotelDetails], get china accommodation details.-------------------------'); const isRunning = job3.pendingInvocations[0]?.job?.running == 1; if (!isRunning) { try { const res = await heytripService.chinaHotelsLgc2('2'); // job3.cancel(); // debug: 0 if (res.next !== true) { job3.cancel(res.restart); console.log('job completed! canceled job[chinaHotelDetails]!'); // job3.reschedule('0 0 0 * * *'); } } catch (error) { job3.cancelNext(true); } } else { console.log('pre job running! cancelNext[chinaHotelDetails]'); job3.cancelNext(true); } }); }); }; module.exports = { Aids, AidsState, newHotelDetails, hotelLgcDetails, chinaHotelDetails }