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 🙏

© 2025 – Pkg Stats / Ryan Hefner

timecrisis-sqlite

v0.1.1

Published

A SQLite storage adapter for the Time Crisis job scheduler

Readme

Time Crisis SQLite Storage Adapter

npm version License: MIT

SQLite storage adapter for Time Crisis job scheduler. Provides a production-ready, durable storage backend using SQLite.

Installation

npm install timecrisis timecrisis-sqlite better-sqlite3

Features

  • 💾 Production-ready SQLite storage implementation
  • 🔄 Automatic schema migrations
  • 🔒 Transaction support
  • 🚀 Efficient job querying and locking
  • 🧹 Built-in cleanup for old jobs

Usage

Basic Setup

import Database from 'better-sqlite3';
import { JobScheduler } from 'timecrisis';
import { SQLiteJobStorage } from 'timecrisis-sqlite';

// Create SQLite connection
const db = new Database('jobs.sqlite');

// Enable WAL mode for better concurrency
db.pragma('journal_mode = WAL');
db.pragma('busy_timeout = 5000');

// Create storage instance
const storage = new SQLiteJobStorage(db);
await storage.init(); // This runs migrations

// Create scheduler
const scheduler = new JobScheduler({
  storage,
  node: 'worker-1',
  pollInterval: 1000,
  maxConcurrentJobs: 5,
});

// Start scheduler
await scheduler.start();

Production Configuration

For production use, configure SQLite for reliability:

const db = new Database('jobs.sqlite', {
  // Verbose error messages
  verbose: process.env.NODE_ENV === 'development',

  // File system synchronization level
  fileMustExist: false,
});

// Performance and reliability settings
db.pragma('journal_mode = WAL');
db.pragma('synchronous = NORMAL');
db.pragma('busy_timeout = 5000');
db.pragma('foreign_keys = ON');
db.pragma('temp_store = MEMORY');

Cleanup Configuration

Configure job retention periods:

// Cleanup old jobs periodically
await storage.cleanup({
  // Keep completed jobs for 7 days
  jobRetention: 7,

  // Keep failed jobs for 14 days
  failedJobRetention: 14,

  // Keep dead letter jobs for 30 days
  deadLetterRetention: 30,
});

Metrics

Get storage-specific metrics:

const metrics = await storage.getMetrics();
console.log(metrics);
// {
//   jobs: {
//     total: 1000,
//     pending: 5,
//     completed: 900,
//     failed: 95,
//     deadLetter: 10,
//     scheduled: 50
//   },
//   averageDurationByType: {
//     'send-email': 245,
//     'process-video': 15000
//   },
//   failureRateByType: {
//     'send-email': 0.02,
//     'process-video': 0.15
//   }
// }

Multiple Workers

The storage adapter supports multiple workers accessing the same database through SQLite's WAL mode and distributed locking:

// Worker 1
const worker1 = new JobScheduler({
  storage: new SQLiteJobStorage(db1),
  node: 'worker-1',
  // Keep lock TTL short for faster failover
  jobLockTTL: 30000,
  leaderLockTTL: 30000,
});

// Worker 2
const worker2 = new JobScheduler({
  storage: new SQLiteJobStorage(db2),
  node: 'worker-2',
  jobLockTTL: 30000,
  leaderLockTTL: 30000,
});

Database Maintenance

Regular maintenance tasks:

// Vacuum database to reclaim space
db.pragma('vacuum');

// Analyze tables for query optimization
db.pragma('analyze');

// Optimize WAL file
db.pragma('wal_checkpoint(TRUNCATE)');

// Backup database
const backup = new Database('backup.sqlite');
db.backup(backup).then(() => {
  backup.close();
});

Error Handling

The adapter provides detailed error types:

try {
  await storage.createJob(job);
} catch (error) {
  if (error instanceof JobNotFoundError) {
    // Handle missing job
  } else if (error instanceof JobRunNotFoundError) {
    // Handle missing job run
  } else if (error instanceof ScheduledJobNotFoundError) {
    // Handle missing scheduled job
  }
}

Transactions

All operations use transactions for consistency:

await storage.transaction(async (trx) => {
  const jobId = await storage.createJob(job);
  await storage.createJobLog({
    jobId,
    level: 'info',
    message: 'Job created',
  });
});

Performance Tips

WAL Configuration:

// Larger WAL file for better write performance
db.pragma('page_size = 4096');
db.pragma('wal_autocheckpoint = 1000');

Contributing

See the main repository for contribution guidelines.