Class: Utilisable::BackgroundService
- Defined in:
- lib/utilisable/background_service.rb
Overview
Simple interface for running background jobs as commands. Meant to be inherited like this: class SidekiqService < Utilisable::BackgroundService
def initialize()
super(, ServiceObjectWorker)
end
end
Needs a worker to work, that should look like this: class ServiceObjectWorker
include Sidekiq::Worker
queue: :default, retry: true
def perform(klass, *args)
klass.constantize.call(args)
end
end
Now you can create a command like this: class SimpleService < SidekiqService
attr_reader :foo
def initialize(foo, = {})
super()
@foo = foo
end
private
def perform
puts foo
end
end
And invoke it like this: SimpleService.call(foo, background: true, perform_in: 15.minutes)
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#worker_class ⇒ Object
readonly
Returns the value of attribute worker_class.
Instance Method Summary collapse
- #call ⇒ Object
-
#initialize(options, worker_class) ⇒ BackgroundService
constructor
A new instance of BackgroundService.
Methods inherited from Service
#broadcast, call, #local_registrations, #on, #transaction
Constructor Details
#initialize(options, worker_class) ⇒ BackgroundService
Returns a new instance of BackgroundService.
45 46 47 48 |
# File 'lib/utilisable/background_service.rb', line 45 def initialize(, worker_class) @options = @worker_class = worker_class end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options
43 44 45 |
# File 'lib/utilisable/background_service.rb', line 43 def @options end |
#worker_class ⇒ Object (readonly)
Returns the value of attribute worker_class
43 44 45 |
# File 'lib/utilisable/background_service.rb', line 43 def worker_class @worker_class end |
Instance Method Details
#call ⇒ Object
50 51 52 53 54 |
# File 'lib/utilisable/background_service.rb', line 50 def call return broadcast(:fail) unless valid? background? ? setup_worker : perform end |