Class: Postjob::Registry

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

Overview

The registry holds a list of all available workflows

Defined Under Namespace

Classes: WorkflowSpec

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRegistry

Returns a new instance of Registry.



167
168
169
170
# File 'lib/postjob/registry.rb', line 167

def initialize
  @workflows = {}
  @workflows_with_versions = []
end

Instance Attribute Details

#workflowsObject (readonly)

Returns the value of attribute workflows.



164
165
166
# File 'lib/postjob/registry.rb', line 164

def workflows
  @workflows
end

#workflows_with_versionsObject (readonly)

Returns the value of attribute workflows_with_versions.



165
166
167
# File 'lib/postjob/registry.rb', line 165

def workflows_with_versions
  @workflows_with_versions
end

Class Method Details

.[](name) ⇒ Object



158
159
160
161
162
# File 'lib/postjob/registry.rb', line 158

def self.[](name)
  lookup! name: name, version: ""
rescue KeyError
  nil
end

.instanceObject



3
4
5
# File 'lib/postjob/registry.rb', line 3

def self.instance
  @instance ||= new
end

.lookup!(name:, version:) ⇒ Object

looks up a specific version of a specific workflow. Returns a WorkflowSpec object.



154
155
156
# File 'lib/postjob/registry.rb', line 154

def self.lookup!(name:, version:)
  instance.lookup! name: name, version: version
end

.queuesObject

returns an array with the name of all queues that are configured here.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/postjob/registry.rb', line 17

def self.queues
  queues = workflows
           .select { |_, spec| spec.runnable? }
           .map(&:last)
           .map(&:options)
           .map(&:queue)

  # For a while "ruby" was the name of the default queue. Since we might
  # have jobs with the queue in the database we always return "ruby" as
  # one of the runnable queues, even if there is no workflow explicitely
  # registered on a "ruby" queue.
  queues << "ruby"
  queues.uniq
end

.register(workflow, options = {}) ⇒ Object



49
50
51
# File 'lib/postjob/registry.rb', line 49

def self.register(workflow, options = {})
  instance.register(workflow, options)
end

.reset!Object

Used for tests



8
9
10
# File 'lib/postjob/registry.rb', line 8

def self.reset! # :nodoc:
  @instance = nil
end

.runnable_workflows_with_versionsObject



36
37
38
39
40
41
42
43
# File 'lib/postjob/registry.rb', line 36

def self.runnable_workflows_with_versions
  workflows
    .select { |_, spec| spec.runnable? }
    .inject([]) do |ary, (name_and_version, _spec)|
      name, version = *name_and_version
      ary << name << "#{name}#{version}"
    end
end

.workflow_namesObject



32
33
34
# File 'lib/postjob/registry.rb', line 32

def self.workflow_names
  instance.workflows.keys.map(&:first).uniq
end

.workflowsObject



12
13
14
# File 'lib/postjob/registry.rb', line 12

def self.workflows
  instance.workflows
end

.workflows_with_versionsObject



45
46
47
# File 'lib/postjob/registry.rb', line 45

def self.workflows_with_versions
  instance.workflows_with_versions
end

Instance Method Details

#lookup!(name:, version:) ⇒ Object

looks up a specific version of a specific workflow. Returns the workflow module itself



187
188
189
190
191
192
# File 'lib/postjob/registry.rb', line 187

def lookup!(name:, version:)
  expect! name => String
  expect! version => String

  @workflows.fetch([name, version])
end

#register(workflow, options) ⇒ Object



172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/postjob/registry.rb', line 172

def register(workflow, options)
  if options[:greedy] && options[:sticky] == false
    raise ArgumentError, "#{workflow}: a greedy job must also be sticky"
  end

  spec = WorkflowSpec.new(workflow, options)

  @workflows_with_versions << spec.name << "#{spec.name}#{spec.options.version}"

  @workflows[[spec.name, ""]] = spec
  @workflows[[spec.name, spec.options.version]] = spec
end