Class: Orchestration::Queue

Inherits:
Object show all
Defined in:
app/services/orchestration/queue.rb

Overview

Represents tasks queue for orchestration

Constant Summary collapse

STATUS =
%w[pending running failed completed rollbacked conflict canceled]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = "Unnamed") ⇒ Queue

Returns a new instance of Queue.


13
14
15
16
# File 'app/services/orchestration/queue.rb', line 13

def initialize(name = "Unnamed")
  @items = []
  @name = name
end

Instance Attribute Details

#itemsObject (readonly)

Returns the value of attribute items


6
7
8
# File 'app/services/orchestration/queue.rb', line 6

def items
  @items
end

#nameObject (readonly)

Returns the value of attribute name


6
7
8
# File 'app/services/orchestration/queue.rb', line 6

def name
  @name
end

Instance Method Details

#allObject


43
44
45
# File 'app/services/orchestration/queue.rb', line 43

def all
  items.sort
end

#clearObject


51
52
53
54
# File 'app/services/orchestration/queue.rb', line 51

def clear
  @items = []
  true
end

#create(options) ⇒ Object


18
19
20
21
22
23
24
25
26
27
28
# File 'app/services/orchestration/queue.rb', line 18

def create(options)
  options[:status] ||= default_status
  new_task = Task.new(options)
  if items.include? new_task
    # Two tasks with same :name are not allowed. Use two different :id options for multiple instances.
    Rails.logger.debug "Task '#{new_task.id || new_task.name || ''}' already in '#{name}' queue"
  else
    Rails.logger.debug "Enqueued task '#{new_task.id || new_task.name || ''}' to '#{name}' queue"
    items << new_task
  end
end

#delete(item) ⇒ Object


30
31
32
# File 'app/services/orchestration/queue.rb', line 30

def delete(item)
  @items.delete item
end

#find_by_id(id) ⇒ Object


38
39
40
41
# File 'app/services/orchestration/queue.rb', line 38

def find_by_id(id)
  string_id = id.to_s
  items.detect { |task| task.id == string_id }
end

#find_by_name(name) ⇒ Object


34
35
36
# File 'app/services/orchestration/queue.rb', line 34

def find_by_name(name)
  items.detect { |task| task.name == name }
end

#task_idsObject


47
48
49
# File 'app/services/orchestration/queue.rb', line 47

def task_ids
  all.map(&:id)
end