breejs / bree

Bree is a Node.js and JavaScript job task scheduler with worker threads, cron, Date, and human syntax. Built for @ladjs, @forwardemail, @spamscanner, @cabinjs.

Home Page:https://jobscheduler.net

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Bree throws an error of jobs array is empty when creating the instance

theahmadzai opened this issue · comments

Describe the bug

Bree throws an error if the jobs array when creating object is empty so in my case I load all my jobs config from database and initially the jobs array is empty I add jobs after loading them from database.

  • Node.js version: 16.13.1
  • OS & version: 11

Actual behavior

image

...

Expected behavior

It should not throw error on creating bree object with empty jobs because jobs might be added later via bree.add
...

Code to reproduce

This is the code I use to load jobs and add it to bree

...
const Bree = require('bree')
const knex = require('./knex')
const logger = require('./logger')
const Bugsnag = require('./bugsnag')
const { JOB_ROOT, DATABASE_TABLE_JOBS } = require('../config')

const bree = new Bree({
  logger,
  root: JOB_ROOT,
  jobs: [],
  errorHandler: (error, workerMetadata) => {
    const { threadId, name } = workerMetadata

    if (threadId) {
      logger.error(`Error in thread id: ${threadId}, name: ${name}`)
    }

    logger.error(error)
    Bugsnag.notify(error)
  },
})

bree.on('worker created', (name) => {
  logger.info(`Worker created: ${name}`)
})

bree.on('worker deleted', (name) => {
  logger.info(`Worker deleted: ${name}`)
})

exports.scheduler = bree

exports.start = async () => {
  const jobs = await knex(DATABASE_TABLE_JOBS).where({ is_active: true })

  jobs.forEach(async ({ name, filename, schedule }) => {
    bree.add({
      name,
      path: `${JOB_ROOT}/${filename}`,
      ...schedule,
    })

    logger.info(`Loaded job(${name}) filename(${filename}) schedule(${JSON.stringify(schedule)})`)
  })

  logger.info(`${jobs.length} jobs loaded.`)

  await bree.start()

  return bree
}

Checklist

  • I have read the documentation.
  • I have tried my code with the latest version of Node.js and Bree.

image
if this is empty it throws error and if not then it does not can we please! allow it to not throw error when empty because jobs might be added later after loading

This is expected behavior. It has checked for an index file in JOB_ROOT and found none. However bree has initialized and is running. Perhaps we should provide a better Error message here and an option to either not do this or silence this error.