When `#perform_later` is called the locale isn't stored on the queue, which results in a locale reset when the job is performed. An example of the problem: I18n.locale = 'de' HelloJob.perform_now # german message, correct but I18n.locale = 'de' HelloJob.perform_later # english message, incorrect This PR attaches the current I18n.locale to every job during the serialization process. It is then restored during deserialization and used to perform the job with the correct locale. It falls back to the default locale if no serialized locale is found in order to provide backward compatibility with previously stored jobs. It is not necessary to clear the queue for the update.
2.7 KiB
-
Include I18n.locale into job serialization/deserialization and use it around
perform
.Fixes #20799.
Johannes Opper
-
Allow
DelayedJob
,Sidekiq
,qu
, andque
to report the job id back toActiveJob::Base
asprovider_job_id
.Fixes #18821.
Kevin Deisz, Jeroen van Baarsen
-
assert_enqueued_jobs
andassert_performed_jobs
in block form use the given number as expected value. This makes the error message much easier to understand.y-yagi
-
A generated job now inherits from
app/jobs/application_job.rb
by default.Jeroen van Baarsen
-
Add an
:only
option toperform_enqueued_jobs
to filter jobs based on type.This allows specific jobs to be tested, while preventing others from being performed unnecessarily.
Example:
def test_hello_job assert_performed_jobs 1, only: HelloJob do HelloJob.perform_later('jeremy') LoggingJob.perform_later end end
An array may also be specified, to support testing multiple jobs.
Example:
def test_hello_and_logging_jobs assert_nothing_raised do assert_performed_jobs 2, only: [HelloJob, LoggingJob] do HelloJob.perform_later('jeremy') LoggingJob.perform_later('stewie') RescueJob.perform_later('david') end end end
Fixes #18802.
Michael Ryan
-
Allow keyword arguments to be used with Active Job.
Fixes #18741.
Sean Griffin
-
Add
:only
option toassert_enqueued_jobs
, to check the number of times a specific kind of job is enqueued.Example:
def test_logging_job assert_enqueued_jobs 1, only: LoggingJob do LoggingJob.perform_later HelloJob.perform_later('jeremy') end end
George Claghorn
-
ActiveJob::Base.deserialize
delegates to the job class.Since
ActiveJob::Base#deserialize
can be overridden by subclasses (likeActiveJob::Base#serialize
) this allows jobs to attach arbitrary metadata when they get serialized and read it back when they get performed.Example:
class DeliverWebhookJob < ActiveJob::Base def serialize super.merge('attempt_number' => (@attempt_number || 0) + 1) end def deserialize(job_data) super @attempt_number = job_data['attempt_number'] end rescue_from(TimeoutError) do |exception| raise exception if @attempt_number > 5 retry_job(wait: 10) end end
Isaac Seymour
Please check 4-2-stable for previous changes.