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.
https://jobscheduler.net
MIT License
2.96k stars 80 forks source link

[fix] Cron last day of the month incorrect behaviour #210

Open Giacomo-R opened 1 year ago

Giacomo-R commented 1 year ago

Describe the bug

Node.js version: 18.12.1

OS version: Windows 11

Description: I am trying to create a job that runs every month 8 days before the last day of the month, however the L-N° returned is wrong, I have tested using the late.parse.cron() function imported from "@breejs/later" and it returns in fact a wrong date.

Actual behavior

Yesterday, when I actually wrote it, it seemed to be working just fine, however today when I started the code below and it began starting in loop the process, even changing any other value of the cron doesn't make the difference and any value above 8 cause the same issue, anything below doesn't begin the loop but still gives incorrect date

I have tried printing the next 12 sheduled running times from "@breejs/later", code and output below, considering that the current date/time is "2023-01-20T10:44:30.086Z", so it simply prints the current time and date:

Code:

var cronSched = later.parse.cron('* * L-8 * *')
let next = later.schedule(cronSched).next(12)
console.log(next)

Result:

[
  2023-01-20T10:44:30.086Z,
  2023-01-20T10:44:30.086Z,
  2023-01-20T10:44:30.086Z,
  2023-01-20T10:44:30.086Z,
  2023-01-20T10:44:30.086Z,
  2023-01-20T10:44:30.086Z,
  2023-01-20T10:44:30.086Z,
  2023-01-20T10:44:30.086Z,
  2023-01-20T10:44:30.086Z,
  2023-01-20T10:44:30.086Z,
  2023-01-20T10:44:30.086Z,
  2023-01-20T10:44:30.086Z
]

I have tried also to run a cron using "L-1" in the month field, it caused the following behaviour, which is still wrong because L alone is 31:

Code:

var cronSched = later.parse.cron('* * L-1 * *')
let next = later.schedule(cronSched).next(12)
console.log(next)

Result:

[
  2023-01-27T00:00:00.000Z,
  2023-01-27T00:00:00.000Z,
  2023-01-27T00:00:00.000Z,
  2023-01-27T00:00:00.000Z,
  2023-01-27T00:00:00.000Z,
  2023-01-27T00:00:00.000Z,
  2023-01-27T00:00:00.000Z,
  2023-01-27T00:00:00.000Z,
  2023-01-27T00:00:00.000Z,
  2023-01-27T00:00:00.000Z,
  2023-01-27T00:00:00.000Z,
  2023-01-27T00:00:00.000Z
]

Code to reproduce

import Bree from "bree"
import Cabin from "cabin"

const bree = new Bree({
    logger: new Cabin(),
    jobs: [
        {
            name: "dailyImport",
            cron: "* * L-8 * *", 

            cronValidate: {
                override: {
                    useLastDayOfMonth: true
                }
            }
        },
    ]
})

await bree.start()

Checklist