Rainbows! Rack HTTP server user/dev discussion
 help / Atom feed
* Waiting for deferred actions to finish
@ 2017-01-09 13:43 alex0375
  2017-01-09 19:43 ` Eric Wong
  0 siblings, 1 reply; 5+ messages in thread
From: alex0375 @ 2017-01-09 13:43 UTC (permalink / raw)
  To: rainbows-public

Hello!

I would like the Rainbows! to wait for deferred actions to finish
before shutdown. The documentation says the following:

> QUIT - graceful shutdown, waits for workers to finish their current request before finishing. This currently does not wait for requests deferred to a separate thread when using EventMachine (when app.deferred?(env) => true)
>
> https://bogomips.org/rainbows/SIGNALS.html

I came up with the following solution:

after_fork do |_server, _worker|
  # Wait for all deferred actions to finish before killing worker.
  Rainbows.at_quit do
    loop do
      break if EventMachine.defers_finished?

      sleep 1
    end
  end
end

Am I missing something? Can this be a part of Rainbows?

Thanks!

^ permalink raw reply	[flat|threaded] 5+ messages in thread

* Re: Waiting for deferred actions to finish
  2017-01-09 13:43 Waiting for deferred actions to finish alex0375
@ 2017-01-09 19:43 ` Eric Wong
  2017-01-09 19:45   ` [PATCH 1/2] tests: re-enable EventMachine tests, again Eric Wong
                     ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Eric Wong @ 2017-01-09 19:43 UTC (permalink / raw)
  To: alex0375; +Cc: rainbows-public

alex0375@gmail.com wrote:
> Am I missing something? Can this be a part of Rainbows?

Nope, you're not missing anything :)
Rainbows! was missing an EM feature introduced in 2012 :x

I'll make a 5.1.0 release with following patches
(coming in-reply-to this email) soon.

There's also rack 2.x compatibility sitting in rainbows.git.
https://bogomips.org/rainbows-public/20161117033531.GA4454@dcvr/T/

The following changes since commit bd7596e50bd094edf5e5842afb8239c158fe2491:

  Revert "t/t0044-autopush.sh: remove test" (2016-12-24 21:42:06 +0000)

are available in the git repository at:

  git://bogomips.org/rainbows em-deferred

for you to fetch changes up to f385ed423e11ad40822f688bc592eaa78efa5b34:

  eventmachine: wait for deferred actions to finish (2017-01-09 18:59:00 +0000)

----------------------------------------------------------------
Eric Wong (2):
      tests: re-enable EventMachine tests, again
      eventmachine: wait for deferred actions to finish

 SIGNALS                       |  6 +++---
 lib/rainbows/event_machine.rb | 10 +++++++++-
 t/GNUmakefile                 |  2 +-
 t/app_deferred.ru             |  8 +++++++-
 t/t0700-app-deferred.sh       | 18 +++++++++++++++---
 5 files changed, 35 insertions(+), 9 deletions(-)

^ permalink raw reply	[flat|threaded] 5+ messages in thread

* [PATCH 1/2] tests: re-enable EventMachine tests, again
  2017-01-09 19:43 ` Eric Wong
@ 2017-01-09 19:45   ` Eric Wong
  2017-01-09 19:45   ` [PATCH 2/2] eventmachine: wait for deferred actions to finish Eric Wong
  2017-01-10  7:31   ` alex0375
  2 siblings, 0 replies; 5+ messages in thread
From: Eric Wong @ 2017-01-09 19:45 UTC (permalink / raw)
  To: rainbows-public; +Cc: alex0375

Based on activity in <git://github.com/eventmachine/eventmachine>,
EventMachine seems to be maintained, again, so resume testing our
integration tests.
---
 t/GNUmakefile | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/t/GNUmakefile b/t/GNUmakefile
index 97b1985..9ead6ee 100644
--- a/t/GNUmakefile
+++ b/t/GNUmakefile
@@ -41,6 +41,7 @@ ifeq ($(RUBY_ENGINE),ruby)
     endif
     models += FiberSpawn
     models += FiberPool
+    models += EventMachine
 
     RUBY_LE_2_1 := $(shell $(RUBY) -e 'puts(RUBY_VERSION.to_f <= 2.1)')
     ifeq ($(RUBY_LE_2_1), true)
@@ -48,7 +49,6 @@ ifeq ($(RUBY_ENGINE),ruby)
       models += CoolioThreadPool
       models += CoolioThreadSpawn
       models += CoolioFiberSpawn
-      models += EventMachine
       models += NeverBlock
     endif
   endif
-- 
EW

^ permalink raw reply	[flat|threaded] 5+ messages in thread

* [PATCH 2/2] eventmachine: wait for deferred actions to finish
  2017-01-09 19:43 ` Eric Wong
  2017-01-09 19:45   ` [PATCH 1/2] tests: re-enable EventMachine tests, again Eric Wong
@ 2017-01-09 19:45   ` Eric Wong
  2017-01-10  7:31   ` alex0375
  2 siblings, 0 replies; 5+ messages in thread
From: Eric Wong @ 2017-01-09 19:45 UTC (permalink / raw)
  To: rainbows-public; +Cc: alex0375

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 <alex0375@gmail.com> on the mailing list for bringing
this up:
https://bogomips.org/rainbows-public/CAKwvcL-VH3we4qA1pkNAstTmWvqNA=Rir2N_YiWztV_qbaLQvA@mail.gmail.com/
---
 SIGNALS                       |  6 +++---
 lib/rainbows/event_machine.rb | 10 +++++++++-
 t/app_deferred.ru             |  8 +++++++-
 t/t0700-app-deferred.sh       | 18 +++++++++++++++---
 4 files changed, 34 insertions(+), 8 deletions(-)

diff --git a/SIGNALS b/SIGNALS
index e53c3e4..fa95a1c 100644
--- a/SIGNALS
+++ b/SIGNALS
@@ -24,9 +24,9 @@ between \Rainbows!, unicorn and nginx.
 * INT/TERM - quick shutdown, kills all workers immediately
 
 * QUIT - graceful shutdown, waits for workers to finish their
-  current request before finishing.  This currently does not
-  wait for requests deferred to a separate thread when using
-  EventMachine (when app.deferred?(env) => true)
+  current request before finishing.  Since Rainbows 5.1.0 (Jan 2017),
+  this waits requests deferred to a separate thread with
+  EventMachine (app.deferred?(env) => true).
 
 * USR1 - reopen all logs owned by the master and all workers
   See Unicorn::Util.reopen_logs for what is considered a log.
diff --git a/lib/rainbows/event_machine.rb b/lib/rainbows/event_machine.rb
index b326e26..896fdac 100644
--- a/lib/rainbows/event_machine.rb
+++ b/lib/rainbows/event_machine.rb
@@ -65,6 +65,11 @@ def em_client_class
     end
   end
 
+  def defers_finished?
+    # EventMachine 1.0.0+ has defers_finished?
+    EM.respond_to?(:defers_finished?) ? EM.defers_finished? : true
+  end
+
   # runs inside each forked worker, this sits around and waits
   # for connections and doesn't die until the parent dies (or is
   # given a INT, QUIT, or TERM signal)
@@ -101,7 +106,10 @@ def worker_loop(worker) # :nodoc:
         end
       end
       EM.add_periodic_timer(1) do
-        EM.stop if ! Rainbows.tick && conns.empty? && EM.reactor_running?
+        if ! Rainbows.tick && conns.empty? && defers_finished? &&
+            EM.reactor_running?
+          EM.stop
+        end
       end
       LISTENERS.map! do |s|
         EM.watch(s, Rainbows::EventMachine::Server) do |c|
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
-- 
EW

^ permalink raw reply	[flat|threaded] 5+ messages in thread

* Re: Waiting for deferred actions to finish
  2017-01-09 19:43 ` Eric Wong
  2017-01-09 19:45   ` [PATCH 1/2] tests: re-enable EventMachine tests, again Eric Wong
  2017-01-09 19:45   ` [PATCH 2/2] eventmachine: wait for deferred actions to finish Eric Wong
@ 2017-01-10  7:31   ` alex0375
  2 siblings, 0 replies; 5+ messages in thread
From: alex0375 @ 2017-01-10  7:31 UTC (permalink / raw)
  To: Eric Wong; +Cc: rainbows-public

Hi, Eric

I have tried the new revision (rainbows-5.0.0.8.gf385) and it works
great for me.

Thanks!

On Mon, Jan 9, 2017 at 10:43 PM, Eric Wong <e@80x24.org> wrote:
> alex0375@gmail.com wrote:
>> Am I missing something? Can this be a part of Rainbows?
>
> Nope, you're not missing anything :)
> Rainbows! was missing an EM feature introduced in 2012 :x
>
> I'll make a 5.1.0 release with following patches
> (coming in-reply-to this email) soon.
>
> There's also rack 2.x compatibility sitting in rainbows.git.
> https://bogomips.org/rainbows-public/20161117033531.GA4454@dcvr/T/
>
> The following changes since commit bd7596e50bd094edf5e5842afb8239c158fe2491:
>
>   Revert "t/t0044-autopush.sh: remove test" (2016-12-24 21:42:06 +0000)
>
> are available in the git repository at:
>
>   git://bogomips.org/rainbows em-deferred
>
> for you to fetch changes up to f385ed423e11ad40822f688bc592eaa78efa5b34:
>
>   eventmachine: wait for deferred actions to finish (2017-01-09 18:59:00 +0000)
>
> ----------------------------------------------------------------
> Eric Wong (2):
>       tests: re-enable EventMachine tests, again
>       eventmachine: wait for deferred actions to finish
>
>  SIGNALS                       |  6 +++---
>  lib/rainbows/event_machine.rb | 10 +++++++++-
>  t/GNUmakefile                 |  2 +-
>  t/app_deferred.ru             |  8 +++++++-
>  t/t0700-app-deferred.sh       | 18 +++++++++++++++---
>  5 files changed, 35 insertions(+), 9 deletions(-)

^ permalink raw reply	[flat|threaded] 5+ messages in thread

end of thread, back to index

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-01-09 13:43 Waiting for deferred actions to finish alex0375
2017-01-09 19:43 ` Eric Wong
2017-01-09 19:45   ` [PATCH 1/2] tests: re-enable EventMachine tests, again Eric Wong
2017-01-09 19:45   ` [PATCH 2/2] eventmachine: wait for deferred actions to finish Eric Wong
2017-01-10  7:31   ` alex0375

Rainbows! Rack HTTP server user/dev discussion

Archives are clonable:
	git clone --mirror http://bogomips.org/rainbows-public
	git clone --mirror http://ou63pmih66umazou.onion/rainbows-public

Newsgroups are available over NNTP:
	nntp://news.public-inbox.org/inbox.comp.lang.ruby.rainbows
	nntp://ou63pmih66umazou.onion/inbox.comp.lang.ruby.rainbows

 note: .onion URLs require Tor: https://www.torproject.org/
       or Tor2web: https://www.tor2web.org/

AGPL code for this site: git clone https://public-inbox.org/ public-inbox