Class Schedule

java.lang.Object
org.jobrunr.scheduling.Schedule
All Implemented Interfaces:
Comparable<Schedule>
Direct Known Subclasses:
CronExpression, CustomSchedule, Interval, ScheduleWrapper

public abstract class Schedule extends Object implements Comparable<Schedule>
  • Field Details

  • Constructor Details

    • Schedule

      protected Schedule()
    • Schedule

      protected Schedule(String scheduleWithOptionalCarbonAwareScheduleMargin)
  • Method Details

    • next

      @VisibleFor("testing") public abstract Instant next(Instant createdAtInstant, Instant currentInstant, ZoneId zoneId)
      Calculates the next occurrence based on the creation time and the provided base time.
      Parameters:
      createdAtInstant - Instant object when the schedule was first created
      currentInstant - Instant object used to calculate the next occurrence (normally Instant.now()).
      zoneId - the zone for which to calculate the schedule
      Returns:
      Instant of the next occurrence.
    • asString

      public String asString()
      Returns the string representation of the schedule. The Schedule must be convertible back and from the String.
      Returns:
      the string representation of the schedule.
    • toHumanReadableString

      public String toHumanReadableString()
      A human-readable string of this Schedule. This value is displayed as the schedule expression on the Dashboard.

      Note that the returned value must not be null.

      Use case

      It's particularly interesting to override this method when implementing a CustomSchedule whose schedule expression are harder to read. Note that cron expressions are already converted to human-readable strings, therefore, if your implementation is composed of multiple CronExpression, it may be sufficient to return toString() of the active one.
      Returns:
      the human-readable string, must be non-null.
    • getExpression

      public String getExpression()
    • getCarbonAwareScheduleMargin

      public CarbonAwareScheduleMargin getCarbonAwareScheduleMargin()
    • isCarbonAware

      public boolean isCarbonAware()
    • isNotCarbonAware

      public boolean isNotCarbonAware()
    • isRunningMoreThanOncePerMinute

      public boolean isRunningMoreThanOncePerMinute()
      Determines whether this schedule triggers more frequently than once per minute.
      Returns:
      true if this schedule will return intervals smaller than a minute (e.g., every 15 seconds or every 30 seconds), false otherwise.
    • durationBetweenSchedules

      public final Duration durationBetweenSchedules()
      Calculates the duration between two consecutive scheduled executions.

      This method evaluates the schedule by computing the next execution time twice: once from a fixed base timestamp, and again from the first computed run time. The difference between these two instants represents the schedule's recurring interval.

      If the schedule does not produce a second execution time (i.e., the schedule ends after the first run), the method returns ChronoUnit.FOREVER, indicating that no further executions are scheduled.

      Returns:
      the Duration between the first and second scheduled run times, or a very large duration representing FOREVER if no second run exists.
      Throws:
      IllegalArgumentException - if the schedule implementation does not comply with the required contract of the next() method.
    • validate

      public void validate()
    • compareTo

      public int compareTo(Schedule schedule)
      Compare two Schedule objects based on next occurrence.

      The next occurrences are calculated based on the current time.

      Specified by:
      compareTo in interface Comparable<Schedule>
      Parameters:
      schedule - the Schedule to be compared.
      Returns:
      the value 0 if this Schedule next occurrence is equal to the argument Schedule next occurrence; a value less than 0 if this Schedule next occurrence is before the argument Schedule next occurrence; and a value greater than 0 if this Schedule next occurrence is after the argument Schedule next occurrence.
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object