






















@@ -353,47 +353,59 @@ export async function update(state: CronServiceState, id: string, patch: CronJob
353353await ensureLoaded(state, { skipRecompute: true });
354354const job = findJobOrThrow(state, id);
355355const now = state.deps.nowMs();
356-applyJobPatch(job, patch, { defaultAgentId: state.deps.defaultAgentId });
357-if (job.schedule.kind === "every") {
358-const anchor = job.schedule.anchorMs;
356+const nextJob = structuredClone(job);
357+applyJobPatch(nextJob, patch, { defaultAgentId: state.deps.defaultAgentId });
358+if (nextJob.schedule.kind === "every") {
359+const anchor = nextJob.schedule.anchorMs;
359360if (typeof anchor !== "number" || !Number.isFinite(anchor)) {
360361const patchSchedule = patch.schedule;
361362const fallbackAnchorMs =
362363patchSchedule?.kind === "every"
363364 ? now
364- : typeof job.createdAtMs === "number" && Number.isFinite(job.createdAtMs)
365- ? job.createdAtMs
365+ : typeof nextJob.createdAtMs === "number" && Number.isFinite(nextJob.createdAtMs)
366+ ? nextJob.createdAtMs
366367 : now;
367-job.schedule = {
368- ...job.schedule,
368+nextJob.schedule = {
369+ ...nextJob.schedule,
369370anchorMs: Math.max(0, Math.floor(fallbackAnchorMs)),
370371};
371372}
372373}
373374const scheduleChanged = patch.schedule !== undefined;
374375const enabledChanged = patch.enabled !== undefined;
375376376-job.updatedAtMs = now;
377+if (scheduleChanged && nextJob.schedule.kind === "cron" && !isJobEnabled(nextJob)) {
378+computeJobNextRunAtMs({ ...nextJob, enabled: true }, now);
379+}
380+381+nextJob.updatedAtMs = now;
377382if (scheduleChanged || enabledChanged) {
378-if (isJobEnabled(job)) {
379-job.state.nextRunAtMs = computeJobNextRunAtMs(job, now);
383+if (isJobEnabled(nextJob)) {
384+nextJob.state.nextRunAtMs = computeJobNextRunAtMs(nextJob, now);
380385} else {
381-job.state.nextRunAtMs = undefined;
382-job.state.runningAtMs = undefined;
386+nextJob.state.nextRunAtMs = undefined;
387+nextJob.state.runningAtMs = undefined;
388+}
389+} else if (isJobEnabled(nextJob) && !hasScheduledNextRunAtMs(nextJob.state.nextRunAtMs)) {
390+nextJob.state.nextRunAtMs = computeJobNextRunAtMs(nextJob, now);
391+}
392+393+if (state.store) {
394+const index = state.store.jobs.findIndex((entry) => entry.id === id);
395+if (index >= 0) {
396+state.store.jobs[index] = nextJob;
383397}
384-} else if (isJobEnabled(job) && !hasScheduledNextRunAtMs(job.state.nextRunAtMs)) {
385-job.state.nextRunAtMs = computeJobNextRunAtMs(job, now);
386398}
387399388400await persist(state);
389401armTimer(state);
390402emit(state, {
391403jobId: id,
392404action: "updated",
393- job,
394-nextRunAtMs: job.state.nextRunAtMs,
405+job: nextJob,
406+nextRunAtMs: nextJob.state.nextRunAtMs,
395407});
396-return job;
408+return nextJob;
397409});
398410}
399411此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。