npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@job-kit/core

v0.1.0

Published

Trigger-agnostic, storage-agnostic job lifecycle engine — zero runtime dependencies.

Readme

@job-kit/core

A production-ready, trigger-agnostic, storage-agnostic job lifecycle engine. Zero runtime dependencies. Designed to be attached directly to a host framework (strapi.jobEngine = engine) and driven or inspected from a UI: engine.state, engine.pause(), engine.inFlightCount, etc.

Philosophy

  • Names developers already recognize: onError, onCompleted, onClaimed, retry, limits, lease.
  • Every hook receives a rich context object — including a reference to the engine itself, so a hook (an audit log, a metrics collector, a UI panel) can inspect or control the engine without needing it wired in separately.
  • Config is scoped by domain (limits, lease, retry, recovery, hooks) instead of a flat list of parameters.
  • Every config leaf may be a getter, so it can be sourced from env vars, a config service, or a feature flag — the engine reads these live on every cycle, never caches them.
  • The engine is a small state machine you can observe (engine.state), pause (engine.pause()), resume (engine.resume()), and query (engine.inFlightCount).

Quick usage

import { JobEngine, exponentialBackoff } from '@job-kit/core';

const engine = new JobEngine({
  jobType: 'payment-webhook',
  storage: myStorageAdapter,
  execute: async (job) => {
    // job.data is your domain payload; job.leaseToken proves you own this attempt.
    await chargeCard(job.data);
  },
  limits: {
    get maxConcurrency() {
      return featureFlags.get('payment-webhook.concurrency') ?? 10;
    },
    batchSize: 50,
  },
  lease: {
    durationMs: 30_000,
    heartbeatIntervalMs: 10_000,
  },
  retry: {
    policy: { maxAttempts: 5, backoffMs: exponentialBackoff(1_000, 60_000) },
    onError: async (ctx) => {
      if (ctx.error.name === 'ValidationError') return 'dead_letter';
      if (ctx.error.name === 'CardDeclined') return 'fail';
      return 'retry';
    },
  },
  recovery: {
    staleIntervalMs: 60_000,
  },
  hooks: {
    onClaimed: (ctx) => auditLog.write('claimed', ctx),
    onStarted: (ctx) => metrics.timer(`${ctx.jobType}.duration`).start(ctx.idempotencyKey),
    onCompleted: (ctx) => metrics.timer(`${ctx.jobType}.duration`).stop(ctx.idempotencyKey),
    onFailed: (ctx) => alerting.page(`${ctx.jobType} failed`, ctx),
    onDeadLetter: (ctx) => archive.store(ctx),
    onEngineError: (error, info) => logger.error(error, info),
  },
});

// Attach to a host framework and let its lifecycle drive the trigger.
strapi.jobEngine = engine;

// Any trigger just calls tick(). This example uses an interval, but this
// package has no idea an interval is being used — a Kafka consumer, a
// RabbitMQ handler, a cron tick, an HTTP endpoint, or a "run now" button
// in an admin UI would all just call the same method.
setInterval(() => void engine.tick(), 500);

// Run once at startup, and periodically (see recovery.staleIntervalMs),
// to reclaim work orphaned by a crash.
await engine.recoverStale();
setInterval(() => void engine.recoverStale(), 60_000);

// From a UI or shutdown hook:
engine.pause(); // stop claiming new work, let in-flight work finish
engine.resume(); // start claiming again
await engine.shutdown(10_000); // stop for good, on SIGTERM/app shutdown

Config reference

| Scope | Field | Default | Notes | | ---------- | ---------------------------------------------------------------------------------------------------------------- | ------------------------------- | ----------------------------------------------------- | | limits | maxConcurrency | 10 | In-flight execution cap for this engine instance | | limits | batchSize | 50 | Candidates fetched per cycle | | lease | durationMs | 30_000 | How long a claim is valid before it's stale | | lease | heartbeatIntervalMs | off | Renews the lease while executing; omit for short jobs | | retry | policy | 5 attempts, exponential backoff | { maxAttempts, backoffMs(attempt) } | | retry | onError | always 'retry' | Returns 'retry' \| 'fail' \| 'dead_letter' | | recovery | staleIntervalMs | 60_000 | Informational — you own the timer, see below | | recovery | batchSize | 100 | Stale jobs recovered per recoverStale() call | | hooks | onClaimed/onStarted/onCompleted/onFailed/onDeadLetter/onRetryScheduled/onRecovered/onEngineError | none | All optional, all isolated from crashing the engine |

Every field above can be written as a getter:

limits: {
   get maxConcurrency() {
      return process.env.JOB_CONCURRENCY ? Number(process.env.JOB_CONCURRENCY) : 10;
   },
},

The state machine

active ──pause()──► paused ──resume()──► active
   │                                        │
   └──────────────── shutdown() ───────────►┤
                                             ▼
                                        shutting_down ──► stopped
  • active — the default; tick() claims and executes work.
  • pausedtick() is a no-op; in-flight work keeps running to completion. Resumable.
  • shutting_downstopped — terminal. shutdown(gracePeriodMs) waits for in-flight work, then releases anything still running so another worker (or this one, after a restart) can pick it up immediately rather than waiting out the lease.

engine.state, engine.inFlightCount are readable at any time — the intended basis for a UI status panel.

Retry verdicts

retry.onError(ctx) returns one of:

  • 'retry' — try again later, subject to retry.policy.maxAttempts. If attempts are exhausted, the engine treats this the same as 'fail' — it does not silently promote it to 'dead_letter'; only an explicit 'dead_letter' verdict routes there.
  • 'fail' — terminal now. Fires hooks.onFailed.
  • 'dead_letter' — terminal, routed to hooks.onDeadLetter instead of onFailed, for errors that need archival/manual review rather than an alert.

If retry.onError is omitted, every error is treated as 'retry' (matching the historical default of "retry everything").

What's intentionally NOT in this package

  • Any storage technology (Strapi, SQL, Redis...) — implement JobStorage.
  • Any trigger mechanism (interval, Kafka, RabbitMQ, cron, HTTP) — implement Trigger, or just call engine.tick() from wherever.
  • Timers of its own. recovery.staleIntervalMs is a documented convention for you to wire up (setInterval, a cron job, a Kafka consumer on a schedule topic...) — the engine takes no timer dependency.
  • True idempotency of side effects (e.g. "never charge a card twice") — the engine guarantees duplicate execution is rare and detectable via lease fencing; use ctx.idempotencyKey to make your own side effects idempotent where it matters.

Package layout

src/
  types.ts     JobStorage/Trigger contracts, ManagedJob, scoped config interfaces
  context.ts   staged per-attempt context, EngineHandle, JobEngineHooks
  errors.ts    error serialization + hook-failure isolation
  engine.ts    JobEngine — the state machine + lifecycle logic
  index.ts     public exports
test/
  inMemoryStorage.ts   in-memory JobStorage, for testing the engine ONLY
  engine.test.ts       28 tests

Known limitation

hooks.onCancelled is defined in the JobEngineHooks type for API completeness, but is not currently fired by the engine: cancellation (storage.cancel(...)) happens outside the claim flow the engine observes (a cancelled pending job simply stops appearing in findEligible results), so there's no natural point in today's engine where it would detect the transition to raise the hook. Firing it would require either a dedicated cancellation-scan (mirroring recoverStale()) or a storage-contract addition — left as an open question rather than implemented speculatively.