Code Monkey home page Code Monkey logo

Comments (3)

jmrozanec avatar jmrozanec commented on May 31, 2024

@greenlaw110 thank you for reporting this!
Could you please provide the cron expression you are using to execute the code, and perhaps a test that reproduces the issue? Thank you in advance!

from cron-utils.

greenlaw110 avatar greenlaw110 commented on May 31, 2024

Actually any cron expression lead to the same result. That's because the pattern I've used to applying cron-util. As you see schedule method is executed right at the time cron task should invoke (actually just invoked), thus you want to get the timeToNextExecution exactly when it's due to execution, the library returns 0, which is the timeToCurrentExecution. I end up with something like following to workaround the issue:

void schedule(AppJobManager manager, _Job job) {
    DateTime now = DateTime.now();
    ExecutionTime executionTime = ExecutionTime.forCron(cron);
    Duration nextExecution = executionTime.timeToNextExecution(now.withDurationAdded(1, 1));
    long seconds = nextExecution.getStandardSeconds();
    int times = 1;
    while (seconds == 0) {
        nextExecution = executionTime.timeToNextExecution(now.withDurationAdded(1, times++));
        seconds = nextExecution.getStandardSeconds();
    }
    manager.executor().schedule(job, seconds, TimeUnit.SECONDS);
}

from cron-utils.

jmrozanec avatar jmrozanec commented on May 31, 2024

@greenlaw110 I tried to reproduce the issue, but I do not get the issue.
If you have too short intervals, then you will get zero seconds; depending on the cron expression you consider. But if you invoke nextExecution.getMillis(), you may notice that time to next execution decreases accurately.

Also looking at different cron setups, returns accurate results, where Duration interval to next execution is not zero seconds. Example: using the following code, you get a non-zero value most of time.

ExecutionTime executionTime = ExecutionTime.forCron(quartzCronParser.parse("5 * * * * *"));
DateTime now = DateTime.now();
Duration nextExecution = executionTime.timeToNextExecution(now);
long seconds = nextExecution.getStandardSeconds();

Please validate and let us know the result. Will be happy to help on it.

from cron-utils.

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.