Class CarbonAwarePeriod

java.lang.Object
org.jobrunr.scheduling.carbonaware.CarbonAwarePeriod
All Implemented Interfaces:
Temporal, TemporalAccessor

public class CarbonAwarePeriod extends Object implements Temporal
Represents a period of time in which a job may be scheduled to minimize carbon emissions.

A CarbonAwarePeriod is constructed using the CarbonAware utility class. It defines a flexible scheduling window that can be centered around a point in time, start from now until a deadline, or be bounded by a custom range. The actual execution time within this window is determined by a carbon-aware scheduler.

Note: The scheduling window must currently span at least 3 hours. This is due to the underlying carbon intensity data being available only at an hourly resolution.

Example usages:

// Schedule before a specific deadline
CarbonAwarePeriod beforeDeadline = CarbonAware.before(Instant.parse("2025-07-01T08:00:00Z"));

// Schedule between two custom times
CarbonAwarePeriod betweenTimes = CarbonAware.between(
    Instant.parse("2025-07-01T00:00:00Z"),
    Instant.parse("2025-07-01T08:00:00Z")
);

// Schedule around a target time with flexibility
CarbonAwarePeriod flexible = CarbonAware.at(
    Instant.parse("2025-07-01T04:00:00Z"),
    Duration.ofHours(4)
);

NOTE: CarbonAwarePeriod implements Temporal as a marker interface. All temporal methods currently throw UnsupportedOperationException and are not intended for direct time manipulation.

  • Method Details

    • getFrom

      public Instant getFrom()
    • getTo

      public Instant getTo()
    • before

      public static CarbonAwarePeriod before(Temporal to)
      Allows to relax the scheduling of a job to minimize carbon impact. The job will run before the provided to Temporal instance.
      Parameters:
      to - the time expressed in java.time.Temporal before which the job must be scheduled.
      Returns:
      A carbon aware period between Instant.now() and the provided to.
    • between

      public static CarbonAwarePeriod between(Temporal from, Temporal to)
      Allows to relax the scheduling of a job to minimize carbon impact. The job will run between the two provided from and to Temporal instances as the interval.
      Parameters:
      from - the start time expressed in Temporal of the carbon aware margin.
      to - the end time expressed in Temporal of the carbon aware margin.
      Returns:
      A carbon aware period between the provided from and to.
    • isSupported

      public boolean isSupported(TemporalUnit unit)
      Specified by:
      isSupported in interface Temporal
    • with

      public Temporal with(TemporalField field, long newValue)
      Specified by:
      with in interface Temporal
    • plus

      public Temporal plus(long amountToAdd, TemporalUnit unit)
      Specified by:
      plus in interface Temporal
    • until

      public long until(Temporal endExclusive, TemporalUnit unit)
      Specified by:
      until in interface Temporal
    • isSupported

      public boolean isSupported(TemporalField field)
      Specified by:
      isSupported in interface TemporalAccessor
    • getLong

      public long getLong(TemporalField field)
      Specified by:
      getLong in interface TemporalAccessor