forked from ixti/sidekiq-throttled
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Unwrap ActiveJob arguments (ixti#184)
When using the `concurrency` strategy with dynamic suffix, I'm getting this error from `sidekiq-throttle`: `ArgumentError: wrong number of arguments (given 1, expected 2+)` This happens because the `key_suffix` I'm using is not receiving the proper arguments. Consider this dummy job: ```ruby class TestJob < ActiveJob::Base include Sidekiq::Throttled::Job queue_as :default sidekiq_throttle( concurrency: { limit: 1, key_suffix: ->(job_class, index, *) { "job_class:#{job_class}/index:#{index}" }, ttl: 1.hour.to_i } ) def perform(job_class, index) puts "Job Performed. job_class: #{job_class}, index: #{index}" end end ``` This follows the documentation, which states that `key_suffix` must accept the same parameters as the job. However, `sidekiq-throttle` fails to unwrap the `ActiveJob` arguments and instead it just calls the `:throttled?` and `:finalize!` methods passing them the arguments in `msg["args"]`, which in `ActiveJob` is an array of JSON objects, like this: ```json { "args": [ { "job_class": "TestJob", "job_id": "96f6c127-1e0c-4d8c-bdaa-c934b5aaded7", "provider_job_id": null, "queue_name": "default", "priority": null, "arguments": ["TestJob", 9], "executions": 0, "exception_executions": {}, "locale": "en", "timezone": null, "enqueued_at": "2024-03-15T11:31:11.464949297Z", "scheduled_at": null } ] } ``` Some other methods in the `Concurrency` class like `:count` or `:reset!` also call the `:key` method, so they are vulnerable to this error too. However, I haven't found any place in the code where these methods are called with any parameter. I wrote a very simple workaround that seems to work fine for my use case, and the test suite passes.
- Loading branch information
Showing
4 changed files
with
60 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters