Skip to main content

Design Cron

  • Cron 调度
    • 重复
      • pattern
      • every
    • 是否立即开始
    • timezone
      • timezone id
      • utc offset
  • 任务调度
    • 是否并行 - 等待上一个完成
* * * * * *
    • 0-59
    • 0-59
    • 0-23
    • 1-31
    • 1-12
    • JAN-DEC
  • 周/星期 - day of week
    • 0-7
    • MIN-SUN
  • 年 - 可选
symbolfor
*all
/step
,list
-range
?ignore,no specific value
Llast
Wweekday
#nth
aliasfor
@reboot重启后执行一次, crond
@yearly0 0 1 1 *
@annually@yearly
@monthly0 0 1 * *
@weekly0 0 * * 0
@daily0 0 * * *
@midnight@daily
@hourly0 * * * *
e.g.forcn
6#3third Friday of the month每月第三个星期五
30 10 ? * 5L0:30 AM on the last Thursday of every month每月最后一个星期五上午10:30
0 0 0 LW * *last weekday of the month at midnight每月最后一个工作日午夜

quartz

K8S CronJob

type CronJobSpec = {
concurrencyPolicy: string;
failedJobsHistoryLimit: number; // 保留失败任务的数量
successfulJobsHistoryLimit: number; // 保留成功任务的数量
schedule: string; // cron
startingDeadlineSeconds: number;
suspend: boolean; // 暂停
timeZone: string;
jobTemplate: JobTemplateSpec;
};

type JobTemplateSpec = {
metadata: ObjectMeta;
spec: JobSpec;
};
type JobSpec = {
activeDeadlineSeconds: number; // 超时时间
backoffLimit: number; // 重试次数
backoffLimitPerIndex: number; //
completionMode: string; // 完成模式
completions: number; // 完成次数
managedBy: string; // 管理者
manualSelector: boolean; // 手动选择
maxFailedIndexes: number; // 最大失败索引
parallelism: number; // 并行度
podFailurePolicy: PodFailurePolicy;
podReplacementPolicy: string; // pod 替换策略
successPolicy: SuccessPolicy;
suspend: boolean; // 暂停
ttlSecondsAfterFinished: number; // 完成后的存活时间
selector: LabelSelector; // 选择器
template: PodTemplateSpec; // pod 模板
};

Nomad Perriodic

type Periodic = {
/**
* @deprecated
*/
cron: string;
crons: string[];
prohibit_overlap: boolean;
time_zone: string;
enabled: boolean;
};
job "docs" {
periodic {
cron = "*/15 * * * * *"
prohibit_overlap = true
}
}

node cron

export type CronContext<C> = C extends null ? CronJob : NonNullable<C>;
export type CronCallback<C, WithOnCompleteBool extends boolean = false> = (
this: CronContext<C>,
onComplete: WithOnCompleteBool extends true ? CronOnCompleteCallback : never,
) => void | Promise<void>;
export type CronOnCompleteCallback = () => void | Promise<void>;
export type CronSystemCommand =
| string
| {
command: string;
args?: readonly string[] | null;
options?: SpawnOptions | null;
};
export type CronCommand<C, WithOnCompleteBool extends boolean = false> =
| CronCallback<C, WithOnCompleteBool>
| CronSystemCommand;

interface BaseCronJobParams<OC extends CronOnCompleteCommand | null = null, C = null> {
cronTime: string | Date | DateTime;
onTick: CronCommand<C, WithOnComplete<OC>>;
onComplete?: OC;
start?: boolean | null;
context?: C;
runOnInit?: boolean | null;
unrefTimeout?: boolean | null;
waitForCompletion?: boolean | null;
errorHandler?: ((error: unknown) => void) | null;
}

export type CronJobParams<OC extends CronOnCompleteCommand | null = null, C = null> = BaseCronJobParams<OC, C> &
(
| {
timeZone?: string | null;
utcOffset?: never;
}
| {
timeZone?: never;
utcOffset?: number | null;
}
);

BullMQ

/**
* Settings for repeatable jobs
*
* @see {@link https://docs.bullmq.io/guide/jobs/repeatable}
*/
export interface RepeatOptions extends Omit<ParserOptions, 'iterator'> {
/**
* A repeat pattern
*/
pattern?: string;

/**
* Custom repeatable key. This is the key that holds the "metadata"
* of a given repeatable job. This key is normally auto-generated but
* it is sometimes useful to specify a custom key for easier retrieval
* of repeatable jobs.
*/
key?: string;

/**
* Number of times the job should repeat at max.
*/
limit?: number;

/**
* Repeat after this amount of milliseconds
* (`pattern` setting cannot be used together with this setting.)
*/
every?: number;

/**
* Repeated job should start right now
* ( work only with cron settings)
*/
immediately?: boolean;

/**
* The start value for the repeat iteration count.
*/
count?: number;

/**
* Offset in milliseconds to affect the next iteration time
*
* */
offset?: number;

/**
* Internal property to store the previous time the job was executed.
*/
prevMillis?: number;

/**
* Internal property to store de job id
* @deprecated
*/
jobId?: string;
}