patient_http-sidekiq 1.1.2 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 25eda5f2b6779bc4c2208a6e57bf50bb98277706c97d7ff6bacf7368c369c842
4
- data.tar.gz: b110134e0fbb6a9ad8cee382c81d809267ebe0d5eb730a93fd10ce5871de2c1e
3
+ metadata.gz: e5ae5287613de3682de817fb6c434e6a1a33c21c1b8aa1dde7b7808c1733b927
4
+ data.tar.gz: 350e8f054d1047fe70482d9c3fc2f5473b077e1e7f1c64cc6f023c8bf76f62b4
5
5
  SHA512:
6
- metadata.gz: 62a51fd65f0d68bbe3223417bd663468ab4fe120704f6a4ddf32f6f220d057365588058061baa41c63aa33c3221dcba16105e85a0d5dba0c2c2e1dd365144626
7
- data.tar.gz: 28550b4fe12f889a9fa28e0e9bd25737693994940cba28d385207870909f019c88d69acc5248a55af302054f5352db6b08780772457b7617608f1677a3ad16c0
6
+ metadata.gz: d843ce929d22f48c20bc7f487b35d11421166218081f4fea2490440781d0a00839266c3cb1dec039611a6728e2213c3a71f0a966454ba40c1efc6ab466648ca7
7
+ data.tar.gz: 006cf97e99a6d27847b038c19065a88b09e28a94c7392fbee5af4253e9483652e8022b21e42c271840cfc8bff326fcfd490b137d1b08c8984760c68f6f7bc3df
data/CHANGELOG.md CHANGED
@@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.
4
4
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
5
5
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6
6
 
7
+ ## 1.2.0
8
+
9
+ ### Added
10
+
11
+ - `PatientHttp::Sidekiq.with_sidekiq_options` sets Sidekiq job options (queue, retry, etc.) at runtime for the requests enqueued within a block. When the options include a queue, the callback job for the request is enqueued on that queue as well.
12
+
7
13
  ## 1.1.2
8
14
 
9
15
  ### Fixed
data/README.md CHANGED
@@ -232,6 +232,21 @@ PatientHttp.execute(request: request, callback: MyCallback, callback_args: {user
232
232
 
233
233
  See the [patient_http docs](https://github.com/bdurand/patient_http) for the full `Request` and `Response` API reference.
234
234
 
235
+ ### Setting Sidekiq Options At Runtime
236
+
237
+ You can set Sidekiq job options for individual requests with `PatientHttp::Sidekiq.with_sidekiq_options`. The options apply to all requests enqueued within the block. Use this, for example, to route urgent requests to a higher priority queue:
238
+
239
+ ```ruby
240
+ PatientHttp::Sidekiq.with_sidekiq_options(queue: "high_priority") do
241
+ PatientHttp.get("https://api.example.com/users/123", callback: MyCallback)
242
+ end
243
+ ```
244
+
245
+ The options are applied with Sidekiq's `set` method, so any Sidekiq job option (`queue`, `retry`, etc.) is allowed. Notes on the behavior:
246
+
247
+ - If the options include a `queue`, the callback job that invokes your `on_complete`/`on_error` methods is enqueued on that queue as well, so the whole request keeps one priority end to end.
248
+ - Nested blocks merge their options, and the innermost values take precedence. The previous options are restored when the block exits, even if the block raises an error.
249
+
235
250
  ### Using Request Templates
236
251
 
237
252
  For repeated requests to the same API, use `PatientHttp::RequestTemplate` to share configuration:
@@ -446,6 +461,7 @@ PatientHttp::Sidekiq.configure do |config|
446
461
  config.payload_store_threshold = 64 * 1024
447
462
 
448
463
  # Sidekiq options for RequestWorker and CallbackWorker
464
+ # (use PatientHttp::Sidekiq.with_sidekiq_options to override per request)
449
465
  config.sidekiq_options = {queue: "patient_http", retry: 5}
450
466
 
451
467
  # Handler called when a callback job exhausts all Sidekiq retries
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.1.2
1
+ 1.2.0
@@ -28,7 +28,7 @@ module PatientHttp
28
28
  # @return [void]
29
29
  def on_complete(response, callback)
30
30
  data = store_if_needed(response.as_json)
31
- CallbackWorker.perform_async(data, "response", callback)
31
+ callback_worker.perform_async(data, "response", callback)
32
32
  delete_stored_request_payload
33
33
  end
34
34
 
@@ -42,7 +42,7 @@ module PatientHttp
42
42
  # @return [void]
43
43
  def on_error(error, callback)
44
44
  data = store_if_needed(error.as_json)
45
- CallbackWorker.perform_async(data, "error", callback)
45
+ callback_worker.perform_async(data, "error", callback)
46
46
  delete_stored_request_payload
47
47
  end
48
48
 
@@ -69,6 +69,18 @@ module PatientHttp
69
69
 
70
70
  private
71
71
 
72
+ # Returns CallbackWorker or a Sidekiq job setter that routes the callback
73
+ # job to the same queue as the request job when a runtime queue was set
74
+ # at enqueue time.
75
+ def callback_worker
76
+ queue = @sidekiq_job["patient_http_callback_queue"]
77
+ if queue
78
+ CallbackWorker.set(queue: queue)
79
+ else
80
+ CallbackWorker
81
+ end
82
+ end
83
+
72
84
  # Delete the externally stored request payload once the request has
73
85
  # completed. Until then the payload must remain fetchable because the
74
86
  # Sidekiq job hash referencing it can be re-pushed by Sidekiq retries,
@@ -18,6 +18,12 @@ require "patient_http"
18
18
  # callback_args: {user_id: 123}
19
19
  # )
20
20
  #
21
+ # Set Sidekiq job options at runtime for the requests enqueued within a block:
22
+ #
23
+ # PatientHttp::Sidekiq.with_sidekiq_options(queue: "high_priority") do
24
+ # PatientHttp::Sidekiq.execute(request, callback: MyCallback)
25
+ # end
26
+ #
21
27
  # Define a callback service class with +on_complete+ and +on_error+ methods:
22
28
  #
23
29
  # class MyCallback
@@ -209,6 +215,32 @@ module PatientHttp
209
215
  configuration.encryptor.decrypt(data)
210
216
  end
211
217
 
218
+ # Set Sidekiq job options for HTTP requests enqueued within the block.
219
+ # Options are applied with Sidekiq's `set` method (queue, retry, etc.).
220
+ # Nested calls merge options with the innermost values taking precedence.
221
+ # If the options include a queue, the callback job for the request is
222
+ # enqueued on that queue as well. Options only apply to requests enqueued
223
+ # in the same fiber as the block. This method has no effect
224
+ # when jobs run inline with Sidekiq::Testing.inline!.
225
+ #
226
+ # @param options [Hash] Sidekiq job options (symbol or string keys)
227
+ # @yield block within which enqueued requests use the options
228
+ # @return [Object] the return value of the block
229
+ def with_sidekiq_options(options)
230
+ unless options.is_a?(Hash)
231
+ raise ArgumentError.new("options must be a Hash, got: #{options.class}")
232
+ end
233
+ raise ArgumentError.new("with_sidekiq_options requires a block") unless block_given?
234
+
235
+ previous = Thread.current[:patient_http_sidekiq_options]
236
+ begin
237
+ Thread.current[:patient_http_sidekiq_options] = (previous || {}).merge(options.transform_keys(&:to_s))
238
+ yield
239
+ ensure
240
+ Thread.current[:patient_http_sidekiq_options] = previous
241
+ end
242
+ end
243
+
212
244
  # Execute an async HTTP request.
213
245
  #
214
246
  # @param request [PatientHttp::Request] the HTTP request to execute
@@ -236,7 +268,14 @@ module PatientHttp
236
268
  encrypted
237
269
  end
238
270
 
239
- RequestWorker.perform_async(data, callback_name, raise_error_responses, callback_args, request_id)
271
+ options = current_sidekiq_options
272
+ if options&.any?
273
+ queue = options["queue"]
274
+ options = options.merge("patient_http_callback_queue" => queue.to_s) if queue
275
+ RequestWorker.set(options).perform_async(data, callback_name, raise_error_responses, callback_args, request_id)
276
+ else
277
+ RequestWorker.perform_async(data, callback_name, raise_error_responses, callback_args, request_id)
278
+ end
240
279
 
241
280
  request_id
242
281
  end
@@ -347,6 +386,15 @@ module PatientHttp
347
386
  # @return [PatientHttp::Processor, nil]
348
387
  # @api private
349
388
  attr_accessor :processor
389
+
390
+ private
391
+
392
+ # Current scoped Sidekiq options, if any.
393
+ #
394
+ # @return [Hash, nil]
395
+ def current_sidekiq_options
396
+ Thread.current[:patient_http_sidekiq_options]
397
+ end
350
398
  end
351
399
  end
352
400
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: patient_http-sidekiq
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.2
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brian Durand