Commit Graph

390 Commits

Author SHA1 Message Date
Rafael França
299a213a73
Merge pull request #33992 from kirs/enqueue-return-false
Make AJ::Base#enqueue return false if the job wasn't enqueued
2018-12-05 11:24:19 -05:00
Rafael Mendonça França
72300f9742 Do not deserialize GlobalID objects that were not generated by Active Job
Trusting any GlobaID object when deserializing jobs can allow
attackers to access information that should not be accessible to them.

Fix CVE-2018-16476.
2018-11-27 15:28:41 -05:00
Rafael França
f0330b6202
Merge pull request #34376 from composerinteralia/default_queue_name
Allow using queue prefix with a default queue name
2018-11-23 17:08:40 -05:00
Alberto Almagro
95d9c3b3d6 Keep executions for each specific exception (#34352)
* Keep executions for each specific declaration

Fixes #34337

ActiveJob used the global executions counter to control the number of
times a job should be retried. The problem with this approach was that
in case a job raised different exceptions during its executions they
weren't retried the number of times defined by their `attemps` number.

**Example:**

Having the following job:
```ruby
class BuggyJob < ActiveJob::Base
  retry_on CustomException, attemps: 3
  retry_on OtherException, attempts: 3
end
```
If the job raised `CustomException` in the first two executions and then
it raised `OtherException`, the job wasn't retried anymore because the
global executions counter was already indicating 3 attempts.

With this patch each `retry_on` declaration has its specific counter so
that the first two executions that raise `CustomException` don't affect
the retries count that future exceptions may have.

* Revert "clarifies documentation around the attempts arugment to retry_on"

This reverts commit 86aa8f8c5631f77ed9a208e5107003c01512133e.
2018-11-23 11:31:14 -08:00
Edouard CHIN
cdb16ac576 Allow all ActiveJob assertion helper to accept Proc in their only kw:
- That feature is useful to enqueue or assert that jobs got enqueued
  or performed based on dynamic conditions.
  We will be able to leverage that feature to fix all ActionMailer
  assertion helper issue when a Mailer define a custom delivery job
  (see next commit).
2018-11-21 23:14:43 +01:00
Bernie Chiu
945fdd7692 Permit ActionController::Parameters for serializable Hash 2018-11-19 17:38:29 -05:00
jacobherrington
308d637550 Change queueing to queuing in docs and comments [skip ci]
My spellchecker flagged this as an incorrect spelling, upon further
research it appears to be a point of contention in English. Either way
might work.

After further examination queuing is much more common in the Rails
codebase so making this change will serve to standardize the spelling.
2018-11-18 22:36:44 -06:00
Alberto Almagro
36ea629fae Make PERMITTED_TYPES private
The constant `PERMITTED_TYPES` is only used by the private method
`serialize_argument` and it already has the `# :nodoc:` annotation as
the other constants in the class.

Complements e899e22
2018-11-12 20:44:59 +01:00
Alberto Almagro
7c9e69dba1 Document missing supported types [ci skip]
This commit adds missing types to the supported types list, which
was extended in #30941
2018-11-11 23:02:54 +01:00
Ryuta Kamizono
e899e228e4 Restore private_constant which is lost accidentally in #30941
https://github.com/rails/rails/pull/30941/files#diff-fc90ec41ef75be8b2259526fe1a8b663L53
2018-11-09 20:05:29 +09:00
Daniel Colson
b517347be1
Allow using queue prefix with a default queue name
Fixes #34366

Currently setting queue_name_prefix will not combine a prefix with the
default_queue_name; it will only affect queue names set with `queue_as`.
With this PR the prefix will affect the default_queue_name as well.

Closes #19831

Currently setting default_queue_name doesn't actually affect the
queue_name default (although default_queue_name does get used if you
pass a falsey `part_name` to `queue_as`). This PR would get
default_queue_name working as expected as well.

Because the queue_name default is now a lambda wrapping the
default_queue_name, rather than the default_queue_name itself, I had to
update one test to use the instance method `#queue_name` (which
`instance_exec`s the value) instead of the class method. I think this
change is OK, since only the instance method is documented.

There was a question about whether we want a `default_queue_name`
configuration. If we want to get rid of it, I would also be happy to
open a PR for that instead. It has been around for a while now, but it
also hasn't really worked for a while now.

r? @matthewd
since you had an opinion about this before
2018-11-03 22:39:18 -04:00
Gannon McGibbon
1f7bd2a171 Restore HWIA support to AJ::Arguments.deserialize
Restore HashWithIndifferentAccess support to
ActiveJob::Arguments.deserialize.
2018-10-30 17:10:33 -05:00
Rafael França
f4876545ff
Merge pull request #33972 from bogdanvlviv/follow-up-33897
Improve `enqueue_retry.active_job` message
2018-10-30 16:53:06 -04:00
Kir Shatrov
ee9fc12024 Make AJ::Base#enqueue return false if the job wasn't enqueued 2018-10-28 15:02:44 -04:00
Rafael França
d76d66fb55
Merge pull request #34204 from XrXr/aj-test-helper-args
Include deserialized arguments in jobs returned by AJ test helpers
2018-10-24 15:49:10 -04:00
bogdanvlviv
0925d7340e
Clarify docs of ActiveJob::TestHelper [ci skip]
I found a few sentences that should be updated as well.
See https://github.com/rails/rails/pull/33571#discussion_r209435886

Follow up #33571
2018-10-16 00:26:18 +03:00
Alan Wu
3ebc229034 Include deserialized arguments in jobs returned by AJ test helpers
`assert_enqueued_with` and `assert_performed_with` return a instantiated
instance of the matching job for further assertion (#21010).

Before this commit the `arguments` method on the returned instance
returns a serialized version of the arguments.
2018-10-12 12:15:16 -04:00
Orhan Toy
6865f73b91 [ci skip] Fix link to Concurrent::ThreadPoolExecutor docs 2018-10-11 13:29:34 +02:00
Alan Wu
1042950b34 Remove unnecessary use of included in ActiveJob::Core
Using `included` to define `attr_acessor` and `attr_writer` is causing
these methods to not show up in the documentation.
2018-10-05 14:51:41 -07:00
Yasuo Honda
aa3dcabd87 Add Style/RedundantFreeze to remove redudant .freeze
Since Rails 6.0 will support Ruby 2.4.1 or higher
`# frozen_string_literal: true` magic comment is enough to make string object frozen.
This magic comment is enabled by `Style/FrozenStringLiteralComment` cop.

* Exclude these files not to auto correct false positive `Regexp#freeze`
 - 'actionpack/lib/action_dispatch/journey/router/utils.rb'
 - 'activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb'

It has been fixed by https://github.com/rubocop-hq/rubocop/pull/6333
Once the newer version of RuboCop released and available at Code Climate these exclude entries should be removed.

* Replace `String#freeze` with `String#-@` manually if explicit frozen string objects are required

 - 'actionpack/test/controller/test_case_test.rb'
 - 'activemodel/test/cases/type/string_test.rb'
 - 'activesupport/lib/active_support/core_ext/string/strip.rb'
 - 'activesupport/test/core_ext/string_ext_test.rb'
 - 'railties/test/generators/actions_test.rb'
2018-09-29 07:18:44 +00:00
Mohit Natoo
9fa4342970 Documentation clarity in ActiveJob::TestHelper [ci skip] (#33571)
* Documentation clarity in ActiveJob::TestHelper [ci skip]

* Documentation for options

[Mohit Natoo + Rafael Mendonça França]
2018-09-28 12:10:26 -04:00
Ryuta Kamizono
886d38e63e
Merge pull request #33570 from mohitnatoo/queue_adapter_name_doc
Documentation for queue_adapter_name method in ActiveJob::QueueAdapter

[ci skip]
2018-09-28 18:35:48 +09:00
Edouard CHIN
4d75f58991 Add a way to check for subset of arguments when performing jobs:
- When calling `assert_performed_with`/`assert_enqueued_with`, the
  +args+ needs to match exactly what the job get passed.

  Some jobs can have lot of arguments, or even a simple hash argument
  has many key. This is not convenient to test as most tests doesn't
  need to check if the arguments matches perfectly.

  This PR make it possible to only check if a subset of arguments were
  passed to the job.
2018-09-26 18:45:52 -04:00
bogdanvlviv
b0f2f5e348
Improve enqueue_retry.active_job message
Since #33751 was added `enqueue_retry.active_job` instrumentation to
the `retry_on` method, then #33897 moved the instrumentation to `retry_job`
method in order to ensure that this method publish `enqueue_retry.active_job`
notification too.
See related discussion https://github.com/rails/rails/pull/33751#discussion_r214140008

Since `enqueue_retry.active_job` moved to `retry_job`, there is no guarantee
that payload of `enqueue_retry.active_job` would have `:error`.
See test `LoggingTest#test_enqueue_retry_logging_on_retry_job` as
example of that case.

Related to https://github.com/rails/rails/pull/33897#discussion_r219707024

I think we can improve notification of `enqueue_retry.active_job`:

- If there is no `event.payload[:error]`, then publish like
  "Retrying RescueJob in 3 seconds." only.
- If `event.payload[:wait]` is `nil`, then publish
  "Retrying RescueJob in 0 seconds." instead of "Retrying RescueJob in nil seconds."
- If there is `event.payload[:error]`, then publish
  "Retrying RescueJob in 3 seconds, due to a DefaultsError.".
- Change the type of the message from `error` to `info.`

Also, this commit removes part of messages - "The original exception was #{ex.cause.inspect}."
of `enqueue_retry.active_job`, `retry_stopped.active_job`, and `discard.active_job` since I
haven't found it useful. Please let me know whether you agree with that?
2018-09-25 01:34:20 +03:00
Kasper Timm Hansen
baffadaec9
Extract instrument method.
Similar to Action View's and Action Controller's instrument helpers.
2018-09-23 20:44:42 +02:00
yuuji.yaginuma
b18f2fe96d Fix "warning: shadowing outer local variable - job" 2018-09-22 09:14:08 +09:00
Sharang Dashputre
14e278f876 Make assert_<enqueued|performed>_with() compare hashes ignoring order of keys
The test helpers now treat `{ a: 1, b: 2 }` and `{ b: 2, a: 1 }` as equals
2018-09-21 00:09:40 +05:30
bogdanvlviv
b6b5a7ac52
retry_job should publish enqueue_retry.active_job notification
Also this commit removes `:wait` from payload of
`retry_stopped.active_job`.

Related to https://github.com/rails/rails/pull/33751#discussion_r214140008
Follow up #33751

/cc @kaspth, @rafaelfranca
2018-09-16 17:46:24 +03:00
Sharang Dashputre
ca875d0c02 Update arguments.rb [ci skip]
Fix typo `Instrinsic` -> `Intrinsic`
2018-09-13 22:14:27 +05:30
Ricardo Díaz
64a9759aff Include test helpers when ActionDispatch::IntegrationTest is loaded
As @dhh brings up, the point of `ActionDispatch::IntegrationTest` is to
allow users to test the integration of all the pieces called by a
controller. Asserting about the emails and jobs queued is part of that
task.

This commit includes the `ActionMailer::TestHelper` and
`ActiveJob::TestHelper` modules when the ActionMailer and ActiveJob
railties are initialized respectively.
2018-09-12 16:58:14 -05:00
Rafael França
66ab9c74dd
Merge pull request #33823 from stoodfarback/aj_backburner_fix_priority
ActiveJob Backburner adapter: fix priority
2018-09-10 17:50:37 -04:00
Graham Turner
86aa8f8c56 clarifies documentation around the attempts arugment to retry_on 2018-09-09 09:46:20 -04:00
stoodfarback
fc4e7d44c8 ActiveJob Backburner adapter: fix priority
The priority wasn't being passed from ActiveJob to Backburner, despite
priority being supported. This also brings it inline with the docs,
which mark Backburner as supporting priorities in the "Backend Features"
table: https://api.rubyonrails.org/classes/ActiveJob/QueueAdapters.html
2018-09-07 21:55:05 -07:00
Rafael França
5bbaae0d20
Merge pull request #33751 from steves/add_retry_notifications_to_aj
Add hooks to ActiveJob around retries and discards
2018-08-30 13:39:51 -05:00
Steve S
3bf33d7c2c
Move ActiveJob retry and discard logging into log subscriber 2018-08-30 13:07:37 -04:00
Steve S
26dc9bc8ee
Add hooks to ActiveJob around retries and discards 2018-08-29 13:28:55 -04:00
Kevin Deisz
7c9751d7fe
Permit list usage cleanup and clearer documentation 2018-08-27 09:51:46 -04:00
Kevin Deisz
0efecd913c
Convert remaining usage of whitelist and blacklist 2018-08-24 16:16:41 -04:00
bogdanvlviv
b8576425ee
DRY in assert_enqueued_jobs 2018-08-20 13:05:29 +03:00
bogdanvlviv
b7beb5d4e5
Fix formatting of ActiveJob::TestHelper api docs 2018-08-20 13:05:29 +03:00
bogdanvlviv
2ec60fb818
Allow assert_performed_with to be called without a block.
Example:
```
def test_assert_performed_with
  MyJob.perform_later(1,2,3)

  perform_enqueued_jobs

  assert_performed_with(job: MyJob, args: [1,2,3], queue: 'high')
end
```

Follow up #33626.
2018-08-20 13:05:29 +03:00
bogdanvlviv
11634e8ef8
Fix assert_performed_jobs and assert_no_performed_jobs
Execution of `assert_performed_jobs`, and `assert_no_performed_jobs`
without a block should respect passed `:except`, `:only`, and `:queue` options.
2018-08-20 13:05:29 +03:00
bogdanvlviv
de4420da44
Allow :queue option to assert_no_performed_jobs.
If the `:queue` option is specified, then only the job(s) enqueued to a specific
queue will not be performed.

Example:
```
def test_assert_no_performed_jobs_with_queue_option
  assert_no_performed_jobs queue: :some_queue do
    HelloJob.set(queue: :other_queue).perform_later("jeremy")
  end
end
```
2018-08-20 13:05:29 +03:00
bogdanvlviv
d50fb21e4d
Allow :queue option to assert_performed_jobs.
If the `:queue` option is specified, then only the job(s) enqueued to a specific
queue will be performed.

Example:
```
def test_assert_performed_jobs_with_queue_option
  assert_performed_jobs 1, queue: :some_queue do
    HelloJob.set(queue: :some_queue).perform_later("jeremy")
    HelloJob.set(queue: :other_queue).perform_later("bogdan")
  end
end
```
2018-08-20 13:05:29 +03:00
bogdanvlviv
e0cf042fa2
Fix perform_enqueued_jobs
Set
````
queue_adapter.perform_enqueued_jobs = true
queue_adapter.perform_enqueued_at_jobs = true
queue_adapter.filter = only
queue_adapter.reject = except
queue_adapter.queue = queue
```
if block given.
Execution of `flush_enqueued_jobs` doesn't require that.
2018-08-20 13:05:22 +03:00
bogdanvlviv
ec2e8f645e
Allow :queue option to perform_enqueued_jobs.
If the `:queue` option is specified, then only the job(s) enqueued to
a specific queue will be performed.

Example:
```
def test_perform_enqueued_jobs_with_queue
  perform_enqueued_jobs queue: :some_queue do
    MyJob.set(queue: :some_queue).perform_later(1, 2, 3) # will be performed
    HelloJob.set(queue: :other_queue).perform_later(1, 2, 3) # will not be performed
  end
  assert_performed_jobs 1
end
```

Follow up #33265

[bogdanvlviv & Jeremy Daer]
2018-08-20 00:20:06 +03:00
yuuji.yaginuma
e49fb7921e Increment execution count before deserialize arguments
Currently, the execution count increments after deserializes arguments.
Therefore, if an error occurs with deserialize, it retries indefinitely.

In order to prevent this, the count is moved before deserialize.
Fixes #33344.
2018-08-18 11:22:41 +09:00
bogdanvlviv
f169752163
Add example perform_enqueued_jobs without block to api docs [ci skip]
Follow up #33626
2018-08-16 09:29:30 +03:00
Kevin Deisz
ec16301488
Allow perform_enqueued_jobs to be called without a block.
Performs all of the jobs that have been enqueued up to this point in the test.
2018-08-15 15:00:40 -04:00
Mohit Natoo
2d57f2f2f2 Documentation for queue_adapter_name method in ActiveJob::QueueAdapter [ci skip] 2018-08-10 02:02:58 +05:30