break_if(&block)
click to toggle source
def break_if(&block)
raise ArgumentError, "block is not given" unless block_given?
@run_breaking_conditions << block
end
broken?()
click to toggle source
end_if(&block)
click to toggle source
def end_if(&block)
raise ArgumentError, "block is not given" unless block_given?
@run_post_conditions << block
end
instance_hook_after_started()
click to toggle source
def instance_hook_after_started
end
instance_shutdown()
click to toggle source
def instance_shutdown
@instance.stop unless @instance.stopped?
@instance.before_shutdown unless @instance.before_shutdown?
@instance.shutdown unless @instance.shutdown?
if @instance.respond_to?(:event_loop_wait_until_stop)
@instance.event_loop_wait_until_stop
end
@instance.after_shutdown unless @instance.after_shutdown?
@instance.close unless @instance.closed?
if @instance.respond_to?(:thread_wait_until_stop)
@instance.thread_wait_until_stop
end
@instance.terminate unless @instance.terminated?
end
instance_start()
click to toggle source
def instance_start
unless @instance.started?
@instance.start
instance_hook_after_started
end
unless @instance.after_started?
@instance.after_start
end
end
run(timeout: nil, start: true, shutdown: true, &block)
click to toggle source
def run(timeout: nil, start: true, shutdown: true, &block)
instance_start if start
if @instance.respond_to?(:thread_wait_until_start)
@instance.thread_wait_until_start
end
if @instance.respond_to?(:event_loop_wait_until_start)
@instance.event_loop_wait_until_start
end
begin
run_actual(timeout: timeout, &block)
ensure
instance_shutdown if shutdown
end
end
run_actual(timeout: nil, &block)
click to toggle source
def run_actual(timeout: nil, &block)
if @instance.respond_to?(:_threads)
until @instance._threads.values.all?(&:alive?)
sleep 0.01
end
end
if @instance.respond_to?(:event_loop_running?)
until @instance.event_loop_running?
sleep 0.01
end
end
if timeout
stop_at = Time.now + timeout
@run_breaking_conditions << ->(){ Time.now >= stop_at }
end
if !block_given? && @run_post_conditions.empty? && @run_breaking_conditions.empty?
raise ArgumentError, "no stop conditions nor block specified"
end
proc = if block_given?
->(){ block.call; sleep(0.1) until stop? }
else
->(){ sleep(0.1) until stop? }
end
if timeout
begin
Timeout.timeout(timeout * 1.1) do |sec|
proc.call
end
rescue Timeout::Error
@broken = true
end
else
proc.call
end
end
stop?()
click to toggle source
def stop?
return true unless @run_post_conditions
return true if @run_post_conditions.all? {|proc| proc.call }
if @run_breaking_conditions.any? {|proc| proc.call }
@broken = true
return true
end
false
end