Commit Graph

563 Commits

Author SHA1 Message Date
zzak
dd89f600f7
🔗 Remove RDoc auto-link from Rails module everywhere 2023-06-23 10:49:30 +09:00
zzak
38bef29064
Replace all occurrences of '<tt>(\w+::\w+::\w+)</tt>' with '+$1+'
E.g.:

* <tt>Rails::Command::NotesCommand</tt> -> +Rails::Command::NotesCommand+

Co-authored-by: Hartley McGuire <skipkayhil@gmail.com>
2023-05-25 06:56:17 +09:00
zzak
e3c73fd183
Replace all occurrences of '<tt>(\w+::\w+)</tt>' with '+$1+'
E.g.:

* <tt>ActiveRecord::Base</tt> -> +ActiveRecord::Base+

Co-authored-by: Hartley McGuire <skipkayhil@gmail.com>
Co-authored-by: Petrik de Heus <petrik@deheus.net>
2023-05-25 06:52:32 +09:00
Shivam Chahar
ad2280afc6 Docs: Fix queue_as doc wording [ci skip]
Changing the wording of `queue_as` doc as per https://github.com/rails/rails/pull/48099#discussion_r1197516258
2023-05-22 19:59:55 +05:30
zzak
398b904c3e
Merge pull request #48099 from shivamsinghchahar/aj-document-queue-with-priority-block
Document queue_with_priority block arguments and their use [ci skip]
2023-05-21 08:22:01 +09:00
Hartley McGuire
d028c14b03
Link rescue_from from modules including Rescuable
Also add some additional words to make it clear that the modules also
implement handling the exceptions configured with rescue_from, because
it was not immediately clear that happened without reading the code.
2023-05-18 12:54:16 -04:00
Shivam Chahar
55ffc6bfe3 Document queue_with_priority block arguments and their use
Currently, we don't document the use case for ActiveJob's `queue_with_priority` block arguments. It seems necessary to document them in the API docs as well considering how useful this option is.
2023-05-18 13:07:02 +05:30
Jordan Brough
fb1dcea6f3 Remove extraneous case condition
In commit 72300f97 the code for the `String` condition was simplified, and it's
now the same as the code for the `*PERMITTED_TYPES` condition, and `String` is
one of the `PERMITTED_TYPES` so we can just eliminate this condition and let
`*PERMITTED_TYPES` cover it.
2023-05-11 07:13:52 -06:00
Sander Verdonschot
e41d1acc91
Improve queue adapter name extraction for logs
We recently made improvements to the name extraction for `MyJob.queue_adapter_name` (see https://github.com/rails/rails/pull/47995 and https://github.com/rails/rails/pull/48003). I had overlooked that we also extract the queue adapter name in a similar (but not identical) way for log messages, so this change aligns the two.
2023-05-02 08:37:53 -04:00
Shivam Chahar
562846ae0c Allow symbols as queue names in activejob assertions 2023-04-23 18:53:48 +05:30
Sander Verdonschot
6daba18791
Allow queue adapters to provide a custom name
This gives queue adapters more freedom to name and organize their code.

For example, if `FancyQueue` wants to have their adapter at
`FancyQueue::ActiveJobAdapter`, the name would be `active_job` before this
change. After this change, they can implement `queue_adapter_name` to return
`fancy_queue`.
2023-04-20 13:23:50 -04:00
Sander Verdonschot
6b4acb57e2
Fix name extraction for module queue adapters
Before this change, setting a module as queue adapter would result in
`queue_adapter_name` being `"module"`. This PR fixes the name extraction logic
to handle module or class queue adapters.
2023-04-19 18:51:49 -04:00
fatkodima
5ab2034730 [ActiveJob] Add logging for enqueue_all 2023-04-04 13:18:19 +03:00
fatkodima
f6d56fed27 Add support for logging background job enqueue callers 2023-04-02 03:23:42 +03:00
Petrik
31b94b1a2e Use h1 for Active Job documentation titles [ci-skip]
Most of the other frameworks use a h1(`=`) instead of h2(`==`) for
class/module documentation. Having a h1 will improve SEO and makes
things look more consistent.

This also adds a missing title and escapes namespaces so it won't be
linked.
2023-04-01 15:36:48 +02:00
Cameron Matheson
0cb0880b1a implement provider_job_id for Backburner jobs 2023-03-23 19:19:34 -06:00
Pascal Zumkehr
3d3ed6eeb0 Make delayed job display_name failsafe
The display_name method is used by delayed job to log information
about a certain job, including failure messages. Whenever a job class
is moved or deleted, the instances still scheduled cannot be
constantized anymore, causing display_name and hence the log method to
raise an exception. In certain cases, e.g. when logging happens in a
rescue block, this may terminate the entire delayed job worker. With
the failsafe method, the worker handles failed jobs gracefully and
continues work, all with appropriate log output.
2023-03-02 13:38:38 +01:00
Étienne Barrié
c326532004 Remove deprecation warning without a deprecator
The initial deprecation was introduced in 6.1:
bbfab0b33abd7481b8a801b672d7b2316609315a

The configuration stayed with a deprecation in 7.0 but was not
preserving the behavior: 10bd5e59c3291bb2034869e97cca30634a559afb

This removes the configuration and deprecation.
2023-02-15 17:23:20 +01:00
Sander Verdonschot
9b62f88a2f
Add perform_all_later to enqueue multiple jobs at once
Sidekiq has a useful optimisation called `push_bulk` that enqueues many jobs at
once, eliminating the repeated Redis roundtrips. However, this feature is not
exposed through Active Job, so it only works for `Sidekiq::Worker` jobs. This
adds a barrier to Active Job adoption for apps that rely on this feature. It
also makes it harder for other queue adapters to implement similar
functionality, as they then have to take care of serialization, callbacks, etc.
themselves.

This commit adds `ActiveJob.perform_all_later(<job1>, <job2>)`, backed by
Sidekiq's `push_bulk` and with a fallback to enqueuing serially if the queue
adapter does not support bulk enqueue.

The performance benefit for 1000 jobs can be more than an order of magnitude:

| Enqueue type       | Serial time (ms) | Bulk time (ms) | Speedup |
| ------------------ | ---------------- | -------------- | ------- |
| Raw Sidekiq        |             2661 |            119 |     22x |
| Active Job Sidekiq |             2853 |            208 |     14x |

(Measured in a simple test app in our production environment.)

Instrumentation for perform_all_later uses a new event `enqueue_all.active_job`
2023-02-02 16:39:22 -05:00
Étienne Barrié
3d6a7b2faa Initialize deprecators before configuring them
Since engine initializers run later in the process, we need to run this
initializer earlier than the default.

This ensures they're all registered before the environments are loaded.
2022-11-28 10:47:26 +01:00
Jonathan Hefner
77db302aa0 Add ActiveJob.deprecator
This commit adds `ActiveJob.deprecator` and replaces all usages of
`ActiveSupport::Deprecation.warn` in `activejob/lib` with
`ActiveJob.deprecator`.

Additionally, this commit adds `ActiveJob.deprecator` to
`Rails.application.deprecators` so that it can be configured via
settings such as `config.active_support.report_deprecations`.

This commit also removes a defunct `ActiveSupport::Deprecation.silence`
call that was added in 9eb4b4ed01f893c2fc7ff3ee4f99301dec7ea3c2 but not
removed when the deprecation was completed in
10bd5e59c3291bb2034869e97cca30634a559afb.
2022-10-31 17:04:26 -05:00
Yasuo Honda
50f8b85fc8
Merge pull request #46258 from arielvalentin/fix-activejob-test-helper
fix: Ensure `ActiveJob::TestHelper` sets `scheduled_at` to a `Float`
2022-10-25 22:49:21 +09:00
Alex Ghiculescu
f737b5ec13 Fix double logging in ActiveRecord::QueryLog
Fixes https://github.com/rails/rails/issues/46103

An issue exists if you set `config.active_record.query_log_tags` to an array that includes `:controller`, `:action`, or `:job`; the relevant item will get duplicated in the log line. This occured because the relevant railties would add the item to `config.active_record.query_log_tags` again during setup. This PR fixes that by only adding those items to the config if they aren't already set.

The issue proposed more documentation to work around this, but I think it's a bug and should be fixed directly.
2022-10-22 15:53:14 -05:00
Ariel Valentin
658fd53741
fix: Ensure ActiveJob::TestHelper sets scheduled_at to a Float
I noticed a difference in behavior when inspecting `ActiveJob::Core#scheduled_at` in non-test environments vs when inspecting in while executing tests.

Prior to this change, `ActiveJob::TestHelper` would set `ActiveJob::Core#scheduled_at` to a `Time` object in tests,
however this attribute was set to a `Float` objects in non-test environments:

<d1aa6af5cc/activejob/lib/active_job/core.rb (L161)>

This change ensures parity in all runtime environments.
2022-10-17 19:22:24 +00:00
Rafael Mendonça França
13c786f5a7
Communicate minimum Sidekiq version in its adapter 2022-09-20 21:07:17 +00:00
Jonathan del Strother
b38992038f
Use a higher-level Sidekiq API for ActiveJob
This allows Sidekiq to use a custom client_class (rather than
Sidekiq::Client), which is necessary for the new transaction-aware
client in 6.5 (https://github.com/mperham/sidekiq/blob/main/Changes.md#650)
2022-09-20 13:14:24 +01:00
Yasuo Honda
cb22eb2b36 Remove QueAdapter from Active Job
Que and Rails (Active Job QueAdapter) have applied changes each other
to support Ruby 3 keyword arguments and options provided as top level keywords, etc
via these commits:

https://github.com/rails/rails/pull/44734
https://github.com/rails/rails/pull/44248
d9244b9074 via https://github.com/que-rb/que/pull/319

Finally, we've found it is quite difficult to maintain them.
Going forward Active Job Que adapter can be included in the future version of que gem itself.
Refer to https://github.com/rails/rails/issues/45899 the background for this change.

Active Job Que adapter can be included in the future version of que gem itself.
2022-09-12 22:31:10 +09:00
Jean Boussier
bd19d1baf1 Optimize AS::LogSubscriber
The various LogSubscriber subclasses tend to subscribe to events
but then end up doing nothing if the log level is high enough.

But even if we end up not logging, we have to go through the
entire notification path, record timing etc.

By allowing subscribers to dynamically bail out early, we can
save a lot of work if all subscribers are silenced.
2022-08-12 09:58:17 +02:00
Sam Bostock
b4fffc3c68
Teach ActiveJob to set configs on itself
Previously configs of the form `config.active_job.X` were only forwarded to
`ActiveJob::Base`. This teaches Active Job to set them on `ActiveJob` directly
instead, if the setter exists.

For consistency, this more or less mirrors the way that Active Record does it.

Co-authored-by: Adrianna Chang <adrianna.chang@shopify.com>
Co-authored-by: Sam Bostock <sam.bostock@shopify.com>

---

Fix use_big_decimal_serializer Rails 7.1 default

This config should be enabled for new Rails 7.1 apps, or apps that have updated
their config to `load_defaults 7.1`, not disabled.

This also clarifies the config accessor comment.

---

Add contributor documentation comment to load_defaults

The process for introducing a change in behavior in Rails can be confusing to
new contributors, so a comment is added roughly explaining how to do so, and
what belongs in `load_defaults` and `new_framework_defaults`.

This comment is aimed at contributors, not consumers, so it is added within the
method, rather than above it.
2022-07-30 11:11:15 -04:00
Sam Bostock
bc1f323338
Add ActiveJob::Serializers::BigDecimalSerializer
Previously, BigDecimal was listed as not needing a serializer.  However,
when used with an adapter storing the job arguments as JSON, it would get
serialized as a simple String, resulting in deserialization also producing
a String (instead of a BigDecimal).

By using a serializer, we ensure the round trip is safe.

During upgrade deployments of applications with multiple replicas making use of
BigDecimal job arguments with a queue adapter serializing to JSON, there exists
a possible race condition, whereby a "new" replica enqueues a job with an
argument serialized using `BigDecimalSerializer`, and an "old" replica fails to
deserialize it (as it does not have `BigDecimalSerializer`).

Therefore, to ensure safe upgrades, serialization will not use
`BigDecimalSerializer` until `config.active_job.use_big_decimal_serializer` is
enabled, which can be done safely after successful deployment of Rails 7.1.

This option will be removed in Rails 7.2, when it will become the default.
2022-07-19 15:31:41 -04:00
Ben Sheldon
8fceabd1e8 Defer constant loading of ActiveRecord::DestroyAssociationAsyncJob via a String instead of a class constant
Co-authored-by: Jonathan Hefner <jonathan@hefner.pro>
2022-07-13 11:58:20 -05:00
Jeremy Daer
8d9d8013ca Preserve job.enqueued_at timestamp precision
* Allows instrumenters to more accurately deduce queue wait time
* Retains IS08601 compatibility

References #39698
2022-07-12 18:13:16 -07:00
Hartley McGuire
a0030bf44f
Remove return_false_on_aborted_enqueue again
This was supposed to be removed in 7c788e91 but only the deprecation was
actually removed.
2022-07-03 16:23:04 -04:00
Jonathan Hefner
9574a39a7a Add db_runtime to Active Job instrumentation
This adds `:db_runtime` to `perform.active_job` notification payloads,
which is the total time taken by database queries while performing a
job.  This value can be used to better understand how a job's time is
spent.

This is similar to the `:db_runtime` value added to
`process_action.action_controller` notification payloads by
`ActiveRecord::Railties::ControllerRuntime`.

Closes #35354.

Co-authored-by: Cory Gwin <gwincr11@github.com>
2022-05-19 14:09:21 -05:00
Jean Boussier
dce43bb01d Remove unused Hash.ruby2_keywords_hash? backport 2022-05-03 11:38:36 +02:00
Petrik
ed96312797 Add the Active Job TestAdapter to QueueAdapter documentation [ci-skip]
Also removed the duplicate `Job` in the Async description.
2022-03-24 14:41:58 +01:00
Damir Zekić
6c93373842 Update ActiveJob que adapter for compatibility
que 1.2 introduced a deprecation warning when specifying job options
directly within the `#enqueue` method. From 1.2 job options need to be
provided as a hash in `job_options` keyword argument.

que 1.x is not compatible with Ruby 3 yet. que 2 will be.

This commit resolves the deprecation warning and ensures future
compatibility with que 2, allowing path to Ruby 3 upgrade preserving
compatibility with que 1.0.

Co-authored-by: Adis Hasović <adis@80pct.com>
2022-03-21 13:27:59 +01:00
Ghouse Mohamed
6ee6cb554b Fix #version docs and some typos 2022-03-16 01:48:37 +05:30
Jonathan Hefner
a199aaedb8 Cross-link API docs [ci-skip]
RDoc will automatically format and link API references as long as they
are not already marked up as inline code.

This commit removes markup from various API references so that those
references will link to the relevant API docs.
2022-02-21 11:45:25 -06:00
Jonathan Hefner
a801aa7cde Mark up inline code [ci-skip] 2022-02-21 11:11:11 -06:00
Jonathan Hefner
e37adfed4e Add Oxford commas [ci-skip] 2022-02-21 11:11:11 -06:00
Nick Holden
edf4ef9b26 Fix config.active_record.destroy_association_async_job configuration
`config.active_record.destroy_association_async_job` should allow applications
to specify the job that will be used to destroy associated records in the
background for `has_many` associations with the `dependent: :destroy_async`
option. That's being ignored, which means the default
`ActiveRecord::DestroyAssociationAsyncJob` always destroys records in the
background. This change stops ignoring the configuration.
2022-02-16 10:42:25 -07:00
Jean Boussier
4ca5aff9f0 Add missing bigdecimal require in ActiveJob::Arguments
Fix: https://github.com/rails/rails/issues/44399

Could cause `uninitialized constant ActiveJob::Arguments::BigDecimal (NameError)`
when loading Active Job in isolation.
2022-02-12 09:54:41 +01:00
Jonathan Rochkind
dcce260edc Add job_id and exception message to ActiveJob retry/discard logging
In other log messages like perform/performed and ActiveJob error logging, the job ID and exception message were already included, eg:

    Error performing TestFailureJob (Job ID: d70ad13e-e58b-409c-a8cc-e0447fc792b5) from Resque(default) in 1446.56ms: RuntimeError (Error Message):

But log message related to retry/discard behavior from ActiveJob::Exceptions did not include the Job ID or exception message. We now include them, in a consistent format with other existing messages.

Job ID is especially useful because, while other ActiveJob-related log messages get the Job ID via tagged logging, retry-related log messages do not currently end up tagged with Job ID. And it's really useful to use Job ID to be able to collect all the log lines related to a particular job, put the error messages together with the retry and subsequent perform messages.

We also include job.executions in the enqueue_retry log message because it was available and useful, to know if this is the 1st retry or 2nd or whatever. Previously job.executions was included in retry_stopped logging, but not enqueue_retry.
2022-02-08 10:06:21 -05:00
Rafael Mendonça França
6fb54c00e4
Merge PR #43066 2022-01-06 16:53:31 +00:00
Rafael Mendonça França
83d85b2207
Start Rails 7.1 development 2021-12-07 15:52:30 +00:00
Rafael Mendonça França
7c788e9175
Remove deprecated :return_false_on_aborted_enqueue option 2021-11-19 23:15:58 +00:00
Rafael Mendonça França
10bd5e59c3
Removed deprecated behavior that was not halting after_enqueue/after_perform callbacks when a previous callback was halted with throw :abort. 2021-11-17 21:51:17 +00:00
shunyama
4e2d55e1aa fix tiny grammatical mistakes[skip ci] 2021-11-13 00:58:14 +09:00
Jean Boussier
6bad959565 Extract ActiveSupport::ExecutionContext out of ActiveRecord::QueryLogs
I'm working on a standardized error reporting interface
(https://github.com/rails/rails/issues/43472) and it has the same need
for a `context` than Active Record's query logs.

Just like query logs, when reporting an error you want to know what the
current controller or job is, etc.

So by extracting it we can allow both API to use the same store.
2021-11-10 09:36:02 +01:00