-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
45 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters