Class: WaitForIt

Inherits:
Object
  • Object
show all
Defined in:
lib/wait_for_it.rb,
lib/wait_for_it/version.rb

Defined Under Namespace

Classes: WaitForItTimeoutError

Constant Summary collapse

DEFAULT_TIMEOUT =

seconds

10
DEFAULT_OUT =
">>"
DEFAULT_ENV =
{}
VERSION =
"0.2.1"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command, options = {}) ⇒ WaitForIt

Creates a new WaitForIt instance

Parameters:

  • command (String)

    Command to spawn

  • options (Hash) (defaults to: {})


81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/wait_for_it.rb', line 81

def initialize(command, options = {})
  @command    = command
  @timeout    = options[:timeout]     || WaitForIt.timeout
  @wait_for   = options[:wait_for]    || WaitForIt.wait_for
  redirection = options[:redirection] || WaitForIt.redirection
  env         = options[:env]         || WaitForIt.env
  @log        = set_log
  @pid        = nil

  raise "Must provide a wait_for: option" unless @wait_for
  spawn(command, redirection, env)
  wait!(@wait_for)

  if block_given?
    begin
      yield self
    ensure
      cleanup
    end
  end

rescue WaitForItTimeoutError => e
  cleanup
  raise e
end

Instance Attribute Details

#logObject (readonly)

Returns the value of attribute log.



107
108
109
# File 'lib/wait_for_it.rb', line 107

def log
  @log
end

#timeoutObject (readonly)

Returns the value of attribute timeout.



107
108
109
# File 'lib/wait_for_it.rb', line 107

def timeout
  @timeout
end

Class Method Details

.config {|_self| ... } ⇒ Object

Configure global WaitForIt settings

Yields:

  • (_self)

Yield Parameters:

  • _self (WaitForIt)

    the object that the method was called on



28
29
30
31
# File 'lib/wait_for_it.rb', line 28

def self.config
  yield self
  self
end

.envObject



67
68
69
# File 'lib/wait_for_it.rb', line 67

def self.env
  @env || DEFAULT_ENV
end

.env=(env) ⇒ Object

Default environment variables under which commands should be executed.



63
64
65
# File 'lib/wait_for_it.rb', line 63

def self.env=(env)
  @env = env
end

.redirectionObject



57
58
59
# File 'lib/wait_for_it.rb', line 57

def self.redirection
  @redirection || DEFAULT_OUT
end

.redirection=(redirection) ⇒ Object

The default shell redirect to the logs



53
54
55
# File 'lib/wait_for_it.rb', line 53

def self.redirection=(redirection)
  @redirection = redirection
end

.timeoutObject



47
48
49
# File 'lib/wait_for_it.rb', line 47

def self.timeout
  @timeout || DEFAULT_TIMEOUT
end

.timeout=(timeout) ⇒ Object

The default timeout that is waited for a process to boot



43
44
45
# File 'lib/wait_for_it.rb', line 43

def self.timeout=(timeout)
  @timeout = timeout
end

.wait_forObject



38
39
40
# File 'lib/wait_for_it.rb', line 38

def self.wait_for
  @wait_for
end

.wait_for=(wait_for) ⇒ Object

The default output is expected in the logs before the process is considered “booted”



34
35
36
# File 'lib/wait_for_it.rb', line 34

def self.wait_for=(wait_for)
  @wait_for = wait_for
end

Instance Method Details

#cleanupObject

Kills the process and removes temporary files



149
150
151
152
153
# File 'lib/wait_for_it.rb', line 149

def cleanup
  shutdown
  close_log
  unlink_log
end

#contains?(input) ⇒ Boolean

Checks the logs of the process to see if they contain a match. Can use a string or a regular expression.

Returns:

  • (Boolean)


111
112
113
# File 'lib/wait_for_it.rb', line 111

def contains?(input)
  log.read.match convert_to_regex(input)
end

#count(input) ⇒ Object

Returns a count of the number of times logs match the input. Can use a string or a regular expression.



117
118
119
# File 'lib/wait_for_it.rb', line 117

def count(input)
  log.read.scan(convert_to_regex(input)).count
end

#wait(input, t = timeout) ⇒ Object

Blocks parent process until given message appears at the



122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/wait_for_it.rb', line 122

def wait(input, t = timeout)
  regex = convert_to_regex(input)
  Timeout::timeout(t) do
    until log.read.match regex
      sleep 0.01
    end
  end
  sleep 0.01
  self
rescue Timeout::Error
  puts "Timeout waiting for #{input.inspect} to find a match using #{ regex } in \n'#{ log.read }'"
  false
end

#wait!(input, t = timeout) ⇒ Object

Same as ‘wait` but raises an error if timeout is reached



137
138
139
140
141
142
143
144
145
146
# File 'lib/wait_for_it.rb', line 137

def wait!(input, t = timeout)
  unless wait(input, t)
    options = {}
    options[:command] = @command
    options[:input]   = input
    options[:timeout] = t
    options[:log]     = @log
    raise WaitForItTimeoutError.new(options)
  end
end