|
|
@@ -28,7 +28,7 @@ const frequency = 1000 * 60 * 60 * 24; // 24小时 |
|
|
|
const config = { |
|
|
|
platform: "amazon", // 平台 |
|
|
|
needScreenshot: true, // 是否需要截图 |
|
|
|
warnTimeRange: 2, // 监控频率(小时) |
|
|
|
monitorFrequency: 2, // 监控频率(小时) |
|
|
|
goodsList: [], // 商品列表 |
|
|
|
isRunning: false, // 是否正在执行抓取任务 |
|
|
|
timer: null, // 定时器 |
|
|
@@ -135,11 +135,11 @@ async function fetchConfig() { |
|
|
|
console.log(res.data); |
|
|
|
const { rows } = res.data; |
|
|
|
if (rows.length > 0) { |
|
|
|
config.warnTimeRange = rows[0].warnTimeRange; |
|
|
|
config.monitorFrequency = rows[0].monitorFrequency; |
|
|
|
} else { |
|
|
|
config.warnTimeRange = 2; // 默认2小时 |
|
|
|
config.monitorFrequency = 2; // 默认2小时 |
|
|
|
} |
|
|
|
console.log(`抓取频率设置为 ${config.warnTimeRange} 小时`); |
|
|
|
console.log(`抓取频率设置为 ${config.monitorFrequency} 小时`); |
|
|
|
return true; |
|
|
|
} catch (error) { |
|
|
|
console.error(`获取抓取配置失败: ${new Date().toLocaleString()}`, error.message); |
|
|
@@ -199,9 +199,9 @@ async function startScheduler() { |
|
|
|
clearInterval(config.timer); |
|
|
|
} |
|
|
|
|
|
|
|
// 设置定时器,根据warnTimeRange的小时数定时执行 |
|
|
|
const intervalMs = config.warnTimeRange * 60 * 60 * 1000; // 转换为毫秒 |
|
|
|
console.log(`设置定时任务,每 ${config.warnTimeRange} 小时执行一次,下次执行时间: ${new Date(Date.now() + intervalMs).toLocaleString()}`); |
|
|
|
// 设置定时器,根据monitorFrequency的小时数定时执行 |
|
|
|
const intervalMs = config.monitorFrequency * 60 * 60 * 1000; // 转换为毫秒 |
|
|
|
console.log(`设置定时任务,每 ${config.monitorFrequency} 小时执行一次,下次执行时间: ${new Date(Date.now() + intervalMs).toLocaleString()}`); |
|
|
|
|
|
|
|
config.timer = setInterval(async () => { |
|
|
|
console.log(`定时任务触发: ${new Date().toLocaleString()}`); |
|
|
@@ -210,10 +210,10 @@ async function startScheduler() { |
|
|
|
// 执行抓取处理 |
|
|
|
await fetchGoodsListAndProcess(); |
|
|
|
|
|
|
|
// 如果warnTimeRange发生变化,重新设置定时器 |
|
|
|
const newIntervalMs = config.warnTimeRange * 60 * 60 * 1000; |
|
|
|
// 如果monitorFrequency发生变化,重新设置定时器 |
|
|
|
const newIntervalMs = config.monitorFrequency * 60 * 60 * 1000; |
|
|
|
if (newIntervalMs !== intervalMs) { |
|
|
|
console.log(`监控频率已变更为 ${config.warnTimeRange} 小时,重新设置定时器`); |
|
|
|
console.log(`监控频率已变更为 ${config.monitorFrequency} 小时,重新设置定时器`); |
|
|
|
clearInterval(config.timer); |
|
|
|
startScheduler(); // 重新启动调度器 |
|
|
|
} |
|
|
@@ -234,4 +234,4 @@ startScheduler(); |
|
|
|
|
|
|
|
// 输出启动信息 |
|
|
|
console.log(`抓取服务已启动: ${new Date().toLocaleString()}`); |
|
|
|
console.log(`初始监控频率: ${config.warnTimeRange} 小时`); |
|
|
|
console.log(`初始监控频率: ${config.monitorFrequency} 小时`); |