Skip to main content

@hile/schedule

Run cron or delayed jobs, optionally in distributed mode with Redis lock protection.

Choose This Package When

Use When

Use these packages for database connections, Redis connections, structured logging, typed Redis caches, scheduled jobs, and file-system loaders.

Do Not Use When

  • Do not use default TypeORM or Redis services for multiple connections.
  • Do not use @hile/cache without returning new Cache(...) from the loader function.
  • Do not use @hile/schedule distributed mode without Redis lock TTLs sized for job duration.

Install

Imports

Copy-Paste Example

More Examples

TypeORM transaction with compensation:
Distributed scheduled job:

Runtime And Lifecycle Notes

  • Cache(undefined) removes the key unless negative caching is configured.
  • Cache#setExpire(seconds) uses seconds.
  • defineCache typed placeholders support string, number, and boolean.
  • RedisCache.loadCache() returns read, write, remove, has, and multi.
  • RedisCache.removeTag(tag) removes tagged cache entries.
  • fieldable caches use Redis hashes and cannot combine with stale or negative cache options.
  • Scheduler.add() supports cron strings and { delay }.
  • Scheduler.load() reads default exports from *.schedule.* files produced by defineJob().
  • scanDirectory() matches .ts, .js, .tsx, .jsx, and .mjs.
  • scanDirectory() only parses and returns the structured route when routing consumers opt in with fileRoutes: true; that AST covers the file-relative route while routePath retains the configured prefix. This keeps router-native prefixes out of the portable file-name DSL and preserves non-routing loaders’ filename contracts. .d.ts and source-map files do not match.
  • parseFileRoute() accepts static, [id], and final required [...paths] segments, and rejects duplicate or object-meta dynamic names. compileFileRoute() targets either find-my-way or rou3, compiles configured native or bracket-style prefixes separately, and rejects parameter-name collisions across the complete route.

Anti-Patterns

  • Returning raw values from defineCache handlers instead of new Cache(value).
  • Treating cache as source of truth.
  • Forgetting to destroy manually created Redis or TypeORM clients.
  • Scheduling jobs without idempotency when side effects can repeat.

Verification Checklist

  • Manual Redis clients call disconnect() during cleanup.
  • Manual TypeORM data sources call destroy() during cleanup.
  • Cache keys include app/tenant prefixes when shared Redis is used.
  • Scheduled jobs have clear duplicate-run policy.

Queue Worker With Idempotency

Complete Example

Enqueue:

Package-Local AI Guide

This package also ships AI.md in npm so agents can read accurate examples after installation.