Module: Subserver

Defined in:
lib/subserver.rb,
lib/subserver/cli.rb,
lib/subserver/util.rb,
lib/subserver/rails.rb,
lib/subserver/health.rb,
lib/subserver/pubsub.rb,
lib/subserver/logging.rb,
lib/subserver/manager.rb,
lib/subserver/testing.rb,
lib/subserver/version.rb,
lib/subserver/launcher.rb,
lib/subserver/listener.rb,
lib/subserver/subscriber.rb,
lib/subserver/message_logger.rb,
lib/subserver/testing/pubsub.rb,
lib/subserver/middleware/chain.rb,
lib/subserver/exception_handler.rb,
lib/subserver/middleware/active_record.rb

Defined Under Namespace

Modules: ExceptionHandler, Logging, Middleware, Pubsub, Subscriber, Testing, Util Classes: CLI, Health, Launcher, Listener, Manager, MessageLogger, Rails, Shutdown

Constant Summary collapse

NAME =
'Subserver'
LICENSE =
'Subserver is licensed under MIT.'
DEFAULTS =
{
  project_id: nil,
  credentials: nil,
  queues: [],
  labels: [],
  require: '.',
  subscriber_dir: './subscribers',
  environment: nil,
  timeout: 35,
  error_handlers: [],
  death_handlers: [],
  lifecycle_events: {
    startup: [],
    listener_startup: [],
    quiet: [],
    shutdown: [],
    heartbeat: [],
  },
  reloader: proc { |&block| block.call },
}
DEFAULT_SUBSCRIBER_OPTIONS =
{
  subscription: nil,
  deadline: 60,
  streams: 2, 
  threads: {
    callback: 4,
    push: 2
  },
  inventory: 1000,
  queue: 'default'
}
VERSION =
"0.5.1"

Class Method Summary collapse

Class Method Details

.configure {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:

  • _self (Subserver)

    the object that the method was called on



58
59
60
# File 'lib/subserver.rb', line 58

def self.configure
  yield self
end

.death_handlersObject

Death handlers are called when all retries for a job have been exhausted and the job dies. It’s the notification to your application that this job will not succeed without manual intervention.

Subserver.configure do |config|

config.death_handlers << ->(job, ex) do
end

end



119
120
121
# File 'lib/subserver.rb', line 119

def self.death_handlers
  options[:death_handlers]
end

.default_middlewareObject



99
100
101
# File 'lib/subserver.rb', line 99

def self.default_middleware
  Middleware::Chain.new
end

.default_subscriber_optionsObject



107
108
109
# File 'lib/subserver.rb', line 107

def self.default_subscriber_options
  defined?(@default_subscriber_options) ? @default_subscriber_options : DEFAULT_SUBSCRIBER_OPTIONS
end

.default_subscriber_options=(hash) ⇒ Object



103
104
105
# File 'lib/subserver.rb', line 103

def self.default_subscriber_options=(hash)
  @default_subscriber_options = default_subscriber_options.merge(Hash[hash.map{|k, v| [k.to_s, v]}])
end

.dump_json(object) ⇒ Object



73
74
75
# File 'lib/subserver.rb', line 73

def self.dump_json(object)
  JSON.generate(object)
end

.error_handlersObject

Register a proc to handle any error which occurs within the Subserver process.

Subserver.configure do |config|
  config.error_handlers << proc {|ex,ctx_hash| MyErrorService.notify(ex, ctx_hash) }
end

The default error handler logs errors to Subserver.logger.



130
131
132
# File 'lib/subserver.rb', line 130

def self.error_handlers
  self.options[:error_handlers]
end

.health_serverObject



85
86
87
# File 'lib/subserver.rb', line 85

def self.health_server
  @health_server ||= Subserver::Health.new
end

.load_config(file = nil) ⇒ Object



62
63
64
65
66
67
# File 'lib/subserver.rb', line 62

def self.load_config(file=nil)
  opts = DEFAULTS.dup
  file = Dir["config/subserver.yml*"].first if file.nil?
  return opts unless file && File.exists?(file)
  opts.merge(parse_config(file))
end

.load_json(string) ⇒ Object



69
70
71
# File 'lib/subserver.rb', line 69

def self.load_json(string)
  JSON.parse(string)
end

.loggerObject



77
78
79
# File 'lib/subserver.rb', line 77

def self.logger
  Subserver::Logging.logger
end

.logger=(log) ⇒ Object



81
82
83
# File 'lib/subserver.rb', line 81

def self.logger=(log)
  Subserver::Logging.logger = log
end

.middleware {|@chain| ... } ⇒ Object

Yields:

  • (@chain)


93
94
95
96
97
# File 'lib/subserver.rb', line 93

def self.middleware
  @chain ||= default_middleware
  yield @chain if block_given?
  @chain
end

.on(event, &block) ⇒ Object

Register a block to run at a point in the Subserver lifecycle. :startup, :quiet or :shutdown are valid events.

Subserver.configure do |config|
  config.on(:shutdown) do
    puts "Goodbye cruel world!"
  end
end

Raises:

  • (ArgumentError)


142
143
144
145
146
# File 'lib/subserver.rb', line 142

def self.on(event, &block)
  raise ArgumentError, "Symbols only please: #{event}" unless event.is_a?(Symbol)
  raise ArgumentError, "Invalid event name: #{event}" unless options[:lifecycle_events].key?(event)
  options[:lifecycle_events][event] << block
end

.optionsObject



50
51
52
# File 'lib/subserver.rb', line 50

def self.options
  @options ||= load_config
end

.options=(opts) ⇒ Object



54
55
56
# File 'lib/subserver.rb', line 54

def self.options=(opts)
  @options = opts
end

.pubsub_clientObject



89
90
91
# File 'lib/subserver.rb', line 89

def self.pubsub_client
  Subserver::Pubsub.client
end