Coder Social home page Coder Social logo

p4sca1 / cron-schedule Goto Github PK

View Code? Open in Web Editor NEW
172.0 2.0 14.0 459.17 MB

A zero-dependency cron parser and scheduler for Node.js, Deno and the browser.

License: MIT License

TypeScript 100.00%
cron schedule typescript job parser interval iterator deno javascript timer

cron-schedule's People

Contributors

airfooox avatar buffcode avatar p4sca1 avatar renovate-bot avatar renovate[bot] avatar semantic-release-bot avatar sky0hunter avatar tehpsalmist avatar zachsents avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

cron-schedule's Issues

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

circleci
.circleci/config.yml
  • node 5.2.0
  • cimg/node 20.16
github-actions
.github/workflows/codeql-analysis.yml
  • actions/checkout v4
  • github/codeql-action v3
  • github/codeql-action v3
  • github/codeql-action v3
npm
package.json
  • @biomejs/biome 1.8.3
  • @types/node 20.14.13
  • rimraf 6.0.1
  • semantic-release 24.0.0
  • typescript 5.5.4
  • vitest 2.0.4
  • node >=18
  • pnpm 9.6.0+sha512.38dc6fba8dba35b39340b9700112c2fe1e12f10b17134715a4aa98ccf7bb035e76fd981cf0bb384dfa98f8d6af5481c2bef2f4266a24bfa20c34eb7147ce0b5e

  • Check this box to trigger a request for Renovate to run again on this repository

Cron.matchDate returns always true when using weekdays and days of month

Because of this line in matchDate: (this.days.indexOf(day) !== -1 || this.weekdays.indexOf(weekday) !== -1) an expression like * * * * * 0,6 (Only on Saturday's or Sunday's) will always return true since indexOf(day) will always return true. I believe correct would be using logical AND instead of OR.

Thanks for the package, just what I was looking for. This behaviour is not hard to overwrite, just letting you know πŸ‘

Ref:

(this.days.indexOf(day) !== -1 || this.weekdays.indexOf(weekday) !== -1)

year support is needed

got error for cron expression '* * * * * ? *'
Invalid cron expression: expected 5 or 6 elements.
spring scheduler can support that well

Error on every weekday

code:

  const cron = parseCronExpression('0 12 10 ? * THU *');
  console.log("next date: " + cron.getNextDate());

error:

  Error: Invalid cron expression: expected 5 or 6 elements.
  at K (C:\Dev\otros\nautioPupeter\node_modules\cron-schedule\src\cron-parser.ts:178:11)
  at Object.<anonymous> (C:\Dev\otros\nautioPupeter\index.ts:125:33)
  at Module._compile (node:internal/modules/cjs/loader:1101:14)
  at Module.m._compile (C:\Dev\otros\nautioPupeter\node_modules\ts-node\src\index.ts:1371:23)
  at Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
  at Object.require.extensions.<computed> [as .ts] (C:\Dev\otros\nautioPupeter\node_modules\ts-node\src\index.ts:1374:12)
  at Module.load (node:internal/modules/cjs/loader:981:32)
  at Function.Module._load (node:internal/modules/cjs/loader:822:12)
  at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
  at main (C:\Dev\otros\nautioPupeter\node_modules\ts-node\src\bin.ts:331:12)

Incorrect values returned for getPrevDate

Hi,
When running this code:

let schedule = parseCronExpression('0 10 * * 3')
wedSched.getPrevDate(new Date('2024-05-01T09:00:00'))

expected result is: 2024-04-24T00:00:00.000Z
actual result is: 2024-03-27T00:00:00.000Z

Node: 20.12.2

Seconds are rounded to 0

Hi,

It seems I run into some issue or I am doing something wrong.

I have the following code:

var cron = require('cron-schedule')
const time = cron.parseCronExpression('*/2 * * * *')
console.log(time.getNextDate(new Date()))

This cron should give me 2 minutes after the current date, but somehow the seconds are always rounded to 0. Why is that?
It doesn't take into calculation seconds at all. My guess if you set the date to 2021-01-10 18:12:35.293 you should get 2021-01-10 18:14:35.293 but instead you get the following (seconds rounded to 0).

image

Cron iterator

Have a cron iterator method which returns an interator that can get next and prev dates using .prev and .next.

Error handling for scheduled tasks

Timer based scheduler:
When a task throws an error at one time, it will no longer execute in the future as it is not rescheduled.

Interval based scheduler:
An error in one task causes all other tasks scheduled to be executed at the same time to not execute.

Instead we should catch errors, call some kind of error callback (optional) and continue as normal.

The automated release is failing 🚨

🚨 The automated release from the master branch failed. 🚨

I recommend you give this issue a high priority, so other packages depending on you could benefit from your bug fixes and new features.

You can find below the list of errors reported by semantic-release. Each one of them has to be resolved in order to automatically publish your package. I’m sure you can resolve this πŸ’ͺ.

Errors are usually caused by a misconfiguration or an authentication problem. With each error reported below you will find explanation and guidance to help you to resolve it.

Once all the errors are resolved, semantic-release will release your package the next time you push a commit to the master branch. You can also manually restart the failed CI job that runs semantic-release.

If you are not sure how to resolve this, here is some links that can help you:

If those don’t help, or if this issue is reporting something you think isn’t right, you can always ask the humans behind semantic-release.


Invalid npm token.

The npm token configured in the NPM_TOKEN environment variable must be a valid token allowing to publish to the registry https://registry.npmjs.org/.

If you are using Two-Factor Authentication, make configure the auth-only level is supported. semantic-release cannot publish with the default auth-and-writes level.

Please make sure to set the NPM_TOKEN environment variable in your CI with the exact value of the npm token.


Good luck with your project ✨

Your semantic-release bot πŸ“¦πŸš€

Cron Schedule Manager

Do you think an optional manager could be implemented? Such as this one (https://github.com/cfurst/CronJobManager), which is aimed at the cron package?

I think the implementation could be really simple and it would improve a lot with environments with multiple cron jobs.

I can help if you need.

Cron Job Task Issue

Hi @P4sca1
It seems the task of the scheduler runs twice when the cron time is due. Is there anything i am doing wrong?

function job(){
 console.log('run')
}

const cron = parseCronExpression('*/15 * * * *');

scheduler.setInterval(cron, job);

When i run the code above it executes the function job twice every 15 minutes

es2022 vs es2022 targeting?

One thing I noticed, while looking at v4.0.0-next.3, in the tsconfig is that you are targeting es2022, though I am wondering whether targeting es2020 would be better, given that the code is meant to support running in browser?

Safari for example seems to not be quite es2022 compliant: https://caniuse.com/?search=ES13

Parsing "@monthly" returns a months array of [0]

Like title says. Other macros like @hourly and @daily work correctly.

See this snippet:

import { parseCronExpression } from "cron-schedule"

parseCronExpression("@monthly").months
// [ 0 ]

parseCronExpression("0 0 1 * *").months
// [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 ]

parseCronExpression("@hourly").months
// [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 ]

this package could not be loaded into a non-babel, native esm node.js environment

this package could not be loaded into a non-babel, native esm node.js environment

this appears in the process log,

(node:6368) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)
/path/to/my/project/node_modules/cron-schedule/dist/cron-schedule.esm.min.js:1

Build issue for scheduler import, with Vite

The documentation indicates the following for the import for the scheduler:

import { IntervalBasedCronScheduler as scheduler } from 'cron-schedule/schedulers/interval-based.js'

Though when I do this in VS Code it says it cant' find it. The following works though, but then breaks in execution of the app:

import { IntervalBasedCronScheduler as scheduler } from 'cron-schedule/dist/schedulers/interval-based.js'

Originally I though this was a documentation issue, but now I am wondering whether the packaging has some issues, since if I use cron-schedule/schedulers/interval-based.js dev runtime works, build build time fails.

Error output:

src/views/MagicScreen.vue:45:54 - error TS2307: Cannot find module 'cron-schedule/schedulers/timer-based.js' or its corresponding type declarations.

45 import { TimerBasedCronScheduler as scheduler } from 'cron-schedule/schedulers/timer-based.js';
                                                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

My import is presented as follows in my Vue file:

import { parseCronExpression } from 'cron-schedule';
import { TimerBasedCronScheduler as scheduler } from 'cron-schedule/schedulers/timer-based.js';

I am using Vite 3.2.3 for packaging.

Looking at issue #284, but yarn add [email protected] -S didn't change anything.

Add timezone support

At the moment, the cron jobs are checked against the local time.
When constructing a Schedule an optional timezone parameter could be used.
parseCronExpression would then get a second parameter timezone which is passed to the Schedule constructor.
For ease of use, it would be great if time zone names would be supported, but we would need to include and maintain a database of timezones. Also time zone offsets are not static but change over time and throughout the year. Maybe the timezone parameter should be a function utcOffset that returns the desired utc offset. One could then use other libraries such as moment-timezone to get the desired offset and cron-schedule bundle size would not increased (or use a peer dependency?). We also need to consider what happens when utcOffset returns diifferent offsets in subsequent calls (which might be realistic due to summer time / DST).

Incomplete parsing of ranges (n-n, n-[n+1])

Given the following input:

parseCronExpression('* 1-2 * * *');

the following exception is thrown:

Failed to parse 1-2: Invalid range (start: 1, end: 2).

Though of course it can be rewritten to 1,2 this libraries claims to conform to the linux cron whose man page states:

Ranges are two numbers separated with a hyphen. The specified range is inclusive. [...]
The first number must be less than or equal to the second one.
(https://man7.org/linux/man-pages/man5/crontab.5.html)

The following input should be allowed (but is not at the moment):

  • 1-1
  • 1-2

The automated release is failing 🚨

🚨 The automated release from the master branch failed. 🚨

I recommend you give this issue a high priority, so other packages depending on you can benefit from your bug fixes and new features again.

You can find below the list of errors reported by semantic-release. Each one of them has to be resolved in order to automatically publish your package. I’m sure you can fix this πŸ’ͺ.

Errors are usually caused by a misconfiguration or an authentication problem. With each error reported below you will find explanation and guidance to help you to resolve it.

Once all the errors are resolved, semantic-release will release your package the next time you push a commit to the master branch. You can also manually restart the failed CI job that runs semantic-release.

If you are not sure how to resolve this, here are some links that can help you:

If those don’t help, or if this issue is reporting something you think isn’t right, you can always ask the humans behind semantic-release.


Invalid npm token.

The npm token configured in the NPM_TOKEN environment variable must be a valid token allowing to publish to the registry https://registry.npmjs.org/.

If you are using Two Factor Authentication for your account, set its level to "Authorization only" in your account settings. semantic-release cannot publish with the default "
Authorization and writes" level.

Please make sure to set the NPM_TOKEN environment variable in your CI with the exact value of the npm token.


Good luck with your project ✨

Your semantic-release bot πŸ“¦πŸš€

Weekday range to sunday edge case

Heyho,

there is a little bug when using a range for weekdays which includes sunday as '7', due to the replacement with '0'.

I.e.: 5 10 * * 6-7 will throw Failed to parse 6-7: Invalid range (start: 6, end: 0).

Issue. with Quarterly Cron Schedules

Hi

This may be a problem with one of your dependancies, not the project itself, as https://crontab.guru seems to have the same problem.

However a sample of the issue.

Start Date of a cron is: 16th May 2021
Cron Expression is: 0 0 16 */3 *

The next list of dates are.
next at 2021-07-16 00:00:00
then at 2021-10-16 00:00:00
then at 2022-01-16 00:00:00
then at 2022-04-16 00:00:00
then at 2022-07-16 00:00:00

This is wrong, it should be August, November, February etc.

If you switch it to 2 or 4, you get correct list of dates.

Regards
Charles

Type definitions can fail to load when loading in a 'esmodule-mode' project

Using this library when Typescript is configured with moduleResolution: "NodeNext", type definitions can fail to load. This is resolved by including the type definitions in the exports map of package.json.

"exports": {
"require": "./dist/cron-schedule.cjs.min.js",
"import": "./dist/cron-schedule.min.mjs"
},

The following patch fixes it:

{
...
  "exports": {
    "require": "./dist/cron-schedule.cjs.min.js",
-   "import": "./dist/cron-schedule.min.mjs"
+   "import": "./dist/cron-schedule.min.mjs",
+   "types": "./dist/index.d.ts"
  },
...
}

Love your library!

Confusion on Scheduling a Cron Task

Hi,
I don't see in the Readme how to schedule a task that repeats based on a CRON string. I tried to schedule a task to run every 5 seconds. But it doesn't really work. It runs once only. It says there are only two schedulers , timeout and interval. But I don't want either, I want a schedular to run a task continously based on a cron string. Just like cron. What's the secret?

import { parseCronExpression, TimerBasedCronScheduler as scheduler } from 'cron-schedule'
const cron = parseCronExpression('*/5 * * * *')

scheduler.setTimeout(cron, () => { // This doesn't feel right, but also only runs once
console.log(new Date(), 'TASK EXECUTED')
})

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.