Class: Kommando::ScheduledCommandAdapters::Memory

Inherits:
Object
  • Object
show all
Defined in:
lib/kommando/scheduled_command_adapters/memory.rb

Constant Summary collapse

@@scheduled =
[]
@@locked_ids =
[]

Class Method Summary collapse

Class Method Details

.clearObject



83
84
85
86
# File 'lib/kommando/scheduled_command_adapters/memory.rb', line 83

def self.clear
  @@scheduled = []
  @@locked_ids = []
end

.countObject



79
80
81
# File 'lib/kommando/scheduled_command_adapters/memory.rb', line 79

def self.count
  @@scheduled.size
end

.fetch!(&block) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/kommando/scheduled_command_adapters/memory.rb', line 23

def self.fetch!(&block)
  scheduled_ids = @@scheduled.map { |command| command[:id] }

  record = @@scheduled.select do |command|
    next if @@locked_ids.include?(command[:id])
    next if Time.now < command[:handle_at]
    next if command[:wait_for_command_ids].any? { |id| scheduled_ids.include?(id) }

    true
  end.sort do |a, b|
    a[:handle_at] <=> b[:handle_at]
  end.first

  if record
    @@locked_ids.push(record[:id])
    result = block.call(record[:name], record[:parameters])

    if result.success?
      @@scheduled.delete(record)
    else
      record[:failures] = record[:failures].append(result.error)
      record[:handle_at] = record[:handle_at] + 5 * 60
    end

    @@locked_ids.delete(record[:id])
  end
end

.firstObject



75
76
77
# File 'lib/kommando/scheduled_command_adapters/memory.rb', line 75

def self.first
  OpenStruct.new(@@scheduled.first)
end

.metricsObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/kommando/scheduled_command_adapters/memory.rb', line 51

def self.metrics
  scheduled_ids = @@scheduled.map { |command| command[:id] }

  executable = @@scheduled.select do |command|
    next if @@locked_ids.include?(command[:id])
    next if Time.now < command[:handle_at]
    next if command[:wait_for_command_ids].any? { |id| scheduled_ids.include?(id) }

    true
  end.size

  scheduled = count

  with_failures = @@scheduled.select do |command|
    command[:failures].size > 0
  end.size

  {
    kommando_executable_commands: executable,
    kommando_scheduled_commands: scheduled,
    kommando_scheduled_commands_with_failures: with_failures,
  }
end

.schedule!(command, parameters, handle_at) ⇒ Object



12
13
14
15
16
17
18
19
20
21
# File 'lib/kommando/scheduled_command_adapters/memory.rb', line 12

def self.schedule!(command, parameters, handle_at)
  @@scheduled << {
    id: parameters.fetch(:command_id),
    name: command,
    parameters: parameters,
    handle_at: handle_at,
    failures: [],
    wait_for_command_ids: parameters.fetch(:wait_for_command_ids, []),
  }
end