Skip to content

About

A Roblox cron-service for clients and servers to create repeating events using cron symbols.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

1 Commit

Folders and files

Repository files navigation

NRBX logo

@nrbx/cron

A lightweight cron-style scheduler for Roblox-TS projects. It lets you register callbacks against cron expressions or shorthand aliases and trigger them from a heartbeat loop.

The package accepts either 5-field expressions (minute hour day-of-month month day-of-week) or 6-field expressions that include seconds at the front. It also includes a set of shorthand values such as @hourly, @daily, and @weekly.

Installation

yarn add @nrbx/cron
npm install @nrbx/cron
pnpm add @nrbx/cron

Then add the package under your Rojo node_modules configuration:

"node_modules": {
  "$className": "Folder",
  "@rbxts": {
    "$path": "node_modules/@rbxts"
  },
  "@nrbx": {
    "$path": "node_modules/@nrbx"
  }
}

And make sure your tsconfig.json includes the package types:

"typeRoots": ["node_modules/@rbxts", "node_modules/@nrbx"]

Quick Start

import { cron, CronShorthand } from "@nrbx/cron";

cron.jobs.add("* * * * *", () => {
	print("Every minute");
});

cron.jobs.add(CronShorthand.Hourly, () => {
	print("Runs every hour");
});

The scheduler uses Roblox's RunService.Heartbeat to check schedules continuously as the game runs.

Cron expression format

This package follows common cron semantics with a few helpful extras:

  • 5-field format: minute hour dayOfMonth month dayOfWeek
  • 6-field format: second minute hour dayOfMonth month dayOfWeek
  • Shorthand aliases: @yearly, @annually, @monthly, @weekly, @daily, @midnight, @hourly, @minutely, @everySecond
  • Lists and ranges are supported, such as 0,30, 1-5, or */15

Examples:

cron.jobs.add("0 * * * *", () => print("Every hour at minute 0"));
cron.jobs.add("0,30 * * * * *", () => print("At second 0 and 30 of every minute"));
cron.jobs.add("30 9 * * 1-5", () => print("Weekdays at 09:30"));
cron.jobs.add(CronShorthand.Daily, () => print("Daily at midnight"));

API

cron.jobs.add(expression, callback)

Registers a new job and returns a unique job ID.

const id = cron.jobs.add("0 8 * * 1-5", () => {
	print("Good morning");
});
Argument Type Description
expression string Cron expression or shorthand alias
callback () => void Function to run when the schedule matches

Returns: string — the job ID.

cron.jobs.remove(id)

Removes a job permanently.

cron.jobs.remove(id);

Returns: boolean — whether a matching job was removed.

cron.jobs.disable(id) / cron.jobs.enable(id)

Pause or resume a registered job without deleting it.

cron.jobs.disable(id);
cron.jobs.enable(id);

cron.jobs.clear()

Remove all registered jobs.

cron.jobs.clear();

cron.jobs.list()

Returns the current job snapshot.

const jobs = cron.jobs.list();
print(jobs.size());

cron.jobs.start() / cron.jobs.stop()

Start or stop the scheduler loop manually.

cron.jobs.stop();
cron.jobs.start();

CronShorthand enum

import { CronShorthand } from "@nrbx/cron";

CronShorthand.Yearly;
CronShorthand.Annually;
CronShorthand.Monthly;
CronShorthand.Weekly;
CronShorthand.Daily;
CronShorthand.Midnight;
CronShorthand.Hourly;
CronShorthand.Minutely;
CronShorthand.EverySecond;

Development

# Install dependencies
yarn install

# Compile with roblox-ts
yarn build

# Watch mode
yarn watch

# Lint + format with Biome
yarn biome

License

MIT


About

A Roblox cron-service for clients and servers to create repeating events using cron symbols.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages