Module: Legion::LLM::Scheduling

Extended by:
Legion::Logging::Helper
Defined in:
lib/legion/llm/scheduling.rb

Constant Summary collapse

DEFAULT_PEAK_RANGE =

Default peak hours in UTC: 14:00-22:00 (9 AM - 5 PM CT)

(14..22)
DEFAULT_DEFER_INTENTS =

Intents that are eligible for deferral during peak hours.

%i[batch background maintenance].freeze

Class Method Summary collapse

Class Method Details

.enabled?Boolean

Returns true when off-peak scheduling is enabled in settings.

Returns:

  • (Boolean)


17
18
19
# File 'lib/legion/llm/scheduling.rb', line 17

def enabled?
  settings.fetch(:enabled, false) == true
end

.next_off_peak(time = Time.now.utc) ⇒ Time

Returns the next off-peak time as a Time object (UTC). Off-peak begins at the hour after the peak window ends.

Returns:

  • (Time)

    next off-peak start time



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/legion/llm/scheduling.rb', line 45

def next_off_peak(time = Time.now.utc)
  now = time.is_a?(Time) ? time : Time.now.utc
  peak_end = peak_range.last
  max_defer = settings.fetch(:max_defer_hours, 8)

  next_time = if peak_hours?(now)
                # During peak — next off-peak is at peak_end + 1
                candidate = Time.utc(now.year, now.month, now.day, peak_end + 1, 0, 0)
                candidate += 86_400 if candidate <= now
                candidate
              else
                # Already off-peak — return now
                now
              end

  # Cap at max_defer_hours from now
  cap = now + (max_defer * 3600)
  [next_time, cap].min
end

.peak_hours?(time = Time.now.utc) ⇒ Boolean

Returns true if the given UTC hour falls within the configured peak window.

Returns:

  • (Boolean)


36
37
38
39
# File 'lib/legion/llm/scheduling.rb', line 36

def peak_hours?(time = Time.now.utc)
  hour = time.is_a?(Time) ? time.hour : Time.now.utc.hour
  peak_range.cover?(hour)
end

.should_defer?(intent: :normal, urgency: :normal) ⇒ Boolean

Determines whether a request should be deferred to off-peak hours.

Parameters:

  • intent (Symbol, String) (defaults to: :normal)

    the request intent

  • urgency (Symbol) (defaults to: :normal)

    :immediate bypasses deferral regardless of settings

Returns:

  • (Boolean)


26
27
28
29
30
31
32
33
# File 'lib/legion/llm/scheduling.rb', line 26

def should_defer?(intent: :normal, urgency: :normal)
  return false unless enabled?
  return false if urgency.to_sym == :immediate

  result = eligible_for_deferral?(intent.to_sym) && peak_hours?
  log.debug("Scheduling defer decision intent=#{intent} urgency=#{urgency} defer=#{result}")
  result
end

.statusObject

Returns a hash summarizing current scheduling state.



66
67
68
69
70
71
72
73
74
75
# File 'lib/legion/llm/scheduling.rb', line 66

def status
  {
    enabled:         enabled?,
    peak_hours:      peak_hours?,
    peak_range:      peak_range.to_s,
    next_off_peak:   peak_hours? ? next_off_peak.iso8601 : 'now',
    defer_intents:   defer_intents,
    max_defer_hours: settings.fetch(:max_defer_hours, 8)
  }
end