1.3.3 • Published 8 months ago

@nodeboot/starter-scheduler v1.3.3

Weekly downloads
-
License
MIT
Repository
github
Last release
8 months ago

šŸ“† @nodeboot/starter-scheduler – Node-Boot Scheduling Starter

Overview

The @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.


✨ Features

āœ… 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.


šŸš€ Installation

Ensure you have Node-Boot installed in your project. Then, install the scheduler starter package:

pnpm add @nodeboot/starter-scheduler

šŸ”„ Usage

1ļøāƒ£ Enable Scheduling in Your Application

To activate the scheduling system, add @EnableScheduling() to your application class:

import {EnableScheduling} from "@nodeboot/starter-scheduler";
import {NodeBootApplication, NodeBoot, ExpressServer} from "@nodeboot/core";

@EnableScheduling()
@NodeBootApplication()
export class SampleApp implements NodeBootApp {
    start(): Promise<NodeBootAppView> {
        return NodeBoot.run(ExpressServer);
    }
}

2ļøāƒ£ Schedule Jobs Using @Scheduler

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

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! šŸ•’


3ļøāƒ£ Understanding Cron Expressions

The @Scheduler decorator follows standard cron syntax:

*    *    *    *    *
│    │    │    │    │
│    │    │    │    └── Day of the Week (0-6, Sunday = 0)
│    │    │    └──── Month (1-12)
│    │    └─────── Day of the Month (1-31)
│    └────────── Hour (0-23)
└──────────── Minute (0-59)
ExpressionMeaning
"* * * * *"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.


šŸ› ļø How It Works Internally

šŸ”¹ @Scheduler(cronExpression: string)

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.

šŸ”¹ @EnableScheduling()

  • Enables automatic scheduling in the application.
  • Registers the Scheduler Adapter into the Node-Boot lifecycle.
  • Ensures all @Scheduler jobs are scheduled at startup.

šŸŽÆ Example: Sending Notifications Every Hour

Imagine you need to send notifications to users every hour. You can achieve this with:

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!


āš ļø Common Issues & Debugging

āŒ @Scheduler Not Running?

āœ”ļø Ensure @EnableScheduling() is added to your application class.
āœ”ļø Verify the cron expression is correct.
āœ”ļø Check the logs to see if the scheduler is registered.


šŸ“š Additional Resources


šŸŽ‰ Conclusion

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! šŸš€šŸŽÆ

1.3.3

8 months ago

1.3.2

8 months ago

1.3.1

10 months ago

1.3.0

10 months ago

1.2.0

11 months ago

1.1.0

11 months ago

1.0.6

11 months ago

1.0.5

11 months ago

1.0.4

11 months ago

1.0.3

11 months ago

1.0.2

12 months ago

1.0.1

12 months ago

1.0.0

12 months ago