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

@robojs/cron

v0.1.1

Published

Easily schedule and manage recurring tasks.

Downloads

16

Readme


@robojs/cron

Effortlessly schedule tasks with cron expressions. This robust plugin allows you to run functions at specified times, manage job persistence, and control tasks dynamically.

Installation

To add this plugin to your Robo.js project:

npx robo add @robojs/cron

New to Robo.js? Start your project with this plugin pre-installed:

npx create-robo <project-name> -p @robojs/cron

Usage

Use the Cron export to run a function on a schedule.

Cron('*/10 * * * * *', () => {
	console.log('This job runs every 10 seconds!')
})

You can schedule jobs to run when your Robo starts using the /src/events/_start.js file.

import { Cron } from '@robojs/cron'

export default () => {
	Cron('*/10 * * * * *', () => {
		console.log('This job runs every 10 seconds!')
	})
}

Cron returns a job object that can be controlled dynamically.

const job = Cron('*/10 * * * * *', () => {
	console.log('This job runs every 10 seconds!')
})

// Pause a job
job.pause()

// Resume a job
job.resume()

// Stop a job
job.stop()

// Get the next run time
const nextRun = job.nextRun()
console.log('Next run in', nextRun)

Job File

You can also point to a file to run as a job.

import { Cron } from '@robojs/cron'

// File: /src/events/_start.js
export default () => {
	// Runs `/src/cron/job.js` every 5 seconds
	const job = Cron('*/5 * * * * *', '/cron/job.js')
}

Your job file should export a default function.

// File: /src/cron/job.js
export default () => {
	console.log('@robojs/cron is awesome!')
}

Persistence

Jobs can be persisted to ensure they continue after a restart and can be retrieved later.

import { Cron } from '@robojs/cron'

// File: /src/commands/start-job.js
export default () => {
	// Runs `/src/cron/job.js` every 5 seconds
	const job = Cron('*/5 * * * * *', '/cron/job.js')

	// Only file-based jobs can be persisted
	const jobId = await job.save()

	// Retrieve a saved job using its ID
	const savedJob = await Cron.get('my-job')

	// Use the job ID to remove the job later
	await Cron.remove(jobId)
}

This is useful for creating long-running tasks as a result of a command or event. You may pass an ID to the save method to prevent duplication and make it easier to retrieve the job later.

const jobId = await job.save('my-job')
const savedJob = await Cron.get('my-job')

API Overview

Create and initialize a cron job by calling Cron(cronExpression, jobFunction). The cronExpression parameter should be a string that defines when the job will run, such as '*/5 * * * * *' for every 5 seconds.

const job = Cron('*/5 * * * * *', () => {
	console.log('This job runs every 5 seconds!')
})

The jobFunction parameter can be either a path to a JavaScript file that exports a function or a direct function definition.

Job Control

  • pause(): Temporarily halts the cron job.
  • resume(): Resumes a paused cron job.
  • stop(): Permanently stops the cron job from running.

Job Management

  • save(): Persists the job so that it continues to run even after a system restart and can be retrieved later. This method returns a Promise that resolves to the unique identifier for the job.
  • get(id): Retrieves a previously saved job using its ID. Returns a Promise that resolves to the job instance if found.
  • nextRun(): Returns the next scheduled run time of the job as a Date object, or null if there is no scheduled run.

Removing a Job

Remove a persisted job from the system by using Cron.remove(id), where id is the unique identifier returned by the save() method. This function ensures that the job is no longer scheduled to run and is removed from storage.

Cron Expressions

Cron expressions are used to define the schedule of a job. They consist of six fields separated by spaces:

  1. Seconds: 0-59
  2. Minutes: 0-59
  3. Hours: 0-23
  4. Day of Month: 1-31
  5. Month: 1-12
  6. Day of Week: 0-6 (Sunday to Saturday)

Each field can be a single value, a range, a list of values, or an increment. For example:

  • * (asterisk): All values
  • */5: Every 5 units
  • 1,2,3: Specific values
  • 1-5: Range of values
  • 1-5/2: Range with increment

Examples

  • * * * * * *: Every second
  • */5 * * * * *: Every 5 seconds
  • 0 0 * * * *: Every hour
  • 0 0 0 * * *: Every day at midnight
  • 0 0 0 1 * *: First day of every month at midnight
  • 0 0 0 * * 0: Every Sunday at midnight

Learn more about cron expressions from the Cron Expression Generator.