Introduction
The Bree package allows us to easily create and manage jobs that are executed on a schedule. It also provides support for adding and removing jobs, as well as tracking their status. In addition, it allows for scheduling jobs that will run on specific days or times, making it an ideal solution for reminder apps.
In this technical blog, we will explore how to create a Job scheduler app using Node.js and the Bree npm package. The Job scheduler app will start new jobs at 9:00 am every day and allow for the addition and removal of already initialized jobs. We will leverage the power of Bree, a job scheduler for Node.js and JavaScript, to achieve this functionality
Prerequisites
Before we begin, make sure you have Node.js and npm installed on your machine. You can check if they are installed by running the following commands in your terminal:
node -v
npm -v
If you don't have Node.js and npm installed, you can download them from the official Node.js website: https://nodejs.org/
Setting up the project
- Create a new directory for your project and navigate to it in your terminal:
mkdir job-scheduler
cd job-scheduler
- Initialize a new Node.js project by running the following command:
npm init
- Install the Bree npm package:
npm install bree
- Create a new folder called jobs in the root directory of your project. This folder will contain all the background jobs
mkdir jobs
Creating the job scheduler app
- Create a new file called job-scheduler.js in the root directory of your project and add the following code:
const Bree = require('bree');
const bree = new Bree({
jobs: [
{
name: 'start-new-jobs',
timeout: 'at 09:00 am',
interval: 'at 09:00 am',
path: './jobs/start-new-jobs.js',
},
],
});
bree.start();
In this code, we import the Bree package and create a new instance of the Bree scheduler. We configure the scheduler to run a job called start-new-jobs at 09:00 am every day. The job is defined in a separate file called start-new-jobs.js in the jobs folder.
- Create a new file called start-new-jobs.js in the jobs folder and add the following code
console.log('Starting new jobs...');
// Add your logic for starting new jobs here
This file represents the job that will be executed at 09:00 am every day. You can add your own logic for starting new jobs instead the console.log statement.
Starting the job scheduler
To start the job scheduler, run the following command in your terminal:
node job-scheduler.js
You should see the message "Starting new jobs..." printed in the console at 09:00 am every day, indicating that the job is being executed successfully.
Adding and removing jobs
To add and remove jobs dynamically, we can use the Bree API. Here's an example of how to add a new job:
- In the job-scheduler.js file, add the following code after the bree.start() statement:
bree.add({
name: 'new-job',
timeout: 'every 1 hour',
path: './jobs/new-job.js',
});
bree.start('new-job')
This code adds a new job called new-job that will run every 1 hour. The job is defined in a separate file called new-job.js in the jobs folder. It is necessary to start the newly added job as bree is already initialized, otherwise it will not consider a new one..
- Create a new file called new-job.js in the jobs folder and add the following code:
console.log('Running new job...');
// Add your logic for the new job here
This file represents the new job that will be executed every 1 hour. You can add your own logic for the new job instead the console.log statement.
To remove a job, you can use the bree.remove() method. Here's an example of how to remove the new-job:
bree.remove('new-job');
Conclusion
In this blog, we have learned how to create a job scheduler using Node.js and the Bree npm package. We started new jobs at 09:00 am every day, added a new job, and removed an already initialized job. Bree provides a powerful and flexible job scheduling solution for Node.js applications, making it easy to manage and execute background tasks
Top comments (2)
Nice content
Nice content