From f385ed423e11ad40822f688bc592eaa78efa5b34 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Mon, 9 Jan 2017 18:59:00 +0000 Subject: eventmachine: wait for deferred actions to finish Since EventMachine 1.0.0 in 2012, the EM.defers_finish? API exists to check for the existence of deferred actions. Support it if it exists in the running version of EM and update the note in our SIGNALS document. Thanks to on the mailing list for bringing this up: https://bogomips.org/rainbows-public/CAKwvcL-VH3we4qA1pkNAstTmWvqNA=Rir2N_YiWztV_qbaLQvA@mail.gmail.com/ --- t/app_deferred.ru | 8 +++++++- t/t0700-app-deferred.sh | 18 +++++++++++++++--- 2 files changed, 22 insertions(+), 4 deletions(-) (limited to 't') diff --git a/t/app_deferred.ru b/t/app_deferred.ru index a70b33b..b3d7ff1 100644 --- a/t/app_deferred.ru +++ b/t/app_deferred.ru @@ -6,12 +6,18 @@ class DeferredApp < Struct.new(:app) def deferred?(env) - env["PATH_INFO"] == "/deferred" + env["PATH_INFO"] =~ %r{\A/deferred} end def call(env) env["rack.multithread"] or raise RuntimeError, "rack.multithread not true" body = "#{Thread.current.inspect}\n" + if env["PATH_INFO"] =~ %r{\A/deferred(\d+)} + delay = $1.to_i + File.open(ENV['fifo'], 'w') { |fp| fp.write "sleeping #{delay}s\n" } + body = "deferred sleep\n" + sleep(delay) + end headers = { "Content-Type" => "text/plain", "Content-Length" => body.size.to_s, diff --git a/t/t0700-app-deferred.sh b/t/t0700-app-deferred.sh index 90614b2..188fdde 100755 --- a/t/t0700-app-deferred.sh +++ b/t/t0700-app-deferred.sh @@ -15,7 +15,7 @@ CONFIG_RU=app_deferred.ru t_begin "setup and start" && { rainbows_setup rtmpfiles deferred_err deferred_out sync_err sync_out - rainbows -D -c $unicorn_config $CONFIG_RU + fifo=$fifo rainbows -D -c $unicorn_config $CONFIG_RU rainbows_wait_start } @@ -36,8 +36,20 @@ t_begin "deferred requests run in a different thread" && { test x"$(uniq < $deferred_out)" != x"$sync_thread" } -t_begin "termination signal sent" && { - kill $rainbows_pid +t_begin "deferred requests run after graceful shutdown" && { + # XXX sleeping 5s ought to be enough for SIGQUIT to arrive, + # hard to tell with overloaded systems... + s=5 + curl -sSf --no-buffer http://$listen/deferred$s \ + >$deferred_out 2>$deferred_err & + curl_pid=$! + msg="$(cat $fifo)" + kill -QUIT $rainbows_pid + test x"$msg" = x"sleeping ${s}s" + wait $curl_pid # for curl to finish + test $? -eq 0 + test ! -s $deferred_err + test x"$(cat $deferred_out)" = 'xdeferred sleep' } t_begin "no errors in stderr" && check_stderr -- cgit v1.2.3-24-ge0c7