Class: Zeridion::Flare::Worker::Registry

Inherits:
Object
  • Object
show all
Defined in:
lib/zeridion_flare/worker/registry.rb

Overview

The job registry. Two roles:

1. A process-global accumulator (Registry.register_payload /
 register_recurring, called from the Job / RecurringJob `included`
 hooks) that records every job class as it is defined — no
 ObjectSpace scan.

2. A per-Worker lookup instance (Registry.new(definitions)) that maps
 job_type → JobDefinition and exposes the distinct queues / job_types
 for poll + register.

Duplicate job_type at registration raises DuplicateJobTypeError so a routing collision surfaces at boot, not at dispatch.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(definitions) ⇒ Registry

── Per-worker instance ─────────────────────────────────────────────



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/zeridion_flare/worker/registry.rb', line 57

def initialize(definitions)
  @by_type = {}
  definitions.each do |definition|
    if @by_type.key?(definition.job_type)
      raise DuplicateJobTypeError, definition.job_type
    end

    @by_type[definition.job_type] = definition
  end
  @by_type.freeze
end

Class Method Details

.from_declaredObject

Snapshot every class declared so far, resolved to JobDefinitions, and de-duplicated by job_type (raising on collision). Returns a Registry instance the Worker uses for the rest of its life.



37
38
39
40
41
42
43
44
45
46
# File 'lib/zeridion_flare/worker/registry.rb', line 37

def from_declared
  snapshot = @global_mutex.synchronize { @global_classes.dup }
  definitions = snapshot.map do |(kind, klass)|
    case kind
    when :payload   then JobDefinition.from_payload_class(klass)
    when :recurring then JobDefinition.from_recurring_class(klass)
    end
  end
  new(definitions)
end

.register_payload(klass) ⇒ Object



26
27
28
# File 'lib/zeridion_flare/worker/registry.rb', line 26

def register_payload(klass)
  @global_mutex.synchronize { @global_classes << [:payload, klass] }
end

.register_recurring(klass) ⇒ Object



30
31
32
# File 'lib/zeridion_flare/worker/registry.rb', line 30

def register_recurring(klass)
  @global_mutex.synchronize { @global_classes << [:recurring, klass] }
end

.reset_declared!Object

Test seam: forget all globally-declared classes. Not part of the public API — used only so unit tests don't bleed registrations.



50
51
52
# File 'lib/zeridion_flare/worker/registry.rb', line 50

def reset_declared!
  @global_mutex.synchronize { @global_classes = [] }
end

Instance Method Details

#allArray<JobDefinition>

Returns:



75
76
77
# File 'lib/zeridion_flare/worker/registry.rb', line 75

def all
  @by_type.values
end

#empty?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/zeridion_flare/worker/registry.rb', line 79

def empty?
  @by_type.empty?
end

#job_typesObject

Every registered job_type (what poll restricts to).



89
90
91
# File 'lib/zeridion_flare/worker/registry.rb', line 89

def job_types
  @by_type.values.map(&:job_type)
end

#lookup(job_type) ⇒ JobDefinition?

Returns:



70
71
72
# File 'lib/zeridion_flare/worker/registry.rb', line 70

def lookup(job_type)
  @by_type[job_type]
end

#queuesObject

Distinct served queues across every registered job.



84
85
86
# File 'lib/zeridion_flare/worker/registry.rb', line 84

def queues
  @by_type.values.map(&:queue).uniq
end

#recurring_schedulesObject

The recurring schedules to announce at register time.



94
95
96
# File 'lib/zeridion_flare/worker/registry.rb', line 94

def recurring_schedules
  @by_type.values.select(&:recurring?).map { |d| RecurringSchedule.from_definition(d) }
end