Giter Club home page Giter Club logo

Comments (6)

Hexagon avatar Hexagon commented on May 14, 2024

Interesting!

Could you pass and print new Date().toISOString() to matchWildcard, to get the exact internal time of each execution.

Something like

const dayjs = require('dayjs');
const Cron = require("croner");

//Simple match wildcard function like: "aa" will match "a*" or "*a" or "**" but not "bb" or "b*"
function matchWildcard(str1, str2, /*+*/ nowIso) {
    console.debug(nowIso); // +
    if (typeof str1 != "string" || typeof str2 != "string")
        return false;
    if (str1.length != str2.length)
        return false;
    for (let i = 0; i < str1.length; i++) {
        if (str1.at(i) != "*" && str2.at(i) != "*" && str1.at(i) != str2.at(i)) {
            console.debug(`${str1} matchs ${str2} ?: FALSE`);
            return false;
        }
    }
    console.debug(`${str1} matchs ${str2} ?: TRUE`);
    return true;
}

//My cron task running every minute
const job = Cron("0 */1 * ? * *", async () => {
    let now = dayjs(),
          nowIso = new Date().toISOString(); // +
    let dayKey = now.format("HH:mm.DD-MM");
    matchWildcard("12:00.27-**", dayKey, /* + */ nowIso);
});

Also, did you just change the date once, and after that croner started maing multiple double runs? Which version of Node?

from croner.

oom- avatar oom- commented on May 14, 2024

Ok so I updated the code with a little extra line and info:

const dayjs = require('dayjs');
const Cron = require("croner");

let counter = 0;

//Simple match wildcard function like: "aa" will match "a*" or "*a" or "**" but not "bb" or "b*"
function matchWildcard(str1, str2, /*+*/ nowIso) {
    console.debug("[NOWISO]:", nowIso); // +
    if (typeof str1 != "string" || typeof str2 != "string")
        return false;
    if (str1.length != str2.length)
        return false;
    for (let i = 0; i < str1.length; i++) {
        if (str1.at(i) != "*" && str2.at(i) != "*" && str1.at(i) != str2.at(i)) {
            console.debug(`${str1} matchs ${str2} ?: FALSE`);
            return false;
        }
    }
    console.debug(`${str1} matchs ${str2} ?: TRUE`);
    return true;
}

//My cron task running every minute
const job = Cron("0 */1 * ? * *", async () => {
    console.log(`---------------------------NEW CALL: ${counter++}--------------------------`)
    let now = dayjs(),
    nowIso = new Date().toISOString(); 
    let dayKey = now.format("HH:mm.DD-MM");
    matchWildcard("12:00.27-**", dayKey,  nowIso);
});

Seems that now even without trying to type date set I get 2 calls :

0|main  | 2022-05-30T09:09:00: ---------------------------NEW CALL: 0--------------------------
0|main  | 2022-05-30T09:09:00: [NOWISO]: 2022-05-30T09:09:00.006Z
0|main  | 2022-05-30T09:09:00: 12:00.27-** matchs 09:09.30-05 ?: FALSE
0|main  | 2022-05-30T09:09:59: ---------------------------NEW CALL: 1--------------------------
0|main  | 2022-05-30T09:09:59: [NOWISO]: 2022-05-30T09:09:59.995Z
0|main  | 2022-05-30T09:09:59: 12:00.27-** matchs 09:09.30-05 ?: FALSE
0|main  | 2022-05-30T09:10:00: ---------------------------NEW CALL: 2--------------------------
0|main  | 2022-05-30T09:10:00: [NOWISO]: 2022-05-30T09:10:00.002Z
0|main  | 2022-05-30T09:10:00: 12:00.27-** matchs 09:10.30-05 ?: FALSE
0|main  | 2022-05-30T09:10:59: ---------------------------NEW CALL: 3--------------------------
0|main  | 2022-05-30T09:10:59: [NOWISO]: 2022-05-30T09:10:59.996Z
0|main  | 2022-05-30T09:10:59: 12:00.27-** matchs 09:10.30-05 ?: FALSE
0|main  | 2022-05-30T09:11:00: ---------------------------NEW CALL: 4--------------------------
0|main  | 2022-05-30T09:11:00: [NOWISO]: 2022-05-30T09:11:00.001Z
0|main  | 2022-05-30T09:11:00: 12:00.27-** matchs 09:11.30-05 ?: FALSE

As you can see the CALL 15 and CALL 16 are following each other at 1 sec.
Docker_Desktop_IlUTUFidv4

I do not have the issue if pm2 stop all and restart it with node main.js directly. :/
Edit: version of node is node:16.15-bullseye-slim

from croner.

Hexagon avatar Hexagon commented on May 14, 2024

It seems like the clock in docker runs too fast, which makes the trigger run to early. Then, as the target time is not yet reached, it makes a new schedule for one second later, and runs again.

Do you have the same problem running with node/pm2 on host machine? I can not replicate the behavior in any of the setups (host, pm2+host, pm2+docker). So it seems to be related to a specific version of something.

However, I've published a new version (4.3.12) which supports time travel. Give it a try :)

from croner.

Hexagon avatar Hexagon commented on May 14, 2024

Reopen if the problem persist

from croner.

Hexagon avatar Hexagon commented on May 14, 2024

Fix caused more problems than it solved, so we're back to square one on this one

from croner.

Hexagon avatar Hexagon commented on May 14, 2024

@oom- Should be resolved in 4.3.14, please verify

from croner.

Related Issues (20)

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.