Browse Source

refactor: 修改商品信息示例中的监控频率变量名

- 将配置中的 `warnTimeRange` 变量名更改为 `monitorFrequency`,以提高代码可读性和一致性
- 更新相关逻辑,确保抓取频率的设置和输出信息与新变量名保持一致
- 该更改旨在使代码更易于理解,符合命名规范
master
lizhuang 1 month ago
parent
commit
211b3af1a7
1 changed files with 11 additions and 11 deletions
  1. 11
    11
      examples/productInfoExample.js

+ 11
- 11
examples/productInfoExample.js View File

const config = { const config = {
platform: "amazon", // 平台 platform: "amazon", // 平台
needScreenshot: true, // 是否需要截图 needScreenshot: true, // 是否需要截图
warnTimeRange: 2, // 监控频率(小时)
monitorFrequency: 2, // 监控频率(小时)
goodsList: [], // 商品列表 goodsList: [], // 商品列表
isRunning: false, // 是否正在执行抓取任务 isRunning: false, // 是否正在执行抓取任务
timer: null, // 定时器 timer: null, // 定时器
console.log(res.data); console.log(res.data);
const { rows } = res.data; const { rows } = res.data;
if (rows.length > 0) { if (rows.length > 0) {
config.warnTimeRange = rows[0].warnTimeRange;
config.monitorFrequency = rows[0].monitorFrequency;
} else { } else {
config.warnTimeRange = 2; // 默认2小时
config.monitorFrequency = 2; // 默认2小时
} }
console.log(`抓取频率设置为 ${config.warnTimeRange} 小时`);
console.log(`抓取频率设置为 ${config.monitorFrequency} 小时`);
return true; return true;
} catch (error) { } catch (error) {
console.error(`获取抓取配置失败: ${new Date().toLocaleString()}`, error.message); console.error(`获取抓取配置失败: ${new Date().toLocaleString()}`, error.message);
clearInterval(config.timer); 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 () => { config.timer = setInterval(async () => {
console.log(`定时任务触发: ${new Date().toLocaleString()}`); console.log(`定时任务触发: ${new Date().toLocaleString()}`);
// 执行抓取处理 // 执行抓取处理
await fetchGoodsListAndProcess(); await fetchGoodsListAndProcess();
// 如果warnTimeRange发生变化,重新设置定时器
const newIntervalMs = config.warnTimeRange * 60 * 60 * 1000;
// 如果monitorFrequency发生变化,重新设置定时器
const newIntervalMs = config.monitorFrequency * 60 * 60 * 1000;
if (newIntervalMs !== intervalMs) { if (newIntervalMs !== intervalMs) {
console.log(`监控频率已变更为 ${config.warnTimeRange} 小时,重新设置定时器`);
console.log(`监控频率已变更为 ${config.monitorFrequency} 小时,重新设置定时器`);
clearInterval(config.timer); clearInterval(config.timer);
startScheduler(); // 重新启动调度器 startScheduler(); // 重新启动调度器
} }


// 输出启动信息 // 输出启动信息
console.log(`抓取服务已启动: ${new Date().toLocaleString()}`); console.log(`抓取服务已启动: ${new Date().toLocaleString()}`);
console.log(`初始监控频率: ${config.warnTimeRange} 小时`);
console.log(`初始监控频率: ${config.monitorFrequency} 小时`);

Loading…
Cancel
Save