Class: Kommando::ScheduledCommandAdapters::ActiveRecord

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/kommando/scheduled_command_adapters/active_record.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.fetch!(&block) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/kommando/scheduled_command_adapters/active_record.rb', line 19

def self.fetch!(&block)
  transaction do
    record = lock('FOR UPDATE SKIP LOCKED').
      where('TIMEZONE(\'UTC\', NOW()) >= handle_at').
      where.not("wait_for_command_ids && (SELECT array_agg(id) FROM #{table_name})").
      order(handle_at: :asc).
      limit(1).
      first

    if record
      result = block.call(record.name, record.parameters)

      if result.success?
        record.destroy
      else
        record.update!({
          failures: record.failures.append(result.error),
          handle_at: (record.handle_at + 5.minutes).getutc,
        })
      end
    end
  end
end

.metricsObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/kommando/scheduled_command_adapters/active_record.rb', line 43

def self.metrics
  executable = where('TIMEZONE(\'UTC\', NOW()) >= handle_at').
    where.not("wait_for_command_ids && (SELECT array_agg(id) FROM #{table_name})").
    count

  scheduled = count

  with_failures = where('array_length(failures, 1) > 0').count

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

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



8
9
10
11
12
13
14
15
16
17
# File 'lib/kommando/scheduled_command_adapters/active_record.rb', line 8

def self.schedule!(command, parameters, handle_at)
  create!({
    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

Instance Method Details

#failuresObject



63
64
65
# File 'lib/kommando/scheduled_command_adapters/active_record.rb', line 63

def failures
  @failures ||= super.map(&:deep_symbolize_keys!)
end

#parametersObject



59
60
61
# File 'lib/kommando/scheduled_command_adapters/active_record.rb', line 59

def parameters
  @parameters ||= super.deep_symbolize_keys!
end