Skip to content

Commit

Permalink
feat: add back lifecycle
Browse files Browse the repository at this point in the history
  • Loading branch information
marcosgz committed Feb 11, 2025
1 parent 19b151c commit 750087d
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 47 deletions.
79 changes: 40 additions & 39 deletions lib/lepus/lifecycle_hooks.rb
Original file line number Diff line number Diff line change
@@ -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
13 changes: 5 additions & 8 deletions lib/lepus/supervisor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module Lepus
class Supervisor < Processes::Base
# include LifecycleHooks
include LifecycleHooks
include Maintenance
include Signals
include Pidfiled
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 750087d

Please sign in to comment.