Guides
Metrics and history
Every job keeps running totals and a capped list of recent runs, so you can see what it has been doing without adding any instrumentation.
baker.getMetrics("send-digests");
// {
// totalExecutions: 12,
// successfulExecutions: 11,
// failedExecutions: 1,
// averageExecutionTime: 842,
// lastExecutionTime: 790,
// lastError: "SMTP connection timed out",
// }
baker.getHistory("send-digests");
// [
// { timestamp: Date, duration: 790, success: true, error: undefined },
// { timestamp: Date, duration: 30012, success: false, error: "SMTP connection timed out" },
// ...
// ]Metrics
totalExecutions number successfulExecutions number failedExecutions number averageExecutionTime number lastExecutionTime number | undefined lastError string | undefined skippedExecutions number | undefined Durations include onTick and onError, since both are awaited as part of the run.
History
getHistory() returns one entry for each finished run, newest first.
timestamp Date duration number success boolean error string | undefined A job keeps its last 100 runs by default. Older entries are dropped, but the totals in the metrics keep counting. Change the limit for one job with maxHistory, or for every job on a baker with schedulerConfig.maxHistoryEntries. maxHistory: 0 keeps no history at all.
const baker = Baker.create({
schedulerConfig: { maxHistoryEntries: 20 },
});
baker.add({ name: "chatty", cron: "@every_second", maxHistory: 5, callback });Both getMetrics() and getHistory() return copies, so changing the result won’t affect the job.
A status endpoint
Here’s a small report you could return from a health check or an admin page.
import Baker from "cronbake";
export function jobReport(baker: Baker) {
return baker.getJobNames().map((name) => {
const metrics = baker.getMetrics(name);
const [latest] = baker.getHistory(name);
return {
name,
status: baker.getStatus(name),
nextRun: baker.isRunning(name) ? baker.nextExecution(name) : null,
lastRun: latest?.timestamp ?? null,
lastRunOk: latest?.success ?? null,
failures: metrics.failedExecutions,
skipped: metrics.skippedExecutions ?? 0,
};
});
}Resetting
job.resetMetrics() clears the totals and history for one job, and baker.resetAllMetrics() does it for all of them.
Turning metrics off
Baker.create({ enableMetrics: false }) makes baker.getMetrics() return zeros and baker.getHistory() return an empty list, and it leaves metrics out of the persisted state.
Jobs still record their runs underneath, and you can reach those numbers directly with baker.getAllJobs().get(name)?.getMetrics(). Think of this option as hiding the numbers, not as a way to save work.
Metrics don’t reliably survive a restart. See what persistence keeps before relying on them.