Class: WaitForIt
- Inherits:
-
Object
- Object
- WaitForIt
- 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
-
#log ⇒ Object
readonly
Returns the value of attribute log.
-
#timeout ⇒ Object
readonly
Returns the value of attribute timeout.
Class Method Summary collapse
-
.config {|_self| ... } ⇒ Object
Configure global WaitForIt settings.
- .env ⇒ Object
-
.env=(env) ⇒ Object
Default environment variables under which commands should be executed.
- .redirection ⇒ Object
-
.redirection=(redirection) ⇒ Object
The default shell redirect to the logs.
- .timeout ⇒ Object
-
.timeout=(timeout) ⇒ Object
The default timeout that is waited for a process to boot.
- .wait_for ⇒ Object
-
.wait_for=(wait_for) ⇒ Object
The default output is expected in the logs before the process is considered “booted”.
Instance Method Summary collapse
-
#cleanup ⇒ Object
Kills the process and removes temporary files.
-
#contains?(input) ⇒ Boolean
Checks the logs of the process to see if they contain a match.
-
#count(input) ⇒ Object
Returns a count of the number of times logs match the input.
-
#initialize(command, options = {}) ⇒ WaitForIt
constructor
Creates a new WaitForIt instance.
-
#wait(input, t = timeout) ⇒ Object
Blocks parent process until given message appears at the.
-
#wait!(input, t = timeout) ⇒ Object
Same as ‘wait` but raises an error if timeout is reached.
Constructor Details
#initialize(command, options = {}) ⇒ WaitForIt
Creates a new WaitForIt instance
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, = {}) @command = command @timeout = [:timeout] || WaitForIt.timeout @wait_for = [:wait_for] || WaitForIt.wait_for redirection = [:redirection] || WaitForIt.redirection env = [: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
#log ⇒ Object (readonly)
Returns the value of attribute log.
107 108 109 |
# File 'lib/wait_for_it.rb', line 107 def log @log end |
#timeout ⇒ Object (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
28 29 30 31 |
# File 'lib/wait_for_it.rb', line 28 def self.config yield self self end |
.env ⇒ Object
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 |
.redirection ⇒ Object
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 |
.timeout ⇒ Object
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_for ⇒ Object
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
#cleanup ⇒ Object
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.
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) = {} [:command] = @command [:input] = input [:timeout] = t [:log] = @log raise WaitForItTimeoutError.new() end end |