Commit Graph

822 Commits

Author SHA1 Message Date
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
545482650b Update redis and resque
Fix: https://github.com/rails/rails/issues/45913
2022-09-07 15:10:43 +02:00
Ryuta Kamizono
e0e64e9474 Fix typo in activejob/CHANGELOG.md [ci-skip] 2022-08-29 15:34:01 +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
Gannon McGibbon
e1635ff4a7 Show default parent classes for generators in help texts
Print default parent class for controller, job, and model generators.

Before:

[--parent=PARENT]               # The parent class for the generated job

After:

[--parent=PARENT]               # The parent class for the generated job
                                # Default: ApplicationJob
2022-07-26 18:13:02 -05: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
Sam Bostock
eb4b540368
Clear JobBuffer on teardown
ActiveJob tests should be able to rely on a clean JobBuffer in each test.
2022-07-18 23:33:07 -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
Gannon McGibbon
02444d9267 Add --parent option to job generator to specify parent class of job.
Example:

`bin/rails g job process_payment --parent=payment_job` generates:

```ruby
class ProcessPaymentJob < PaymentJob
  # ...
end
```
2022-07-05 20:55:38 -04: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
Gannon McGibbon
5f57c7d352 Add more detailed description to job generator 2022-06-13 14:44:28 -05:00
Juanito Fatas
7a069dc6f8 Work around Sidekiq 6.4 and 6.5 API difference 2022-06-10 00:47:09 +09:00
Juanito Fatas
6e6e2b9457 Prepare for upcoming Sidekiq Configuration design
Reference commit: 67daa7a408

While the docs[1] suggests to use Sidekiq::Config, but it is not
available yet. So uses Sidekiq instead to make activejob integration
tests work.

[1]https://github.com/mperham/sidekiq/blob/main/docs/global_to_local.md#explicit-configuration
2022-06-10 00:16:35 +09:00
Gannon McGibbon
3dad2919d5 Rename behaviour to behavior in documentation 2022-05-26 17:14:18 -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
ksss
fff5567803
Remove autoload to unused constant 2022-04-06 15:31:15 +09: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ć
c7b4a8b4ba Update CHANGELOG 2022-03-21 13:43:11 +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
Andrew White
fca1aeed9c
Allow overriding of Que and QC urls in integration tests
The Active Job integration tests for Que and Queue Classic assume a
connection to a PostgreSQL database running on localhost:5432 since the
psql command to create the databases does not pass the -h and -p options.
2022-03-19 10:19:23 +00:00
Ghouse Mohamed
6ee6cb554b Fix #version docs and some typos 2022-03-16 01:48:37 +05:30
Jean Boussier
3f6c1d02c1 Eager load ActiveJob::ConfiguredJob and Serializers
Otherwise they are autoloaded when the first job is enqueued
which isn't good for production performance.
2022-03-01 12:16:52 +01:00
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
Matthew Draper
e28c4e5d59 Add missing requires 2022-02-17 11:13:51 +10:30
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
Hartley McGuire
f83f5c81af fix Que integration tests for Active Job
Replace Que.* settings that were removed in Que 1.0 with options passed
to Que::Locker. The Locker class creates its own thread for managing
work distribution, so the Thread created in the QueJobsManager was also
removed.
2022-01-27 16:24:16 -05:00
Hartley McGuire
b6b2001f51 add assertion helpers for Active Job integration
Using `assert` by itself without an error message leads to test output
like the following:

```
Failure:
QueuingTest#test_current_timezone_is_kept_while_running_perform_later [.../rails/activejob/test/integration/queuing_test.rb:118]:
Expected false to be truthy.
```

These added helpers provide a more specific error message so that the
reasons for test failures become more clear:

```
Failure:
QueuingTest#test_current_timezone_is_kept_while_running_perform_later [.../rails/activejob/test/integration/queuing_test.rb:118]:
Job AJ-63fe8bf6-7062-4a56-a10a-97c37fcaa6e6 was not executed
```
2022-01-26 19:41:45 -05:00
Hartley McGuire
4eefa1feb0 fix que integration in Active Job tests part 2
Fixing the synchronous setting uncovered some more errors with Que 1.0:
exception tests with retry_on started failing due to an assertion in
Que's Active Job integration that running jobs won't nest. However, this
is not the case when running retriable jobs synchronously.

This change overrides Que's Active Job wrapper to not make this
assertion.

I also opened an issue in the que repo: https://github.com/que-rb/que/issues/329
2022-01-24 21:56:38 -05:00
Hartley McGuire
ee5d18bb92 fix que integration in Active Job tests
Que.mode was removed in 6783fdd68a
and replaced by Que.run_synchronously = true per changelog:
fa4988ee33/CHANGELOG.md (L120)

It looks like it started failing because Que 1.0 was released yesterday.
2022-01-24 16:15:22 -05:00
Rafael Mendonça França
6fb54c00e4
Merge PR #43066 2022-01-06 16:53:31 +00:00
Ryuta Kamizono
65766ebcc8 Bump license years to 2022 [ci-skip] 2022-01-01 15:22:15 +09: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
Rafael Mendonça França
1fde031e89 Fix gemspec 2021-11-15 21:06:21 +00:00
Rafael Mendonça França
9195b7fd0a
Require MFA to release rails 2021-11-15 20:37:42 +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
Dorian Marié
13b3c46365 fix typo of using where instead of were 2021-11-07 14:17:00 +01:00
HParker
29e502f374 Improve active_job test_helper error messages
Error messages from assert_enqueued_with and assert_performed_with
shows which other jobs are enqueued or performed when you get an
error.  We can improve these messages to make it clearer why the job
didn't match. This one error messages now have three different formats
based on why the job didn't match

1. Didn't match because no jobs are queued at all. This now reports,
```
No performed job found with {:job=>HelloJob, :args=>[]}

No jobs where performed
```

2. Didn't match because that job class was not queued. This now reports,
```
No performed job found with {:job=>MultipleKwargsJob, :args=>[#<Person:0x00007fe38f9f8100 @id=11>]}

No jobs of class MultipleKwargsJob where performed, job classes performed: HelloJob
```

3. Doesn't match due to arguments, queue, priority or other reason. This now reports
```
No performed job found with {:job=>HelloJob, :args=>[#<Person:0x00007fe3a89fc2c8 @id=11>]}

Potential matches: {"job_class"=>"HelloJob", "job_id"=>"f8636fd9-c7a0-4565-9198-17e175f30f0e", "provider_job_id"=>nil, "queue_name"=>"default", "priority"=>nil, "arguments"=>[{"_aj_globalid"=>"gid://aj/Person/9"}], "executions"=>0, "exception_executions"=>{}, "locale"=>"en", "timezone"=>nil, "enqueued_at"=>"2021-10-27T23:19:54Z", :job=>HelloJob, :args=>[#<Person:0x00007fe3a89dce00 @id="9">], :queue=>"default", :priority=>nil}
```

Which matches the old error message, but only reports jobs that where
queued of the same class as the job you are asserting was queued.
2021-10-27 16:31:50 -07:00
Jean Boussier
51c45d334c Refactor ActiveRecord::QueryLogs context API
This remove the null object as discussed in
8af78700d2.
Since it pretends to be nil but acts as thruthy in boolean context
it cause more confusion than it clean code.

`update_context` is removed in favor of `set_context` without
a block.
2021-10-26 09:55:33 +02:00