Commit Graph

646 Commits

Author SHA1 Message Date
Jean Boussier
e922c59207 Implement Active Job enqueue_after_transaction_commit
A fairly common mistake with Rails is to enqueue a job from inside a
transaction, with a record as argumemnt, which then lead to a RecordNotFound
error when picked up by the queue.

This is even one of the arguments advanced for job runners backed by the
database such as `solid_queue`, `delayed_job` or `good_job`.

But relying on this is undesirable in my opinion as it makes the Active Job
abstraction leaky, and if in the future you need to migrate to another backend
or even just move the queue to a separate database, you may experience a lot of
race conditions of the sort.

To resolve this problem globally, we can make Active Job optionally transaction
aware, and automatically defer job queueing to `after_commit`.

Co-Authored-By: Cristian Bica <cristian.bica@gmail.com>
2024-04-03 16:32:16 +02:00
Rafael Mendonça França
154f7c2181
Register autoload for ActiveJob::Arguments
Since #50715, there are cases where `ActiveJob::Arguments` is used
but not required anymore in the test environment. This could be a
test for a custom argument serializer.

One solution could be to add a require "active_job/arguments" in
all the usages, but that isn't a convention we follow in Rails.

Instead, let's register the autoload for `ActiveJob::Arguments` in
`active_job.rb` so that it's always available when needed.
2024-02-13 20:34:10 +00:00
Nick Dower
22bc976576 Tiny update to callbacks docs [ci skip]
The following was added to the `ActiveJob::Callbacks`,
`ActiveModel::Callbacks` and `AbstractController:Callbacks` docs
in #29072:

> NOTE: Calling the same callback multiple times will overwrite
> previous callback definitions.

The comment refers to "calling" callbacks but seems to be about defining
callbacks, as mentioned in the PR description.

In the ActiveJob and AbstractController docs, I believe this will be
misinterpreted as referring to setting callbacks, which, as far as I can
tell, does not have such a restriction.

In the ActiveModel docs, I believe it would be slightly clearer to
replace "calling" with "defining".
2024-02-02 12:11:30 +01:00
Lorenzo Zabot
349ce8dc2d docs: correct fixed-width font documentation 2024-01-25 20:20:10 +01:00
Petrik
db6eb7b72c Specify singleton method name for accessors defined on singleton_class [ci-skip]
RDoc can't extract the name of accessors defined with
`singleton_class.attr_accessor`, resulting in methods called `unknown`
in RDoc.
2024-01-22 12:34:07 +01:00
Petrik
8565f45100 Use relative includes of README's in documentation [ci-skip]
The Rails documentation uses the `:include:` directive to inline the
README of the framework into the main documentation page. As the
README's aren't in the root directory from where SDoc is run we need to
add the framework path to the include:

    # :include: activesupport/README.md

This results in a warning when installing the gems as generating the rdoc for the gem is run from the gem/framework root:

    Couldn't find file to include 'activesupport/README.rdoc' from lib/active_support.rb

The `:include:` RDoc directive supports includes relative to the current
file as well:

    # :include: ../README.md

This makes sure it works for the Rails API docs and the separate gems.

Co-authored-by: Jonathan Hefner <jonathan@hefner.pro>
2024-01-18 10:39:15 +01:00
Sander Verdonschot
427acd8d6a
Improve after_enqueue example
The current example suggests that after_enqueue is only called if the
enqueue is successful, which is not always true.
2024-01-12 17:14:50 -05:00
maximerety
f72d2118fe
[Fix #50713] Do not trigger loading of ActiveJob::Base in ActiveJob::TestHelper 2024-01-11 19:52:47 +01:00
Jean Boussier
27140247c2 Cleanup defined? usage
Now that we dropped support for Ruby 2.7, we no longer
need to check if variables are defined before accessing them
to avoid the undefined variable warning.
2024-01-05 15:05:35 +01:00
Will Jessop
7606002f13 Move singleton require.
ActiveSupport::Deprecation used to `include Singleton` which is why the require is here, but it was removed in 9812641891f1c9ba4c3f8ffb8549ec26fc2668c4 (June 5 2023). Leaving the require seems to have been an oversight.

However, module ActiveJob::Serializers::ObjectSerializer does require Singleton and was relying on AS:Deprecation to require it. This PR just moves the require to the place it's actually used.
2023-12-30 12:02:01 +00:00
John Hawthorn
1a4474e7ee
Revert "Do not overwrite AJ logger if it is supplied" 2023-12-04 17:32:50 -08:00
Joshua Young
18098ccc4c [Fix #50230] Preserve serialized timezone when deserializing with ActiveJob::Serializers::TimeWithZoneSerializer 2023-12-03 00:37:09 +10:00
Aerdayne
e03cc187f5 Do not overwrite AJ logger if it is supplied
Use conditional assignment within 'active_job.logger' initializer
2023-11-25 09:36:45 +01:00
Rafael Mendonça França
03dae98092
Remove deprecated :exponentially_longer value for the :wait in retry_on 2023-11-22 22:13:59 +00:00
Rafael Mendonça França
798289699d
Remove deprecated support to set numeric values to scheduled_at attribute 2023-11-22 22:13:06 +00:00
Rafael Mendonça França
2a761d23d2
Remove deprecated primitive serializer for BigDecimal arguments 2023-11-22 22:13:03 +00:00
Katherine Oelsner
e57e462a62 ActiveJob: fallback serializing String subclasses
https://github.com/rails/rails/pull/50090 broke serialization of String
subclasses that don't have serializers, like ActiveSupport::SafeBuffer.

Co-authored-by: John Hawthorn <john@hawthorn.email>
2023-11-20 23:41:19 +00:00
fatkodima
14578eaa4f Fix ActiveJob arguments serialization to correctly serialize String subclasses having custom serializers
Co-authored-by: Jean Boussier <jean.boussier@gmail.com>
2023-11-18 01:55:24 +02:00
William Johnston
584a9d409c
Fix ActiveJob method name in sucker_punch 1.0 error 2023-10-23 16:13:11 -05:00
Akhil G Krishnan
6eb93332c4 Typo fix in BigDecimal job arguments warning 2023-10-19 21:22:31 +05:30
Dustin Brown
be9d0f05a4 Don't include enqueuing info when job wasn't enqueued 2023-10-11 10:57:36 -07:00
Jonathan Hefner
4230d0d2be Fix perform_enqueued_jobs :at option doc [ci-skip]
The `:at` option will perform jobs that have been enqueued to run at the
exact specified time, as well as before the specified time.
2023-10-07 12:26:26 -05:00
Jonathan Hefner
705d515ac5 Reword adapter caveat for perform_enqueued_jobs [ci-skip] 2023-10-07 12:26:26 -05:00
Jonathan Hefner
3f2987403e Reword queue_adapter_for_test doc [ci-skip] 2023-10-07 12:26:26 -05:00
Jonathan Hefner
ed08eea99f Capitalize framework names [ci-skip] 2023-10-07 12:26:26 -05:00
Petrik de Heus
6c9967f85a
Merge pull request #49413 from ghiculescu/document-retry-on
Document hierarchy for `retry_on` and `discard_on`
2023-09-28 07:34:06 +02:00
Petrik de Heus
a7041d3138
Merge pull request #49394 from johanstenberg92/fix-method-documentation-for-attempts-retry-on [ci skip]
Adjust method documentation for `ActiveJob::Exceptions::ClassMethods#retry_on`
2023-09-28 07:28:35 +02:00
Alex
bb8d6c9349 Document hierarchy for retry_on and discard_on
Fixes https://github.com/rails/rails/issues/49406
2023-09-28 15:00:29 +10:00
Rafael Mendonça França
32eba97a08
Make sure scheduled_at is a Time object when asserting enqueued jobs
When the `:at` attribute is sent to the test adapter it is converted
to a float, and that float is what is added to the payload, so we
need to convert back to a Time object.

See:
8a37276416/activejob/lib/active_job/enqueuing.rb (L95)
8a37276416/activejob/lib/active_job/queue_adapters/test_adapter.rb (L34)
2023-09-27 15:29:11 +00:00
fatkodima
7ef86b6a49 Enable Lint/RedundantSafeNavigation rubocop cop 2023-09-27 14:55:07 +03:00
Johan Stenberg
9ea1e7aa78
Update activejob/lib/active_job/exceptions.rb
Co-authored-by: Alex Ghiculescu <alex@tanda.co>
2023-09-27 08:37:12 +02:00
Rafael Mendonça França
fb6c6007d0
Development of Rails 7.2 starts now
🎉
2023-09-27 03:59:11 +00:00
Rafael Mendonça França
e5386cb402
Preparing for 7.1.0.rc1 release 2023-09-27 03:08:31 +00:00
Ben Sheldon [he/him]
c919b474a7
Set, serialize, and deserialize Active Job scheduled_at as Time; deserialize enqueued_at as Time; deprecate setting scheduled_at= with numeric/epoch
Co-authored-by: Adam Pahlevi <adam.pahlevi@gmail.com>
2023-09-26 17:01:41 -07:00
Johan Stenberg
09889cd4f0 Adjust method documentation for ActiveJob::Exceptions::ClassMethods#retry_on. The argument does take the initial job execution attempt into account, this is not reflected in the method documentation. 2023-09-26 20:07:18 +02:00
Victor Mours
1bdabc1102 Clarify that the default retry strategy uses polynomial backoff and not exponential backoff 2023-09-16 00:51:14 +02:00
Rafael Mendonça França
699dfdb426
Preparing for 7.1.0.beta1 release 2023-09-13 00:36:01 +00:00
Ben Sheldon
b1b7debaa3
Fix Active Job log message to correctly report a job failed to enqueue when the adapter raises an ActiveJob::EnqueueError 2023-09-02 07:52:43 -07:00
Adrianna Chang
27c5c7cd99
Merge pull request #48920 from fidalgo/fidalgo/update-assert_enqueued_with-documentation
[docs] Update assert_enqueued_with documentation with keyword arguments example
2023-08-29 10:36:22 -04:00
Rafael Mendonça França
53a3a95d01
Merge PR #48010 2023-08-21 17:39:03 +00:00
Hartley McGuire
ff6e885d59
Remove uneeded requires of core_ext/string/filters
`actionpack/lib/action_dispatch/routing.rb`
- added: 013745151be062aa4d0fc1f2a008a7303fdb6e04
- removed: 93034ad7fea7e00562103a7cd0acfab19bbfadf9

`activejob/lib/active_job/log_subscriber.rb`
- added: b314ab555e0d85e6efb41be94fb5f3a157bb12fe
- removed: 5ab2034730feacfc2caee418f8c0b55191d27427

`activemodel/lib/active_model/errors.rb`
- added: cf7fac7e29bb2816412c949fdaed3d61a923eb23
- removed: 9de6457ab0767ebab7f2c8bc583420fda072e2bd

`activerecord/lib/active_record/core.rb`
- added: b3bfa361c503e107aff4dee5edf79bd7fd3d3725
- removed: e1066f450d1a99c9a0b4d786b202e2ca82a4c3b3

`activesupport/lib/active_support/core_ext/module/introspection.rb`
- added: 358ac36edf1695fcbec0aa21f126a3d8b83d4b5a
- removed: 167b4153cac0069a21e0bb9689cb16f34f6abbaa

`activesupport/lib/active_support/duration.rb`
- added: 75924c4517c8f87712d3f59c11f10152ed57b9d8
- removed: a91ea1d51048342d13fc73f9b09ce4cfd086bb34

`railties/lib/rails/commands/server/server_command.rb`
- added: f2173648938b418d120f5a68d8f3862d8ae9dace
- removed: 553b86fc751c751db504bcbe2d033eb2bb5b6a0b

`railties/lib/rails/command/base.rb`
- added: 6813edc7d926965e5644cd8befaf229a35b9d8ca
- removed: b617a561d865a65cfc140caa0e3c4af4350bfcef
2023-08-16 17:39:25 -04:00
Jonathan del Strother
074d431493
Fix invalid durations in ActiveJob serialization
Durations that were round-tripped through ActiveJob::Arguments.serialize
would appear fine at a first glance, but trying to perform
duration-math on them would fail:

```
irb(main):001:0> d = ActiveJob::Arguments.deserialize(ActiveJob::Arguments.serialize([1.year]))[0]
=> 1 year
irb(main):002:0> d + 1.day
activesupport-6.1.4.4/lib/active_support/duration.rb:242:in `+': undefined method `merge' for [[:years, 1]]:Array (NoMethodError)
```
2023-08-13 11:27:16 +01:00
Paulo Fidalgo
9dbac2cedc
Update assert_enqueued_with documentation with keyword arguments example
This commit adds an example to the `assert_enqueued_with` method documentation,
demonstrating how to use it with keyword arguments.
The example clarifies that keyword arguments should be specified as a hash inside an array,
providing guidance for developers writing tests for jobs that use keyword arguments.
2023-08-11 09:54:59 +02:00
Alex Ghiculescu
639dab59dc Active Job: async adapter should always run jobs immediately if immediate set
This is an internal fix, not user facing. I noticed it while working on https://github.com/rails/rails/pull/48585.

The `async` adapter has an `immediate` option, which should only be used in tests. This option should tell the adapter to run jobs inline. This works correctly with `perform_later`, but it does not work with `enqueue_at`, which is what other internal mechanisms such as `retry_job` use.

This PR fixes this bug.
2023-07-03 09:02:51 +10:00
Alex Ghiculescu
ba70a5be86 Make perform_enqueued_jobs compatible with all Active Job adapters 2023-06-28 20:08:08 +10:00
Alex Baldwin
719e49485f Fix error ActiveJob passed class with permitted?
Resolves #48561
Co-authored-by: Sampat Badhe <sampatbadhe@gmail.com>
2023-06-23 10:51:46 -04:00
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