Module: Faktory

Defined in:
lib/faktory/mutate.rb,
lib/faktory.rb,
lib/faktory/cli.rb,
lib/faktory/cli.rb,
lib/faktory/job.rb,
lib/faktory/util.rb,
lib/faktory/batch.rb,
lib/faktory/fetch.rb,
lib/faktory/rails.rb,
lib/faktory/client.rb,
lib/faktory/logging.rb,
lib/faktory/manager.rb,
lib/faktory/testing.rb,
lib/faktory/version.rb,
lib/faktory/launcher.rb,
lib/faktory/tracking.rb,
lib/faktory/processor.rb,
lib/faktory/connection.rb,
lib/faktory/job_logger.rb,
lib/faktory/middleware/chain.rb,
lib/faktory/exception_handler.rb

Overview

require β€˜faktory/mutate’ cl = Faktory::Client.new cl.discard(Faktory::RETRIES) do |filter|

filter.with_type("QuickBooksSyncJob")
filter.matching("*uid:12345*"))

end

Defined Under Namespace

Modules: ExceptionHandler, Job, Logging, Middleware, Mutator, Queues, Testing, Trackable, Util Classes: BaseError, Batch, BatchStatus, CLI, Client, CommandError, Connection, EmptyQueueError, Fetcher, JobLogger, Launcher, Manager, ParseError, Processor, Rails, Shutdown, UnitOfWork

Constant Summary collapse

NAME =
'Faktory'.freeze
LICENSE =
'See LICENSE and the LGPL-3.0 for licensing details.'
DEFAULTS =
{
  queues: ['default'],
  concurrency: 10,
  require: '.',
  environment: 'development',
  # As of 2017, Heroku's process timeout is 30 seconds.
  # After 30 seconds, processes are KILLed so assume 25
  # seconds to gracefully shutdown and 5 seconds to hard
  # shutdown.
  timeout: 25,
  error_handlers: [],
  lifecycle_events: {
    startup: [],
    quiet: [],
    shutdown: [],
  },
  reloader: proc { |&block| block.call },
}
DEFAULT_JOB_OPTIONS =
{
  'retry' => 25,
  'queue' => 'default'
}
RETRIES =

Valid targets

"retries"
SCHEDULED =
"scheduled"
DEAD =
"dead"
VERSION =
"1.0.0"

Class Method Summary collapse

Class Method Details

.client_middleware {|@client_chain| ... } ⇒ Object

Yields:

  • (@client_chain)


90
91
92
93
94
# File 'lib/faktory.rb', line 90

def self.client_middleware
  @client_chain ||= Middleware::Chain.new
  yield @client_chain if block_given?
  @client_chain
end

.configure_client {|_self| ... } ⇒ Object

Configuration for Faktory client, use like:

Faktory.configure_client do |config|
  config.default_job_options = { retry: 3 }
end

Yields:

  • (_self)

Yield Parameters:

  • _self (Faktory)

    the object that the method was called on



67
68
69
# File 'lib/faktory.rb', line 67

def self.configure_client
  yield self unless worker?
end

.configure_worker {|_self| ... } ⇒ Object

Configuration for Faktory executor, use like:

Faktory.configure_worker do |config|
  config.worker_middleware do |chain|
    chain.add MyServerHook
  end
  config.default_job_options = { retry: 3 }
end

Yields:

  • (_self)

Yield Parameters:

  • _self (Faktory)

    the object that the method was called on



57
58
59
# File 'lib/faktory.rb', line 57

def self.configure_worker
  yield self if worker?
end

.default_job_optionsObject



106
107
108
# File 'lib/faktory.rb', line 106

def self.default_job_options
  defined?(@default_job_options) ? @default_job_options : DEFAULT_JOB_OPTIONS
end

.default_job_options=(hash) ⇒ Object



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

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

.dump_json(object) ⇒ Object



113
114
115
# File 'lib/faktory.rb', line 113

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

.error_handlersObject

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

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

The default error handler logs errors to Faktory.logger.



140
141
142
# File 'lib/faktory.rb', line 140

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

.faktory=(hash) ⇒ Object



86
87
88
# File 'lib/faktory.rb', line 86

def self.faktory=(hash)
  @pool = Faktory::Connection.create(hash)
end

.load_json(string) ⇒ Object



110
111
112
# File 'lib/faktory.rb', line 110

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

.loggerObject



117
118
119
# File 'lib/faktory.rb', line 117

def self.logger
  Faktory::Logging.logger
end

.logger=(log) ⇒ Object



120
121
122
# File 'lib/faktory.rb', line 120

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

.on(event, &block) ⇒ Object

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

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

Raises:

  • (ArgumentError)


152
153
154
155
156
# File 'lib/faktory.rb', line 152

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



41
42
43
# File 'lib/faktory.rb', line 41

def self.options
  @options ||= DEFAULTS.dup
end

.options=(opts) ⇒ Object



44
45
46
# File 'lib/faktory.rb', line 44

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

.serverObject

Raises:

  • (ArgumentError)


75
76
77
78
79
80
# File 'lib/faktory.rb', line 75

def self.server
  raise ArgumentError, "requires a block" unless block_given?
  server_pool.with do |conn|
    yield conn
  end
end

.server_poolObject



82
83
84
# File 'lib/faktory.rb', line 82

def self.server_pool
  @pool ||= Faktory::Connection.create
end

.worker?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/faktory.rb', line 71

def self.worker?
  defined?(Faktory::CLI)
end

.worker_middleware {|@worker_chain| ... } ⇒ Object

Yields:

  • (@worker_chain)


96
97
98
99
100
# File 'lib/faktory.rb', line 96

def self.worker_middleware
  @worker_chain ||= Middleware::Chain.new
  yield @worker_chain if block_given?
  @worker_chain
end

.πŸ’ƒπŸ•Ί(io = $stdout) ⇒ Object



124
125
126
127
128
129
130
131
# File 'lib/faktory.rb', line 124

def self.πŸ’ƒπŸ•Ί(io = $stdout)
  colors = (31..37).to_a
  sz = colors.size
  "DANCE MODE ACTIVATED".chars.each_with_index do |chr, idx|
    io.print("\e[#{colors[rand(sz)]};1m#{chr}")
  end
  io.print("\e[0m\n")
end