chore(cron): simplify enabled checks for lint

This commit is contained in:
Gustavo Madeira Santana
2026-02-15 10:19:50 -05:00
parent fa4c282f9e
commit 88caa4b50c
3 changed files with 14 additions and 21 deletions

View File

@@ -55,7 +55,7 @@ export function findJobOrThrow(state: CronServiceState, id: string) {
} }
export function computeJobNextRunAtMs(job: CronJob, nowMs: number): number | undefined { export function computeJobNextRunAtMs(job: CronJob, nowMs: number): number | undefined {
if (job.enabled === false) { if (!job.enabled) {
return undefined; return undefined;
} }
if (job.schedule.kind === "every") { if (job.schedule.kind === "every") {
@@ -102,7 +102,7 @@ function normalizeJobTickState(params: { state: CronServiceState; job: CronJob;
changed = true; changed = true;
} }
if (job.enabled === false) { if (!job.enabled) {
if (job.state.nextRunAtMs !== undefined) { if (job.state.nextRunAtMs !== undefined) {
job.state.nextRunAtMs = undefined; job.state.nextRunAtMs = undefined;
changed = true; changed = true;
@@ -220,9 +220,7 @@ export function recomputeNextRunsForMaintenance(state: CronServiceState): boolea
export function nextWakeAtMs(state: CronServiceState) { export function nextWakeAtMs(state: CronServiceState) {
const jobs = state.store?.jobs ?? []; const jobs = state.store?.jobs ?? [];
const enabled = jobs.filter( const enabled = jobs.filter((j) => j.enabled && typeof j.state.nextRunAtMs === "number");
(j) => j.enabled !== false && typeof j.state.nextRunAtMs === "number",
);
if (enabled.length === 0) { if (enabled.length === 0) {
return undefined; return undefined;
} }
@@ -482,11 +480,7 @@ export function isJobDue(job: CronJob, nowMs: number, opts: { forced: boolean })
if (opts.forced) { if (opts.forced) {
return true; return true;
} }
return ( return job.enabled && typeof job.state.nextRunAtMs === "number" && nowMs >= job.state.nextRunAtMs;
job.enabled !== false &&
typeof job.state.nextRunAtMs === "number" &&
nowMs >= job.state.nextRunAtMs
);
} }
export function resolveJobPayloadTextForMain(job: CronJob): string | undefined { export function resolveJobPayloadTextForMain(job: CronJob): string | undefined {

View File

@@ -84,7 +84,7 @@ export async function list(state: CronServiceState, opts?: { includeDisabled?: b
} }
} }
const includeDisabled = opts?.includeDisabled === true; const includeDisabled = opts?.includeDisabled === true;
const jobs = (state.store?.jobs ?? []).filter((j) => includeDisabled || j.enabled !== false); const jobs = (state.store?.jobs ?? []).filter((j) => includeDisabled || j.enabled);
return jobs.toSorted((a, b) => (a.state.nextRunAtMs ?? 0) - (b.state.nextRunAtMs ?? 0)); return jobs.toSorted((a, b) => (a.state.nextRunAtMs ?? 0) - (b.state.nextRunAtMs ?? 0));
}); });
} }
@@ -151,7 +151,7 @@ export async function update(state: CronServiceState, id: string, patch: CronJob
job.updatedAtMs = now; job.updatedAtMs = now;
if (scheduleChanged || enabledChanged) { if (scheduleChanged || enabledChanged) {
if (job.enabled !== false) { if (job.enabled) {
job.state.nextRunAtMs = computeJobNextRunAtMs(job, now); job.state.nextRunAtMs = computeJobNextRunAtMs(job, now);
} else { } else {
job.state.nextRunAtMs = undefined; job.state.nextRunAtMs = undefined;

View File

@@ -90,7 +90,7 @@ function applyJobResult(
"cron: disabling one-shot job after error", "cron: disabling one-shot job after error",
); );
} }
} else if (result.status === "error" && job.enabled !== false) { } else if (result.status === "error" && job.enabled) {
// Apply exponential backoff for errored jobs to prevent retry storms. // Apply exponential backoff for errored jobs to prevent retry storms.
const backoff = errorBackoffMs(job.state.consecutiveErrors ?? 1); const backoff = errorBackoffMs(job.state.consecutiveErrors ?? 1);
const normalNext = computeJobNextRunAtMs(job, result.endedAt); const normalNext = computeJobNextRunAtMs(job, result.endedAt);
@@ -107,7 +107,7 @@ function applyJobResult(
}, },
"cron: applying error backoff", "cron: applying error backoff",
); );
} else if (job.enabled !== false) { } else if (job.enabled) {
job.state.nextRunAtMs = computeJobNextRunAtMs(job, result.endedAt); job.state.nextRunAtMs = computeJobNextRunAtMs(job, result.endedAt);
} else { } else {
job.state.nextRunAtMs = undefined; job.state.nextRunAtMs = undefined;
@@ -129,11 +129,10 @@ export function armTimer(state: CronServiceState) {
const nextAt = nextWakeAtMs(state); const nextAt = nextWakeAtMs(state);
if (!nextAt) { if (!nextAt) {
const jobCount = state.store?.jobs.length ?? 0; const jobCount = state.store?.jobs.length ?? 0;
const enabledCount = state.store?.jobs.filter((j) => j.enabled !== false).length ?? 0; const enabledCount = state.store?.jobs.filter((j) => j.enabled).length ?? 0;
const withNextRun = const withNextRun =
state.store?.jobs.filter( state.store?.jobs.filter((j) => j.enabled && typeof j.state.nextRunAtMs === "number")
(j) => j.enabled !== false && typeof j.state.nextRunAtMs === "number", .length ?? 0;
).length ?? 0;
state.deps.log.debug( state.deps.log.debug(
{ jobCount, enabledCount, withNextRun }, { jobCount, enabledCount, withNextRun },
"cron: armTimer skipped - no jobs with nextRunAtMs", "cron: armTimer skipped - no jobs with nextRunAtMs",
@@ -347,7 +346,7 @@ function findDueJobs(state: CronServiceState): CronJob[] {
if (!j.state) { if (!j.state) {
j.state = {}; j.state = {};
} }
if (j.enabled === false) { if (!j.enabled) {
return false; return false;
} }
if (typeof j.state.runningAtMs === "number") { if (typeof j.state.runningAtMs === "number") {
@@ -371,7 +370,7 @@ export async function runMissedJobs(
if (!j.state) { if (!j.state) {
j.state = {}; j.state = {};
} }
if (j.enabled === false) { if (!j.enabled) {
return false; return false;
} }
if (skipJobIds?.has(j.id)) { if (skipJobIds?.has(j.id)) {
@@ -410,7 +409,7 @@ export async function runDueJobs(state: CronServiceState) {
if (!j.state) { if (!j.state) {
j.state = {}; j.state = {};
} }
if (j.enabled === false) { if (!j.enabled) {
return false; return false;
} }
if (typeof j.state.runningAtMs === "number") { if (typeof j.state.runningAtMs === "number") {