Class: Karafka::Web::Tracking::MemoizedShell

Inherits:
Object
  • Object
show all
Defined in:
lib/karafka/web/tracking/memoized_shell.rb

Overview

Class used to run shell command that also returns previous result in case of a failure This is used because children can get signals when performing stat fetches and then fetch is stopped. This can cause invalid results from sub-shell commands.

This will return last result as log as there was one.

Instance Method Summary collapse

Constructor Details

#initializeMemoizedShell

Returns a new instance of MemoizedShell.



19
20
21
# File 'lib/karafka/web/tracking/memoized_shell.rb', line 19

def initialize
  @accu = {}
end

Instance Method Details

#call(cmd) ⇒ String?

Returns sub-shell evaluation string result or nil if we were not able to run or re-run the call.

Parameters:

  • cmd (String)

Returns:

  • (String, nil)

    sub-shell evaluation string result or nil if we were not able to run or re-run the call.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/karafka/web/tracking/memoized_shell.rb', line 26

def call(cmd)
  attempt ||= 0

  while attempt < MAX_ATTEMPTS
    attempt += 1

    stdout_str, status = Open3.capture2(cmd)

    if status.success?
      @accu[cmd] = stdout_str
      return stdout_str
    else
      return stdout_str if attempt > MAX_ATTEMPTS
      return @accu[cmd] if @accu.key?(cmd)
    end
  end

  @accu[cmd]
end