Class: Alerty::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/alerty/command.rb

Instance Method Summary collapse

Constructor Details

#initialize(command:) ⇒ Command

Returns a new instance of Command.



7
8
9
10
11
# File 'lib/alerty/command.rb', line 7

def initialize(command:)
  @command = command
  @opts = { timeout: Config.timeout, exclusive: Config.lock_path, popen2e: true }
  @hostname = Socket.gethostname
end

Instance Method Details

#runObject



13
14
15
16
17
18
19
20
21
22
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
50
51
52
53
54
# File 'lib/alerty/command.rb', line 13

def run
  record = {}
  with_retry do |retries|
    started_at = Time.now
    begin
      result = with_clean_env { Frontkick.exec(@command, @opts) }
    rescue Frontkick::Timeout => e
      record = {
        hostname:   @hostname,
        command:    @command,
        exitstatus: 1,
        output:     "`#{@command}` is timeout (#{@opts[:timeout]} sec)",
        started_at: started_at.to_f,
        duration:   @opts[:timeout],
        retries:    retries,
      }
    rescue Frontkick::Locked => e
      record = {
        hostname:   @hostname,
        command:    @command,
        exitstatus: 1,
        output:     "`#{@opts[:exclusive]}` is locked by another process",
        started_at: started_at.to_f,
        duration:   0,
        retries:    retries,
      }
    else
      record = {
        hostname:   @hostname,
        command:    @command,
        exitstatus: result.exitstatus,
        output:     result.output,
        started_at: started_at.to_f,
        duration:   result.duration,
        retries:    retries,
      }
    end
    Alerty.logger.info { "result: #{record.to_json}" }
    record
  end
  record
end