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 🙏

© 2024 – Pkg Stats / Ryan Hefner

cron-logger

v1.0.0

Published

`cron-logger` is a reusable and extensible package for Node.js that simplifies logging and monitoring for scheduled CRON jobs. It provides advanced features such as pluggable storage, event-driven architecture, retry policies, and batch size tracking, mak

Downloads

58

Readme

cron-logger

cron-logger is a reusable and extensible package for Node.js that simplifies logging and monitoring for scheduled CRON jobs. It provides advanced features such as pluggable storage, event-driven architecture, retry policies, and batch size tracking, making it ideal for microservices and enterprise-grade applications.


Features

  • Pluggable Storage Adapters: Supports MongoDB, PostgreSQL, or custom storage mechanisms.
  • Event-Driven: Emit events on success, failure, and completion for real-time monitoring and alerting.
  • Retry Policies: Automatically retry failed jobs with configurable settings.
  • Batch Size Tracking: Capture metadata about the number of records processed.
  • Flexible Configuration: Environment-based and easily customizable through JSON or code.
  • TypeScript Support: Includes TypeScript definitions for type-safe development.

Installation

Install the package via npm:

npm install cron-logger

Getting Started

Basic Usage

Here's how to use cron-logger in your Node.js application:

Step 1: Import and Configure

const mongoose = require('mongoose');
const CronLogger = require('cron-logger');
const MongoDBAdapter = require('cron-logger/lib/adapters/MongoDBAdapter');
const AuditLog = require('./models/AuditLog'); // Your MongoDB model

mongoose.connect('mongodb://localhost:27017/auditLogs', {
  useNewUrlParser: true,
  useUnifiedTopology: true,
});

// Create a MongoDB storage adapter
const mongoAdapter = new MongoDBAdapter(AuditLog);

// Define your CRON task
const exampleTask = async () => {
  console.log('Processing records...');
  const recordsProcessed = Math.floor(Math.random() * 100);
  if (recordsProcessed === 0) throw new Error('No records processed.');
};

// Define batch size function (optional)
const exampleBatchSizeFn = async () => Math.floor(Math.random() * 100);

// Initialize CronLogger
const cronLogger = new CronLogger({
  cronName: 'Example Job',
  schedule: '*/5 * * * * *', // Run every 5 seconds
  task: exampleTask,
  storageAdapter: mongoAdapter,
  batchSizeFn: exampleBatchSizeFn,
  retryPolicy: { enabled: true, maxRetries: 3, delay: 5000 },
});

// Hook into events
cronLogger.on('success', (log) => console.log(`[${log.cronName}] Task succeeded.`));
cronLogger.on('failure', (log) => console.error(`[${log.cronName}] Task failed: ${log.error}`));
cronLogger.on('complete', (log) => console.log(`[${log.cronName}] Task complete: ${log.executionStatus}`));

// Start the CRON job
cronLogger.start();

Configuration Options

CronLogger Options

| Option | Type | Required | Description | | ------------------ | ------------ | -------- | ------------------------------------------------------------------------- | | cronName | string | Yes | A unique name for the CRON job. | | schedule | string | Yes | CRON expression for scheduling (e.g.,*/5 * * * * *). | | task | function | Yes | The task to be executed by the CRON job. Must return a Promise. | | storageAdapter | object | Yes | An adapter to store audit logs (e.g., MongoDB, PostgreSQL). | | batchSizeFn | function | No | Function to calculate the batch size (e.g., number of records processed). | | retryPolicy | object | No | Configuration for retrying failed tasks (see below). |

Retry Policy

The retryPolicy object allows you to configure automatic retries for failed jobs.

| Option | Type | Default | Description | | -------------- | ----------- | --------- | ------------------------------------------ | | enabled | boolean | false | Whether retries are enabled. | | maxRetries | number | 3 | Maximum number of retries for failed jobs. | | delay | number | 5000 | Delay between retries in milliseconds. |


Storage Adapters

The package supports pluggable storage adapters for logging. Here's how to use the built-in MongoDB adapter or create a custom one.

MongoDB Adapter

Use the provided MongoDBAdapter:

const MongoDBAdapter = require('cron-logger/lib/adapters/MongoDBAdapter');
const AuditLog = require('./models/AuditLog'); // Your MongoDB model

const mongoAdapter = new MongoDBAdapter(AuditLog);

Custom Adapter

Implement the StorageAdapter interface to create your own adapter:

const StorageAdapter = require('cron-logger/lib/StorageAdapter');

class CustomAdapter extends StorageAdapter {
  async log(logData) {
    // Custom logic to store log data
    console.log('CustomAdapter logging data:', logData);
  }
}

module.exports = CustomAdapter;

Events

cron-logger emits the following events for monitoring and alerting:

| Event | Payload | Description | | ------------ | ---------------------------------------------- | -------------------------------------------------------- | | success | { cronName, batchSize } | Emitted when the task completes successfully. | | failure | { cronName, error } | Emitted when the task fails. | | complete | { cronName, executionStatus, endDateTime } | Emitted after the task execution, regardless of outcome. |


TypeScript Support

The package includes TypeScript type definitions. Example usage:

import CronLogger, { CronLoggerOptions } from 'cron-logger';

const options: CronLoggerOptions = {
  cronName: 'Example Job',
  schedule: '*/5 * * * * *',
  task: async () => { console.log('Task executed'); },
  storageAdapter: new MongoDBAdapter(AuditLog),
};
const cronLogger = new CronLogger(options);
cronLogger.start();

Example CRON Schedules

| Expression | Description | | ----------------- | ------------------------------------ | | */5 * * * * * | Every 5 seconds | | 0 */1 * * * * | Every 1 minute | | 0 0 * * * * | Every hour | | 0 0 0 * * * | Every day at midnight | | 0 0 0 1 * * | First day of every month at midnight |


Contributing

We welcome contributions! Please fork the repository, make your changes, and submit a pull request. Ensure your code passes all tests and adheres to the project's coding standards.


License

cron-logger is licensed under the MIT License. See the LICENSE file for details.