module Servitude::ServerThreaded

Public Class Methods

included( base ) click to toggle source
# File lib/servitude/server_threaded.rb, line 6
def self.included( base )
  base.class_eval do
    after_initialize :initialize_thread
    after_initialize :initialize_celluloid_logger
  end
end

Protected Instance Methods

call_handler_respecting_thread_count( options ) click to toggle source

Correctly calls a single supervised actor when the threads configuraiton is set to 1, or a pool of actors if threads configuration is > 1. Also protects against a supervised actor from being nil if the supervisor is reinitializing when access is attempted.

# File lib/servitude/server_threaded.rb, line 71
def call_handler_respecting_thread_count( options )
  if config.threads > 1
    pool.async.call( options )
  else
    raise Servitude::SupervisionError unless handler
    handler.call( options )
  end
end
format_backtrace( backtrace ) click to toggle source
# File lib/servitude/server_threaded.rb, line 62
def format_backtrace( backtrace )
  "  #{backtrace.join "\n  "}"
end
handle_error( options, e ) click to toggle source
# File lib/servitude/server_threaded.rb, line 57
def handle_error( options, e )
  parts = [[e.class.name, e.message].join( ' ' ), format_backtrace( e.backtrace )]
  error( parts.join( "\n" ))
end
handler() click to toggle source
# File lib/servitude/server_threaded.rb, line 84
def handler
  Celluloid::Actor[:handler]
end
handler_class() click to toggle source
# File lib/servitude/server_threaded.rb, line 15
def handler_class
  raise NotImplementedError
end
initialize_celluloid_logger() click to toggle source
# File lib/servitude/server_threaded.rb, line 88
def initialize_celluloid_logger
  Celluloid.logger = nil
end
initialize_thread() click to toggle source
# File lib/servitude/server_threaded.rb, line 92
def initialize_thread
  return unless config.threads == 1
  handler_class.supervise_as :handler
end
notify_and_sleep_if_configured() click to toggle source
# File lib/servitude/server_threaded.rb, line 41
def notify_and_sleep_if_configured
  if config.supervision_retry_timeout_in_seconds &&
      config.supervision_retry_timeout_in_seconds > 0
    debug "Sleeping for #{config.supervision_retry_timeout_in_seconds}s ..."
    sleep( config.supervision_retry_timeout_in_seconds )
  end
end
pool() click to toggle source
# File lib/servitude/server_threaded.rb, line 80
def pool
  @pool ||= handler_class.pool( size: config.threads )
end
run() click to toggle source
# File lib/servitude/server_threaded.rb, line 19
def run
  raise NotImplementedError
end
warn_for_dead_actor_error() click to toggle source
# File lib/servitude/server_threaded.rb, line 53
def warn_for_dead_actor_error
  warn "RETRYING due to Celluloid::DeadActorError ..."
end
warn_for_supevision_error() click to toggle source
# File lib/servitude/server_threaded.rb, line 49
def warn_for_supevision_error
  warn "RETRYING due to waiting on supervisor to restart actor ..."
end
with_supervision( options={}, &block ) click to toggle source
# File lib/servitude/server_threaded.rb, line 23
def with_supervision( options={}, &block )
  begin
    block.call
  rescue Servitude::SupervisionError
    # supervisor is restarting actor
    warn_for_supevision_error
    notify_and_sleep_if_configured
    retry
  rescue Celluloid::DeadActorError
    # supervisor has yet to begin restarting actor
    warn_for_dead_actor_error
    notify_and_sleep_if_configured
    retry
  rescue => e
    handle_error( options, e )
  end
end