|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +# Adapter context for the :resque ActiveJob backend. |
| 4 | +# |
| 5 | +# Composes with "active_job backend harness" to drive resque entirely |
| 6 | +# in-memory via mock_redis — no live Redis required, mirroring how the |
| 7 | +# sidekiq context uses Sidekiq's fake mode. resque-scheduler is loaded so |
| 8 | +# the AJ adapter's +enqueue_at+ works; ActiveJob routes both scheduled |
| 9 | +# jobs (+wait:+) and +retry_on+ re-enqueues through +enqueue_at+, so the |
| 10 | +# delayed schedule has to be functional for the shared retry/scheduled |
| 11 | +# examples to pass. |
| 12 | +# |
| 13 | +# This context deliberately does NOT require sentry-resque. Loading it |
| 14 | +# would install resque's own Sentry integration (its server-side error |
| 15 | +# capture) and could register ResqueAdapter in skippable_job_adapters, |
| 16 | +# short-circuiting the AJ extension under test. |
| 17 | + |
| 18 | +# resque 3+ ships an ActiveJob adapter that inherits from |
| 19 | +# ActiveJob::QueueAdapters::AbstractAdapter, which only exists in Rails |
| 20 | +# 7.2+. Requiring resque on older Rails drags that adapter in (it pulls it |
| 21 | +# during app initialization), raising NameError, so don't even load the gem |
| 22 | +# there. The matching spec file applies the same Rails-version guard and |
| 23 | +# skips. RAILS_VERSION isn't defined yet at support-load time, so read |
| 24 | +# Rails.version directly. |
| 25 | +return if ::Rails.version.to_f < 7.2 |
| 26 | + |
| 27 | +begin |
| 28 | + require "mock_redis" |
| 29 | + require "resque" |
| 30 | + require "resque-scheduler" |
| 31 | +rescue LoadError |
| 32 | + # resque/mock_redis isn't bundled on this matrix (e.g. JRuby). The |
| 33 | + # matching spec file rescues the same LoadError and skips, so just |
| 34 | + # don't define the context here. |
| 35 | + return |
| 36 | +end |
| 37 | + |
| 38 | +RSpec.shared_context "resque adapter" do |
| 39 | + # ResqueAdapter is stateless; memoized once to mirror the other adapter |
| 40 | + # contexts (sidekiq, solid_queue). |
| 41 | + RESQUE_ADAPTER_FOR_TEST = ::ActiveJob::QueueAdapters::ResqueAdapter.new |
| 42 | + |
| 43 | + def queue_adapter_for_test |
| 44 | + RESQUE_ADAPTER_FOR_TEST |
| 45 | + end |
| 46 | + |
| 47 | + # Point resque at a fresh in-memory Redis for every example so no queue, |
| 48 | + # delayed-schedule, or +queues+ set state leaks between examples. |
| 49 | + # Resque memoizes its data store, so reassigning +Resque.redis+ rebuilds |
| 50 | + # it against the new MockRedis. |
| 51 | + def boot_adapter(_adapter) |
| 52 | + ::Resque.redis = ::MockRedis.new |
| 53 | + ::Resque.logger = ::Logger.new(nil) |
| 54 | + end |
| 55 | + |
| 56 | + def drain(at: nil) |
| 57 | + # resque has no in-process "run everything" helper, so we reserve and |
| 58 | + # perform jobs ourselves. +Resque::Job#perform+ runs the job and |
| 59 | + # re-raises any exception (after its failure hooks), which preserves |
| 60 | + # the +expect { drain }.to raise_error(...)+ semantics the shared |
| 61 | + # examples rely on. ActiveJob — not resque — owns retry: a failing |
| 62 | + # +retry_on+ job re-enqueues itself through the adapter's +enqueue_at+ |
| 63 | + # into resque-scheduler's delayed set, so each loop iteration first |
| 64 | + # promotes any now-due delayed jobs back onto their queues before |
| 65 | + # reserving. That cascades retries (and +wait: 0+ re-enqueues) to |
| 66 | + # completion within a single drain. |
| 67 | + run = lambda do |
| 68 | + loop do |
| 69 | + promote_due_delayed_jobs(Time.current) |
| 70 | + job = reserve_next_job |
| 71 | + break if job.nil? |
| 72 | + |
| 73 | + job.perform |
| 74 | + end |
| 75 | + end |
| 76 | + |
| 77 | + # Only wrap in travel_to when the caller explicitly asks for a future |
| 78 | + # time (e.g. the scheduled_at example) so that delayed jobs scheduled |
| 79 | + # in the future become due — otherwise a nested travel_to from a spec |
| 80 | + # that already called +travel+ would raise. |
| 81 | + at ? travel_to(at, &run) : run.call |
| 82 | + end |
| 83 | + |
| 84 | + def last_enqueued_payload |
| 85 | + # The AJ-on-resque adapter wraps the serialized AJ payload as the sole |
| 86 | + # element of the resque job's +args+ (see ResqueAdapter#enqueue / |
| 87 | + # JobWrapper.perform). resque pushes to the tail and pops from the |
| 88 | + # head, so the most recently enqueued job sits at the end of its |
| 89 | + # queue. The shared example that reads this enqueues a single job, so |
| 90 | + # returning the tail of the first non-empty queue is sufficient. |
| 91 | + ::Resque.queues.each do |queue| |
| 92 | + size = ::Resque.size(queue) |
| 93 | + next if size.zero? |
| 94 | + |
| 95 | + item = ::Resque.peek(queue, size - 1) |
| 96 | + return item["args"].first if item |
| 97 | + end |
| 98 | + |
| 99 | + nil |
| 100 | + end |
| 101 | + |
| 102 | + private |
| 103 | + |
| 104 | + # Move every delayed job whose scheduled timestamp is at or before |
| 105 | + # +up_to+ out of resque-scheduler's delayed set and back onto its |
| 106 | + # destination queue, ready to be reserved. |
| 107 | + def promote_due_delayed_jobs(up_to) |
| 108 | + while (timestamp = ::Resque.next_delayed_timestamp(up_to)) |
| 109 | + while (item = ::Resque.next_item_for_timestamp(timestamp)) |
| 110 | + klass = ::Resque.constantize(item["class"]) |
| 111 | + queue = item["queue"] || ::Resque.queue_from_class(klass) |
| 112 | + ::Resque.enqueue_to(queue, klass, *item["args"]) |
| 113 | + end |
| 114 | + end |
| 115 | + end |
| 116 | + |
| 117 | + # Reserve (pop) the next job from any non-empty queue, or nil when every |
| 118 | + # queue is empty. |
| 119 | + def reserve_next_job |
| 120 | + ::Resque.queues.each do |queue| |
| 121 | + job = ::Resque.reserve(queue) |
| 122 | + return job if job |
| 123 | + end |
| 124 | + |
| 125 | + nil |
| 126 | + end |
| 127 | +end |
0 commit comments