From 750087d4af0de97abe5a78fc05ac086260415a62 Mon Sep 17 00:00:00 2001 From: "Marcos G. Zimmermann" Date: Tue, 11 Feb 2025 09:41:49 -0300 Subject: [PATCH] feat: add back lifecycle --- lib/lepus/lifecycle_hooks.rb | 79 ++++++++++++++++++------------------ lib/lepus/supervisor.rb | 13 +++--- 2 files changed, 45 insertions(+), 47 deletions(-) diff --git a/lib/lepus/lifecycle_hooks.rb b/lib/lepus/lifecycle_hooks.rb index a115fec..8cc1f28 100644 --- a/lib/lepus/lifecycle_hooks.rb +++ b/lib/lepus/lifecycle_hooks.rb @@ -1,48 +1,49 @@ # frozen_string_literal: true module Lepus + # @TODO: Move after/before fork hooks to this module module LifecycleHooks def self.included(base) - # base.extend ClassMethods - # base.send :include, InstanceMethods - # base.instance_variable_set(:@lifecycle_hooks, {start: [], stop: []}) + base.extend ClassMethods + base.send :include, InstanceMethods + base.instance_variable_set(:@lifecycle_hooks, {start: [], stop: []}) end - # module ClassMethods - # attr_reader :lifecycle_hooks - - # def on_start(&block) - # lifecycle_hooks[:start] << block - # end - - # def on_stop(&block) - # lifecycle_hooks[:stop] << block - # end - - # def clear_hooks - # lifecycle_hooks[:start] = [] - # lifecycle_hooks[:stop] = [] - # end - # end - - # module InstanceMethods - # private - - # def run_start_hooks - # run_hooks_for :start - # end - - # def run_stop_hooks - # run_hooks_for :stop - # end - - # def run_hooks_for(event) - # self.class.lifecycle_hooks.fetch(event, []).each do |block| - # block.call - # rescue Exception => exception # rubocop:disable Lint/RescueException - # handle_thread_error(exception) - # end - # end - # end + module ClassMethods + attr_reader :lifecycle_hooks + + def on_start(&block) + lifecycle_hooks[:start] << block + end + + def on_stop(&block) + lifecycle_hooks[:stop] << block + end + + def clear_hooks + lifecycle_hooks[:start] = [] + lifecycle_hooks[:stop] = [] + end + end + + module InstanceMethods + private + + def run_start_hooks + run_hooks_for :start + end + + def run_stop_hooks + run_hooks_for :stop + end + + def run_hooks_for(event) + self.class.lifecycle_hooks.fetch(event, []).each do |block| + block.call + rescue Exception => exception # rubocop:disable Lint/RescueException + handle_thread_error(exception) + end + end + end end end diff --git a/lib/lepus/supervisor.rb b/lib/lepus/supervisor.rb index 57c50c1..811e07e 100644 --- a/lib/lepus/supervisor.rb +++ b/lib/lepus/supervisor.rb @@ -2,7 +2,7 @@ module Lepus class Supervisor < Processes::Base - # include LifecycleHooks + include LifecycleHooks include Maintenance include Signals include Pidfiled @@ -25,17 +25,17 @@ def initialize(configuration) def start boot - # run_start_hooks + run_start_hooks start_processes - # launch_maintenance_task + launch_maintenance_task supervise end def stop super - # run_stop_hooks + run_stop_hooks end private @@ -125,6 +125,7 @@ def terminate_gracefully term_forks shutdown_timeout = 5 + puts "\nWaiting up to #{shutdown_timeout} seconds for processes to terminate gracefully..." Timer.wait_until(shutdown_timeout, -> { all_forks_terminated? }) do reap_terminated_forks end @@ -180,10 +181,6 @@ def reap_terminated_forks pid, _ = ::Process.waitpid2(-1, ::Process::WNOHANG) break unless pid - # if (terminated_fork = forks.delete(pid)) && (!status.exited? || status.exitstatus > 0) - # handle_claimed_jobs_by(terminated_fork, status) - # end - configured_processes.delete(pid) end rescue SystemCallError