|
| 1 | +# Interactor::Sidekiq |
| 2 | + |
| 3 | +Provides [Interactor](https://github.com/collectiveidea/interactor) with asynchronous action using [Sidekiq](https://github.com/mperham/sidekiq). |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +```ruby |
| 8 | +gem 'interactor-sidekiq', '~> 1.0' |
| 9 | +``` |
| 10 | + |
| 11 | +## #async_call |
| 12 | +You can now add asynchronous behavior to both types of inetagents (basic interactors and organizers). |
| 13 | + |
| 14 | +```ruby |
| 15 | +class RegularAction |
| 16 | + include Interactor |
| 17 | + |
| 18 | + def call |
| 19 | + { context: context.variable } |
| 20 | + end |
| 21 | +end |
| 22 | +``` |
| 23 | +With the above example we can already use **async_call**. |
| 24 | + |
| 25 | +```sh |
| 26 | +>> RegularAction.call(key: 'value') |
| 27 | +#<Interactor::Context key="value"> |
| 28 | +>> Sidekiq::Queues.jobs_by_queue |
| 29 | +{} |
| 30 | +``` |
| 31 | +```sh |
| 32 | +>> RegularAction.async_call(key: 'value') |
| 33 | +#<Interactor::Context key="value"> |
| 34 | +>> Sidekiq::Queues.jobs_by_queue |
| 35 | +{"default"=>[{"retry"=>true, "queue"=>"default", "args"=>["{\"key\":\"value\",\"interactor_class\":\"RegularAction\"}"], "class"=>"Interactor::SidekiqWorker::Worker", "jid"=>"91a374e10e584b02cb84eec3", "created_at"=>1656283783.3459146, "enqueued_at"=>1656283783.3459556}]} |
| 36 | +``` |
| 37 | + |
| 38 | +You can pass the **sidekiq_options** and **sidekiq_scheduling_options** to customize the behavior of the **async_call** method. |
| 39 | + |
| 40 | +#### Passing options from sidekiq |
| 41 | + |
| 42 | +To set custom [sidekiq_options] (https://github.com/mperham/sidekiq/wiki/Advanced-Options#workers) you can add `sidekiq_options` class method in your interactors - these options will be passed to Sidekiq `` set ` before scheduling the asynchronous worker. |
| 43 | + |
| 44 | +#### Passing scheduling options |
| 45 | + |
| 46 | +In order to be able to schedule jobs for future execution following [Scheduled Jobs](https://github.com/mperham/sidekiq/wiki/Scheduled-Jobs), you can add the `sidekiq_schedule_options` class method in your subscriber definition - these options will be passed to Sidekiq's `perform_in` method when the worker is called. |
| 47 | + |
| 48 | +```sh |
| 49 | +>> RegularAction.async_call(message: 'hello!', sidekiq_options: { queue: :low_priority }, sidekiq_schedule_options: { perform_in: 5 }) |
| 50 | + |
| 51 | +Interactor::Context message: 'hello!', sidekiq_options: { queue: :low_priority }, sidekiq_schedule_options: { perform_in: 5 } |
| 52 | +``` |
| 53 | +
|
| 54 | +## Failure |
| 55 | +
|
| 56 | +If you pass invalid parameters to sidekiq, you will get an immediate return with the error message. |
| 57 | +```sh |
| 58 | +>> result = RegularAction.async_call(message: 'hello!', sidekiq_schedule_options: "error") |
| 59 | +#<Interactor::Context key="value", sidekiq_options="bad error message", error="undefined method `transform_keys' for \"bad error message\":String"> |
| 60 | +>> result.failure? |
| 61 | +true |
| 62 | +>> Sidekiq::Queues.jobs_by_queue |
| 63 | +{} |
| 64 | +``` |
| 65 | +
|
| 66 | +## Interactor::Async |
| 67 | +
|
| 68 | +Now you need an interactor to always assume asynchronous behavior using: **Interator::Async**. |
| 69 | +
|
| 70 | +#### Passing handle sidekiq exception |
| 71 | +
|
| 72 | +When executing the perform method in sidekiq there may be a problem, thinking about it we have already made it possible for you to handle this error. |
| 73 | +**If the context is failed during invocation of the interactor in background, the Interactor::Failure is raised**. |
| 74 | +
|
| 75 | +
|
| 76 | +```ruby |
| 77 | +class AsyncAction |
| 78 | + include Interactor::Async |
| 79 | + |
| 80 | + def call |
| 81 | + { context: context.variable } |
| 82 | + end |
| 83 | + |
| 84 | + def self.sidekiq_options |
| 85 | + { queue: :low_priority } |
| 86 | + end |
| 87 | + |
| 88 | + def self.sidekiq_schedule_options |
| 89 | + { perform_in: 5 } |
| 90 | + end |
| 91 | + |
| 92 | + def self.handle_sidekiq_exception(error) |
| 93 | + # Integrate with Application Monitoring and Error Tracking Software |
| 94 | + end |
| 95 | +end |
| 96 | +``` |
| 97 | +
|
| 98 | +## Compatibility |
| 99 | +
|
| 100 | +The same Ruby versions as Sidekiq are offically supported, but it should work |
| 101 | +with any 2.x syntax Ruby including JRuby and Rubinius. |
| 102 | +
|
| 103 | +## Running Specs |
| 104 | +
|
| 105 | +``` |
| 106 | +bundle exec rspec |
| 107 | +``` |
| 108 | +
|
| 109 | +## License |
| 110 | +
|
| 111 | +MIT |
0 commit comments