Class: Fractor::PriorityWork

Inherits:
Work
  • Object
show all
Defined in:
lib/fractor/priority_work.rb

Overview

PriorityWork extends Work with priority levels for priority-based scheduling

Priority levels:

  • :critical - Highest priority, processed first
  • :high - High priority
  • :normal - Default priority (backward compatible)
  • :low - Low priority
  • :background - Lowest priority

Examples:

Creating priority work

work = Fractor::PriorityWork.new(data: "urgent task", priority: :high)

Using default priority

work = Fractor::PriorityWork.new(data: "normal task")
work.priority # => :normal

Constant Summary collapse

PRIORITY_LEVELS =
{
  critical: 0,
  high: 1,
  normal: 2,
  low: 3,
  background: 4,
}.freeze

Instance Attribute Summary collapse

Attributes inherited from Work

#input, #timeout

Instance Method Summary collapse

Methods inherited from Work

#inspect, #to_s

Constructor Details

#initialize(input, priority: :normal) ⇒ PriorityWork

Initialize a new PriorityWork

Parameters:

  • input (Object)

    The input data for the work

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

    Priority level (:critical, :high, :normal, :low, :background)

Raises:

  • (ArgumentError)

    if priority is not a valid level



35
36
37
38
39
40
# File 'lib/fractor/priority_work.rb', line 35

def initialize(input, priority: :normal)
  super(input)
  validate_priority!(priority)
  @priority = priority
  @created_at = Time.now
end

Instance Attribute Details

#created_atObject (readonly)

Returns the value of attribute created_at.



28
29
30
# File 'lib/fractor/priority_work.rb', line 28

def created_at
  @created_at
end

#priorityObject (readonly)

Returns the value of attribute priority.



28
29
30
# File 'lib/fractor/priority_work.rb', line 28

def priority
  @priority
end

Instance Method Details

#<=>(other) ⇒ Integer

Compare priorities for sorting Lower priority value = higher priority For same priority, older work comes first (FIFO within priority)

Parameters:

Returns:

  • (Integer)

    -1, 0, or 1 for comparison



62
63
64
65
66
67
68
69
70
71
# File 'lib/fractor/priority_work.rb', line 62

def <=>(other)
  return nil unless other.is_a?(PriorityWork)

  # First compare by priority value
  result = priority_value <=> other.priority_value
  return result unless result.zero?

  # If same priority, use FIFO (older first)
  created_at <=> other.created_at
end

#ageFloat

Calculate age in seconds (used for priority aging)

Returns:

  • (Float)

    Age in seconds since creation



52
53
54
# File 'lib/fractor/priority_work.rb', line 52

def age
  Time.now - @created_at
end

#higher_priority_than?(other) ⇒ Boolean

Check if this work has higher priority than another

Parameters:

Returns:

  • (Boolean)

    true if this work has higher priority



77
78
79
80
81
# File 'lib/fractor/priority_work.rb', line 77

def higher_priority_than?(other)
  return false unless other.is_a?(PriorityWork)

  priority_value < other.priority_value
end

#priority_valueInteger

Get numeric priority value (lower is higher priority)

Returns:

  • (Integer)

    Numeric priority (0-4)



45
46
47
# File 'lib/fractor/priority_work.rb', line 45

def priority_value
  PRIORITY_LEVELS[@priority]
end