You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Agenda/Cron是否支持每两周周一、周二重复任务?

Answer: Scheduling Bi-Weekly Tasks on Mondays & Tuesdays with Agenda/Cron

Hey there! Great question—yes, both Agenda and Cron fully support scheduling tasks that repeat every two weeks on specific days (Monday and Tuesday), and you can absolutely adjust the interval and target days dynamically. Let me walk you through the solutions:

1. Using Agenda's Native API (Most Intuitive)

Agenda provides a clean, readable way to define this schedule without wrestling with raw Cron syntax. Here’s how you’d set it up:

// Example: Schedule a task to run every 2 weeks on Monday and Tuesday
agenda.define('your-task-name', (job) => {
  // Your task logic here
});

// Define dynamic parameters
const repeatInterval = 2; // Adjust this number to change the week interval (e.g., 3 for every 3 weeks)
const targetDays = ['Monday', 'Tuesday']; // Modify this array to add/remove target days

agenda.every(`${repeatInterval} weeks`, 'your-task-name', null, {
  skipImmediate: true, // Optional: Skip running immediately when scheduling
  timezone: 'Your/Timezone' // Optional: Set your desired timezone
}).on(targetDays);

This approach makes dynamic adjustments straightforward—just update repeatInterval or targetDays and re-apply the schedule as needed.

2. Using Raw Cron Expression

If you prefer using Cron syntax (Agenda also accepts Cron strings), you can use an extended Cron expression that handles the bi-weekly interval. Note that standard Cron doesn’t natively support "every N weeks" directly, but most modern implementations (including Agenda’s) support this extension. Here’s a working expression:

0 0 * * 1,2/2

Breakdown of the Cron expression:

  • 0 0: Run at midnight (adjust to your preferred time, e.g., 3 15 for 3:15 PM)
  • * *: Every day of the month, every month
  • 1,2/2: Every 2nd occurrence of Monday (1) and Tuesday (2) — which translates to every two weeks on those days

To build this dynamically, you can construct the string programmatically:

const weekInterval = 2;
const days = [1, 2]; // 1 = Monday, 2 = Tuesday (use 0-6 or 1-7 based on your Cron setup)
const cronString = `0 0 * * ${days.join(',')}/${weekInterval}`;

// Use this string in Agenda
agenda.every(cronString, 'your-task-name');

Key Notes

  • Always configure the correct timezone if scheduling tasks across regions—Agenda lets you set this via the timezone option to avoid time drift issues.
  • If you update the dynamic parameters later, be sure to re-schedule the task or update the existing job definition to apply the changes.

内容的提问来源于stack exchange,提问作者naruto

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 07:44:17