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

@nendlabs/cloc

v0.0.2

Published

a framework for running background tasks using intervals or cron expressions

Readme

@nendlabs/cloc

A lightweight framework for running and managing background tasks on a schedule. Ideal for scenarios like periodic maintenance, data syncing, or any recurring task in your application.

Installation

npm install @nendlabs/cloc

Quick Start

This example demonstrates how to set up a simple task with a logging dependency:

import { cloc } from '@nendlabs/cloc';
import { Logger } from './logger';

// Define your dependencies
declare global {
  namespace Cloc {
    interface Dependencies {
      logger: ReturnType<(typeof Logger)['createChild']>;
    }
  }
}

// Initialize cloc
cloc({
  tasks: {
    greet: {
      intervalMs: 5000,
      fn: async ({ logger }) => {
        logger.info('Hello from cloc!');
      },
    },
  },
  dependencies: {
    logger: Logger.createChild({ namespace: 'my-app' }),
  },
});

Configuration

The cloc function accepts the following configuration options. Choose the ones that best suit your use case:

interface Config {
  // Define tasks inline
  tasks?: Record<string, Cloc.Task>;
  
  // OR specify a directory containing task files
  tasksDir?: string;
  
  // Dependencies to inject into tasks
  dependencies: Cloc.Dependencies;
  
  // Optional dependency transformer
  transformDependencies?: (
    task: Cloc.Task,
    dependencies: Cloc.Dependencies
  ) => Cloc.Dependencies;
  
  // Optional telemetry configuration
  telemetry?: {
    enabled: boolean;
    project: string;
  };
}

Tasks

Defining Tasks

Tasks can be defined in one of two ways:

Inline Tasks

Inline tasks are directly defined in your configuration object:

cloc({
  tasks: {
    taskName: {
      intervalMs: 5000,
      fn: async (deps) => {
        // Your task logic here
      },
    },
  },
  dependencies: {
    // Your Task function dependencies here
  },
});

File-based Tasks

Organize tasks in separate files for better modularity and scalability:

// tasks/my-task.ts
export default {
  intervalMs: 5000,
  fn: async ({ logger }) => {
    logger.info('Running my task');
  },
} as Cloc.Task;

Then initialize cloc with the tasks directory:

cloc({
  tasksDir: './tasks',
  dependencies: {
    // Your dependencies here
  },
});

Task Dependencies

Set up type-safe dependencies by extending the global Cloc.Dependencies interface. This ensures your tasks have access to the right tools:

import { PrismaClient } from '@prisma/client';

declare global {
  namespace Cloc {
    interface Dependencies {
      prisma: PrismaClient;
      config: ApplicationConfig;
      // Add more dependencies as needed
    }
  }
}

These dependencies will be injected into the task functions and provided to cloc during instantiation.

Transforming Dependencies

Adapt dependencies for specific tasks using a transformer function:

cloc({
  dependencies: {
    logger: Logger.createChild({ namespace: 'my-app' }),
  },
  transformDependencies: (task, dependencies) => {
    // Transform dependencies for this task
    const taskLogger = dependencies.logger.createChild({ namespace: task.name });
    return { ...dependencies, logger: taskLogger };
  },
});

Telemetry

Enable Prometheus-based telemetry metrics using prom-client to monitor task performance:

cloc({
  // ... other config
  telemetry: {
    enabled: true,
    project: 'my-project',
  },
});

Cloc collects the following metrics using Prometheus-compatible instruments:

  • Active Tasks (Gauge): Tracks the number of tasks currently running.
  • Task Execution Count (Counter): Counts how many times tasks have been executed.
  • Task Execution Duration (Histogram): Measures the duration of task executions in milliseconds.
  • Task Failures (Counter): Tracks the number of task executions that resulted in errors.

Usage

These metrics help to:

  1. Monitor Activity: Use the active gauge to understand task concurrency in real-time.
  2. Track Performance: Analyze the duration histogram to identify slow-running tasks.
  3. Measure Reliability: Use the failure counter to monitor task stability and troubleshoot errors.
  4. Audit Frequency: Leverage the count counter to verify task schedules and execution consistency.

Integrate these insights into your monitoring system to enhance system reliability and performance.