Skip to content

Commit

Permalink
disable lifecycle hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
marcosgz committed Jan 23, 2025
1 parent e85e651 commit f8e5d85
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 46 deletions.
3 changes: 3 additions & 0 deletions lib/lepus.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ def initialize(value)
class InvalidConsumerConfigError < Error
end

class ShutdownError < Error
end

module Processes
class ProcessMissingError < RuntimeError
def initialize
Expand Down
4 changes: 2 additions & 2 deletions lib/lepus/consumer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def config
return if @abstract_class == true
return @config if defined?(@config)

name = name.split("::").last.gsub(/([a-z\d])([A-Z])/, '\1_\2').downcase
name = Primitive::String.new(self).underscore.split("/").last
@config = ConsumerConfig.new(queue: name, exchange: name)
end

Expand All @@ -43,7 +43,7 @@ def use(middleware, opts = {})
if middleware.is_a?(Symbol) || middleware.is_a?(String)
begin
require_relative "middlewares/#{middleware}"
class_name = middleware.to_s.split("_").map(&:capitalize).join
class_name = Primitive::String.new(middleware).classify
class_name = "JSON" if class_name == "Json"
middleware = Lepus::Middlewares.const_get(class_name)
rescue LoadError, NameError
Expand Down
78 changes: 39 additions & 39 deletions lib/lepus/lifecycle_hooks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,46 +3,46 @@
module Lepus
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
3 changes: 2 additions & 1 deletion lib/lepus/processes/consumer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ class Consumer < Base

def initialize(class_name:, **options)
@consumer_class = class_name
@consumer_class = @consumer_class.constantize if @consumer_class.is_a?(String)
@consumer_class = Lepus::Primitive::String.new(@consumer_class).constantize if @consumer_class.is_a?(String)

super(**options)
end

Expand Down
2 changes: 2 additions & 0 deletions lib/lepus/processes/interruptible.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
module Lepus::Processes
module Interruptible
def wake_up
puts "[Interruptible |> #{self.class.name}] Waking up process..."
interrupt
end

Expand All @@ -11,6 +12,7 @@ def wake_up
SELF_PIPE_BLOCK_SIZE = 11

def interrupt
puts "[Interruptible |> #{self.class.name}] Interrupting process..."
self_pipe[:writer].write_nonblock(".")
rescue Errno::EAGAIN, Errno::EINTR
# Ignore writes that would block and retry
Expand Down
8 changes: 4 additions & 4 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

0 comments on commit f8e5d85

Please sign in to comment.