Unlock the Power of Job Scheduling with Bree.js

What is Bree.js?

Bree.js is a robust job scheduler designed specifically for Node.js applications. It allows you to execute code in a scheduled, repetitive manner, making it an essential tool for automating tasks and improving overall system efficiency.

Why Choose Bree.js?

Bree.js stands out from other job schedulers in the Node.js community due to its unique features. One of its key advantages is its use of worker threads, which ensures that background jobs are executed efficiently without blocking the main thread. Additionally, Bree.js provides concurrency, throttling, and retries, making it an ideal choice for handling complex tasks.

Getting Started with Bree.js

To get started with Bree.js, simply install it using npm or yarn, and create a folder called “jobs” in the root directory. This folder will contain all your background jobs. Then, create a job file, for example, “test.js”, and write your logic inside it. Finally, use bree.start() to start the scheduler and run all configured jobs.


// Install Bree.js
npm install bree

// Create a job file (test.js)
const { Bree } = require('bree');

const bree = new Bree({
  jobs: [
    {
      name: 'test',
      path: './test.js',
      interval: '5s'
    }
  ]
});

// Start the scheduler
bree.start();

Scheduling a Job

Scheduling a job with Bree.js is straightforward. You can pass an additional parameter to a job array element, specifying the interval at which the script should run. For example, you can use the interval parameter to run a script every 5 seconds. Bree.js also supports other scheduling options, including timeout and cron, which allow you to run scripts at specific times or intervals.


// Schedule a job to run every 5 seconds
const bree = new Bree({
  jobs: [
    {
      name: 'test',
      path: './test.js',
      interval: '5s'
    }
  ]
});

Passing Data to Jobs

Since Bree.js uses worker threads, you can pass data to a job by using the workerData object. This allows you to share data between the main file and the job file, making it easier to manage complex tasks.


// Pass data to a job
const bree = new Bree({
  jobs: [
    {
      name: 'test',
      path: './test.js',
      workerData: {
        foo: 'bar'
      }
    }
  ]
});

Building a Practical Application

To demonstrate the power of Bree.js, let’s build a Twitter scheduler application. This application will allow users to schedule Tweets at specified times. We’ll use Express.js as our web server, body-parser to parse POST requests, and Twit to interact with the Twitter API.

Scheduler Logic

Our scheduler job will use Cabin.js for logging, and will check for messages from the parent thread. If a “cancel” message is received, the job will be cancelled. We’ll also use Async/Await to connect to the database, retrieve active Tweets, and send them at the scheduled time.


// Scheduler logic
const { Bree } = require('bree');
const Cabin = require('cabin');
const async = require('async');

const bree = new Bree({
  jobs: [
    {
      name: 'twitter',
      path: './twitter.js',
      interval: '1m'
    }
  ]
});

const cabin = new Cabin();

bree.on('twitter', async () => {
  // Connect to the database
  const db = await connectToDatabase();

  // Retrieve active Tweets
  const tweets = await retrieveActiveTweets(db);

  // Send Tweets at the scheduled time
  await sendTweets(tweets);

  // Log success
  cabin.log('info', 'Tweets sent successfully!');
});

Leave a Reply

Your email address will not be published. Required fields are marked *