Gathering detailed insights and metrics for clujo
Gathering detailed insights and metrics for clujo
Gathering detailed insights and metrics for clujo
Gathering detailed insights and metrics for clujo
Schedule functions on a cron-like schedule. Built in distributed locking to prevent overlapping executions in a clustered environment.
npm install clujo
Typescript
Module System
Node Version
NPM Version
TypeScript (100%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
4 Stars
171 Commits
1 Watchers
1 Branches
1 Contributors
Updated on Jun 14, 2025
Latest Version
2.0.3
Package Id
clujo@2.0.3
Unpacked Size
401.26 kB
Size
58.10 kB
File Count
45
NPM Version
10.8.3
Node Version
22.9.0
Published on
Oct 25, 2024
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
2
Clujo is a flexible solution for managing scheduled tasks in your distributed Node.js / Deno applications. It would not be possible without the amazing work of the following projects:
ioredis
instance is provided to start method) - used to ensure single execution in a distributed environmentComing soon: bun support.
IMPORTANT: Clujo is now published under @ramplex/clujo
on npm and jsr. If you are using the old clujo
package, please update your dependencies to use @ramplex/clujo
instead. All future versions will be published under the new package name.
runOnStartup
feature, Clujo allows you to run tasks immediately when needed, in addition to their scheduled times.Clujo is available on jsr and npm, and supports Node.js and Deno v2.0.0 or later.
Install Clujo using npm, pnpm, yarn:
1npm install @ramplex/clujo 2yarn add @ramplex/clujo 3pnpm install @ramplex/clujo
1deno add jsr:@ramplex/clujo
1npx jsr add @ramplex/clujo 2yarn dlx jsr add @ramplex/clujo 3pnpm dlx jsr add @ramplex/clujo
Here's a simple example to get you started with Clujo:
1import { TaskGraph, Clujo } from '@ramplex/clujo'; 2 3// Define your tasks 4const tasks = new TaskGraph({ 5 // Optional: provide initial context value 6 contextValue: { initialData: "some value" }, 7 // Optional: provide dependencies available to all tasks 8 dependencies: { logger: console } 9}) 10 .addTask({ 11 id: "task1", 12 execute: async ({ deps, ctx }) => { 13 deps.logger.log("Task 1 executing"); 14 deps.logger.log("Initial data:", ctx.initial.initialData); 15 return "Task 1 result"; 16 }, 17 }) 18 .addTask({ 19 id: "task2", 20 execute: async ({ deps, ctx }) => { 21 deps.logger.log("Task 2 executing"); 22 // since task2 depends on task1, it will have access to the result of task1 23 deps.logger.log("Task 1 result:", ctx.task1); 24 return "Task 2 result"; 25 }, 26 // will only execute after task 1 completes 27 dependencies: ["task1"], 28 }) 29 .addTask({ 30 id: "task3", 31 execute: async ({ deps, ctx }) => { 32 deps.logger.log("Task 3 executing"); 33 return "Task 3 result"; 34 }, 35 // since task3 has no dependencies, it will run in parallel with task1 at the start of execution 36 }) 37 .build(); 38 39// Create a Clujo instance 40const clujo = new Clujo({ 41 id: "myClujoJob", 42 cron: { 43 pattern: "*/5 * * * * *", 44 }, 45 taskGraphRunner: tasks, 46}); 47 48// Start the job 49clujo.start({ 50 // Optional: provide an ioredis client for distributed locking 51 redis: { client: new Redis() }, 52 // Optional: run the job immediately on startup 53 runOnStartup: true, 54 // Optional: provide a callback to run when the job completes that takes in the completed context object 55 onTaskCompletion: (ctx) => console.log(ctx), 56}); 57 58// Trigger the job manually to get a complete context object 59const completedContext = await clujo.trigger(); 60 61// Gracefully stop the job by waiting until the current execution completes 62// Will force stop after timeout milliseconds 63await clujo.stop(timeoutMs);
In the event a Javascript Date
object is provided instead of a cron pattern, the task graph
will be executed precisely once at the specific date/time specified. Time is in ISO 8601 local time.
The context object contains the appropriate context for the task.
i
depends on tasks j_1,...,j_n
, then it can be guaranteed the context object will have the result of tasks j_1,...,j_n
under the keys j_1,...,j_n
. The value at these keys is the return of task j_i
, i = 1,...,n
.i-1
, i=1,...,N
. All tasks run sequentially1 <= i != j <= N
. N tasks where task i
depends on task j
. N\{i}
tasks run concurrently, task i
runs after task j
.i
depends on task j
, task j
depends on task i
. Cyclic dependencies will result in an error pre-execution.In the event a task execution fails, all further dependent tasks will not be executed. Other independent tasks will continue to run.
Can build up more complex cases from these simple cases
1// Using a static context value 2const tasks = new TaskGraph({ 3 contextValue: { users: [], config: {} }, 4 dependencies: { logger: console } 5}) 6 .addTask({ 7 id: "task1", 8 execute: ({ deps, ctx }) => { 9 deps.logger.log(ctx.initial.users); 10 return "result"; 11 } 12 }) 13 .build(); 14 15// Using an (sync or async) context factory 16const tasks = new TaskGraph({ 17 contextFactory: async (deps) => { 18 const users = await fetchUsers(); 19 return { users }; 20 }, 21 dependencies: { logger: console } 22}) 23 .addTask({ 24 id: "task1", 25 execute: ({ deps, ctx }) => { 26 deps.logger.log(ctx.initial.users); 27 return "result"; 28 } 29 }) 30 .build();
The context and dependencies are type-safe, ensuring you can only access properties that actually exist.
Tasks can access their dependencies' results through the context object, and all tasks have access to the initial context under ctx.initial
.
When an ioredis
client is provided, Clujo will use it to acquire a lock for each task execution. This ensures that tasks are not executed concurrently in a distributed environment.
1import Redis from 'ioredis'; 2 3const redis = new Redis(); 4 5clujo.start({ 6 redis: { 7 client: redis, 8 lockOptions: { /* optional lock options */ } 9 } 10});
You can run tasks immediately when the job starts by setting the runOnStartup
option to true
.
The triggered execution will prevent a scheduled execution from running at the same time in the event
the scheduled execution overlaps with the triggered execution.
1clujo.start({ 2 runOnStartup: true 3});
Tasks can have their own error handlers, allowing you to define custom logic for handling failures. The function can be synchronous or asynchronous, and has access to the same context as the execute function.
1.addTask({ 2 id: "taskWithErrorHandler", 3 execute: async ({ deps, ctx }) => { 4 // Task logic 5 }, 6 errorHandler: async (error, { deps, ctx }) => { 7 console.error("Task failed:", error); 8 } 9})
Specify a retry policy for a task to automatically retry failed executions. The task will be retried up to maxRetries
times, with a delay of retryDelayMs
between each retry.
1.addTask({ 2 id: "taskWithRetry", 3 execute: async ({ deps, ctx }) => { 4 // Task logic 5 }, 6 retryPolicy: { maxRetries: 3, retryDelayMs: 1000 } 7})
The Scheduler class provides a convenient way to manage multiple Clujo jobs together. It allows you to add, start, and stop groups of jobs in a centralized manner.
1import { Scheduler } from '@ramplex/clujo'; 2import { Redis } from 'ioredis'; 3 4const scheduler = new Scheduler(); 5 6// Add jobs to the scheduler 7scheduler.addJob({ 8 job: myFirstClujoJob, 9 // Optional completion handler for this clujo 10 completionHandler: async (ctx) => { 11 console.log('First job completed with context:', ctx); 12 } 13}); 14 15scheduler.addJob({ 16 job: mySecondClujoJob 17}); 18 19// Add more jobs as needed
You can start all added jobs at once, optionally providing a Redis instance for distributed locking:
1const redis = new Redis(); // Your Redis configuration 2 3// Start all jobs without distributed locking 4scheduler.start(); 5 6// Or, start all jobs with distributed locking 7scheduler.start(redis);
To stop all running jobs:
1// Stop all jobs with a default timeout of 5000ms 2await scheduler.stop(); 3 4// Or, specify a custom timeout in milliseconds 5await scheduler.stop(10000);
Contributions are welcome! Please describe the contribution in an issue before submitting a pull request. Attach the issue number to the pull request description and include tests for new features / bug fixes.
Clujo is MIT licensed.
No vulnerabilities found.
No security vulnerabilities found.