You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

152 lines
5.1 KiB
JavaScript

10 months ago
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
*/
10 months ago
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();
// }
// });
};
10 months ago
/**
* 更新酒店的状态, 是否下架
* 每天启动同步;
* * 每次同步需要3000+
*/
const AidsState = () => {
const dailyJob = scheduleJob('0 0 0 * * *', 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();
}
});
});
};
/**
* 获取新增的酒店详情
* ID同步结束后启动, ID大约需要3小时
* 每天03:05:00启动
*/
const newHotelDetails = () => {
// return false;
const dailyJob1 = scheduleJob('0 5 3 * * *', async function () {
const job1 = scheduleJob('*/5 * * * * *', async function () {
console.log('-------------------------syncing heytrip, get accommodation details.-------------------------');
const isRunning = job1.pendingInvocations[0]?.job?.running == 1;
if (!isRunning) {
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]!');
}
} else {
console.log('pre job running! cancelNext[newHotelDetails]');
job1.cancelNext();
}
});
10 months ago
});
};
/**
* 更新酒店详情, 按语种
* 每周启动一次
* 周五20:05:00启动
*/
10 months ago
const hotelLgcDetails = () => {
// return false;
const dailyJob2 = scheduleJob('0 5 20 * * 5', async function () {
const job2 = scheduleJob('*/5 * * * * *', async function () {
console.log('-------------------------syncing heytrip, get accommodation details.-------------------------');
10 months ago
const isRunning = job2.pendingInvocations[0]?.job?.running == 1;
if (!isRunning) {
const res = await heytripService.newHotelsLgc('1');
10 months ago
// job2.cancel(); // debug: 0
if (res.next !== true) {
job2.cancel(res.restart);
console.log('job completed! canceled job[hotelLgcDetails]!');
}
} else {
console.log('pre job running! cancelNext[hotelLgcDetails]');
job2.cancelNext();
10 months ago
}
});
10 months ago
});
};
/**
* 更新中国酒店的中文详情.
* 每周启动一次
* 周六20:05:00启动
*/
10 months ago
const chinaHotelDetails = () => {
// return false;
const dailyJobCN = scheduleJob('0 5 20 * * 6', async function () {
const job3 = scheduleJob('*/4 * * * * *', async function () {
console.log('-------------------------syncing heytrip, get china accommodation details.-------------------------');
const isRunning = job3.pendingInvocations[0]?.job?.running == 1;
if (!isRunning) {
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 * * *');
}
} else {
console.log('pre job running! cancelNext');
job3.cancelNext();
10 months ago
}
});
10 months ago
});
};
module.exports = {
Aids, AidsState,
newHotelDetails, hotelLgcDetails, chinaHotelDetails
10 months ago
}