Giter VIP home page Giter VIP logo

Comments (5)

Hexagon avatar Hexagon commented on June 3, 2024 1

Yep, I'll consider adding a feature like this, just got to figure out how to make is as usable as possible 👍

from croner.

Hexagon avatar Hexagon commented on June 3, 2024

Hi!

That's actually by design, both previousRun() and currentRun() only return the time of an actual trigger. There is currently not way to list previous matches, but there is an discussion started at #219

from croner.

Earu avatar Earu commented on June 3, 2024

I see. Any idea on how id compute the previous run then?

or in the same vein, figure out if a job was run the same day?

from croner.

Hexagon avatar Hexagon commented on June 3, 2024

You should be able to do something like

import Cron from 'https://esm.run/[email protected]';
const job = new Cron("0 0 11 * * *"); // 11:00:00 every day
const today = new Date(new Date().setHours(0,0,0,0)); // (hours, minutes, seconds, milliseconds)
const firstRunToday = job.nextRun(today);
const earlierTriggerToday = firstRunToday && firstRunToday < new Date();
const laterTriggerToday = job.nextRun() && (job.nextRun().setHours(0,0,0,0) == today.getTime());
console.log(earlierTriggerToday, laterTriggerToday);

Edit (making a bit more modular):

import Cron from 'https://esm.run/[email protected]';

// Function to check if the cron job has triggered earlier today
function hasTriggeredEarlierToday(job) {
    const today = new Date(new Date().setHours(0,0,0,0)); // Start of the current day
    const firstRunToday = job.nextRun(today);

    return firstRunToday && firstRunToday < new Date();
}

// Function to check if the cron job will trigger later today
function willTriggerLaterToday(job) {
    const today = new Date(new Date().setHours(0,0,0,0)); // Start of the current day
    const nextRun = job.nextRun();

    return nextRun && (nextRun.setHours(0,0,0,0) == today.getTime());
}

// Example usage
const job = new Cron("0 0 11 * * *"); // 11:00:00 every day
const earlierTriggerToday = hasTriggeredEarlierToday(job);
const laterTriggerToday = willTriggerLaterToday(job);

console.log('Has triggered earlier today:', earlierTriggerToday);
console.log('Will trigger later today:', laterTriggerToday);

from croner.

Earu avatar Earu commented on June 3, 2024

Thanks ! That's more or less what I was trying to achieve, do you thinks maybe a similar function could make its way into a future version ? I'm sure I'm not the only one having a use case for checking whether a cron expr is gonna run/has run on a specific date (in my case today).

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.