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.0"

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: {})


77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/wait_for_it.rb', line 77

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.



103
104
105
# File 'lib/wait_for_it.rb', line 103

def log
  @log
end

#timeoutObject (readonly)

Returns the value of attribute timeout.



103
104
105
# File 'lib/wait_for_it.rb', line 103

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



24
25
26
27
# File 'lib/wait_for_it.rb', line 24

def self.config
  yield self
  self
end

.envObject



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

def self.env
  @env || DEFAULT_ENV
end

.env=(env) ⇒ Object

Default environment variables under which commands should be executed.



59
60
61
# File 'lib/wait_for_it.rb', line 59

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

.redirectionObject



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

def self.redirection
  @redirection || DEFAULT_OUT
end

.redirection=(redirection) ⇒ Object

The default shell redirect to the logs



49
50
51
# File 'lib/wait_for_it.rb', line 49

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

.timeoutObject



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

def self.timeout
  @timeout || DEFAULT_TIMEOUT
end

.timeout=(timeout) ⇒ Object

The default timeout that is waited for a process to boot



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

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

.wait_forObject



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

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”



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

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

Instance Method Details

#cleanupObject

Kills the process and removes temporary files



145
146
147
148
149
# File 'lib/wait_for_it.rb', line 145

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)


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

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.



113
114
115
# File 'lib/wait_for_it.rb', line 113

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



118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/wait_for_it.rb', line 118

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



133
134
135
136
137
138
139
140
141
142
# File 'lib/wait_for_it.rb', line 133

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