const cron = require('node-cron');

const settings = [
  ['sync', '0 8 * * *', require('../../sync/sync-service')],  // 每天早上8点同步一次
  ['fetch-meta', '10 8 * * *', require('./fetch-meta')],  // 每天早上8点10分跑一次，跟在sync同步之后，ai自动生成标题和文案
  ['open-art', '0 10 * * *', require('./open-art')],  // 每天早上10点跑一次，开放部分内容
  ['sitemap', '10 10 * * *', require('./sitemap')],  // 每天10点10分跑一次，生成sitemap
]



settings.forEach(setting => {
  let [name, schedule, job] = setting;
  if (!job.run) throw new Error(`Job [${name}] has no run() function`);
  console.log(`[${name}] run@'${schedule}' installed!`);
  cron.schedule(schedule, () => {
    let date = new Date().toLocaleString();
    console.log(`[${name}]@'${schedule}' running @ ${date}`);
    if (job.run) {
      job.run().then(console.log).catch(console.error);
    }
  })
})
