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.0"
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
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, = {}) @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.
103 104 105 |
# File 'lib/wait_for_it.rb', line 103 def log @log end |
#timeout ⇒ Object (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
24 25 26 27 |
# File 'lib/wait_for_it.rb', line 24 def self.config yield self self end |
.env ⇒ Object
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 |
.redirection ⇒ Object
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 |
.timeout ⇒ Object
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_for ⇒ Object
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
#cleanup ⇒ Object
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.
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) = {} [:command] = @command [:input] = input [:timeout] = t [:log] = @log raise WaitForItTimeoutError.new() end end |