Node-Boot starter package for schedulers
npm install @nodeboot/starter-scheduler@nodeboot/starter-scheduler ā Node-Boot Scheduling StarterThe @nodeboot/starter-scheduler package provides a simple yet powerful mechanism to schedule jobs within a Node-Boot application, similar to how Spring Boot Scheduler works.
It leverages node-cron under the hood to execute scheduled tasks at specified intervals based on cron expressions.
With minimal configuration, developers can automatically trigger functions within beans using the @Scheduler decorator.
---
ā
Annotation-based scheduling ā Just add @Scheduler(cronExpression) to any bean method.
ā
Cron-based execution ā Supports flexible scheduling using cron expressions.
ā
Lifecycle-aware ā Scheduling starts when the application initializes.
ā
Minimal setup ā Requires only @EnableScheduling to activate.
---
Ensure you have Node-Boot installed in your project. Then, install the scheduler starter package:
``sh`
pnpm add @nodeboot/starter-scheduler
---
To activate the scheduling system, add @EnableScheduling() to your application class:
`typescript
import {EnableScheduling} from "@nodeboot/starter-scheduler";
import {NodeBootApplication, NodeBoot, ExpressServer} from "@nodeboot/core";
@EnableScheduling()
@NodeBootApplication()
export class SampleApp implements NodeBootApp {
start(): Promise
return NodeBoot.run(ExpressServer);
}
}
`
---
To schedule a method to run at a specific interval, add the @Scheduler decorator to a method inside a bean (@Service, @Component, etc.).
The method will automatically run based on the cron expression provided.
#### Example: Run a Task Every Minute
`typescript
import {Scheduler} from "@nodeboot/starter-scheduler";
import {Service} from "@nodeboot/core";
@Service()
export class TaskService {
@Scheduler(" *") // Runs every minute
public logMessage() {
console.log(Task executed at: ${new Date().toISOString()});`
}
}
ā This will log a message every minute! š
---
The @Scheduler decorator follows standard cron syntax:
`plaintext`
*
ā ā ā ā ā
ā ā ā ā āāā Day of the Week (0-6, Sunday = 0)
ā ā ā āāāāā Month (1-12)
ā ā āāāāāāāā Day of the Month (1-31)
ā āāāāāāāāāāā Hour (0-23)
āāāāāāāāāāāāā Minute (0-59)
| Expression | Meaning |
| --------------- | --------------------------------- |
| " *" | Runs every minute |"0 "
| | Runs every hour (at 0 min) |"0 0 *"
| | Runs daily at midnight |"0 0 1"
| | Runs every Monday at midnight |"/5 *"
| | Runs every 5 minutes |
For more advanced cron expressions, refer to node-cron documentation.
---
This decorator:
- Registers the method as a scheduled job.
- Uses the Adapter Pattern to integrate with the Node-Boot lifecycle.
- Triggers execution based on the cron expression.
- Enables automatic scheduling in the application.
- Registers the Scheduler Adapter into the Node-Boot lifecycle.
- Ensures all @Scheduler jobs are scheduled at startup.
---
Imagine you need to send notifications to users every hour. You can achieve this with:
`typescript
import {Scheduler} from "@nodeboot/starter-scheduler";
import {Service} from "@nodeboot/core";
@Service()
export class NotificationService {
@Scheduler("0 ") // Runs at the start of every hour
public sendNotifications() {
console.log(š¢ Sending notifications to users at ${new Date().toISOString()});`
}
}
š This will trigger notifications every hour, automatically!
---
āļø Ensure @EnableScheduling() is added to your application class.
āļø Verify the cron expression is correct.
āļø Check the logs to see if the scheduler is registered.
---
- Node-Cron Documentation
- Understanding Cron Expressions
- Node-Boot Framework
---
The @nodeboot/starter-scheduler package makes scheduling effortless and declarative within Node-Boot applications.
By simply adding @Scheduler(cronExpression)`, you can automate periodic tasks, without manually managing timers.
Happy scheduling! ššÆ