Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ gemspec
gem 'bundler'
gem 'rake'
gem 'rspec'
gem 'activesupport'
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,16 @@ In order to be able to schedule jobs for future execution following [Scheduled J
#<Interactor::Context message="hello!", sidekiq_options={ queue: :low_priority }, sidekiq_schedule_options={ perform_in: 5 }>
```

### Customising the SidekiqWorker Class

You can declare you own class to use for the `::Sidekiq::Worker`. This is useful when raising errors and categorising queues.

```ruby
sidekiq_worker_class class MyWorkerClass < ::Interactor::SidekiqWorker::Worker; end
```

The class must inherit from `::Interactor::SidekiqWorker::Worker`.

## Failure

If you pass invalid parameters to sidekiq, you will get an immediate return with the error message.
Expand Down Expand Up @@ -85,11 +95,11 @@ class AsyncAction
def self.sidekiq_options
{ queue: :low_priority }
end

def self.sidekiq_schedule_options
{ perform_in: 5 }
end

def self.handle_sidekiq_exception(error)
# Integrate with Application Monitoring and Error Tracking Software
end
Expand Down
27 changes: 26 additions & 1 deletion lib/interactor/sidekiq.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ def self.included(base)
extend ClassMethods
extend SidekiqWorker
include Hooks
include SidekiqWorkerConfiguration

# Public: Gets the Interactor::Context of the Interactor instance.
attr_reader :context
Expand Down Expand Up @@ -60,7 +61,7 @@ def async_call(context = {})
options = handle_sidekiq_options(context)
schedule_options = delay_sidekiq_schedule_options(context)

Worker.set(options).perform_in(schedule_options.fetch(:delay, 0), handle_context_for_sidekiq(context))
worker_class.set(options).perform_in(schedule_options.fetch(:delay, 0), handle_context_for_sidekiq(context))
new(context.to_h).context
rescue Exception => e
begin
Expand All @@ -72,6 +73,14 @@ def async_call(context = {})

private

def worker_class
return Worker if custom_sidekiq_worker_class.nil?

return custom_sidekiq_worker_class if custom_sidekiq_worker_class < Worker

raise "#{klass} is not a valid Sidekiq worker class. It must be a subclass of ::Interactor::SidekiqWorker::Worker."
end

def handle_context_for_sidekiq(context)
context.to_h.merge(interactor_class: to_s)
end
Expand Down Expand Up @@ -100,6 +109,22 @@ def handle_sidekiq_schedule_options(context)
end
end

module SidekiqWorkerConfiguration
def self.included(base)
base.class_eval do
extend ClassMethods

class_attribute :custom_sidekiq_worker_class
end
end

module ClassMethods
def sidekiq_worker_class(klass)
self.custom_sidekiq_worker_class = klass
end
end
end

module Async
def self.included(base)
base.class_eval do
Expand Down
Loading