Commit Graph

691 Commits

Author SHA1 Message Date
Eugene Kenny
0ebc720a04 Set retry_jitter to 0.0 for upgraded applications
The point of new framework defaults is that they're opt-in for upgraded
applications, but the default for this option applied the new behaviour.
2020-05-27 22:15:19 +01:00
Jonathan Hefner
b16c38ab6a Support procs for assert_{enqueued,performed}_with
PR #33995 added support for specifying the `args` argument of
`assert_enqueued_with` and `assert_performed_with` as a matcher proc.
In doing so, it added undocumented support for specifying the other
arguments as matcher procs as well.  This commit officially documents
that support, and adds tests to ensure the behavior.
2020-05-07 15:36:41 -05:00
Jonathan Hefner
7e08e19644 Fix assert_{enqueued,performed}_with doc examples [ci skip]
The example tests in the method docs for `assert_enqueued_with` and
`assert_performed_with` previously specified `queue` args in their
assertions but not in their setups.  Thus, the example tests would not
pass as written.  This commit fixes the examples, and properly
demonstrates the `queue` arg.
2020-05-07 14:13:10 -05:00
Rafael Mendonça França
684db54462
Add back the support to pass at as a proc in the job assertions 2020-05-01 12:22:57 -04:00
Santiago Bartesaghi
32bb46899d Remove unmaintained gem from README [ci skip] 2020-04-25 02:08:14 -03:00
Eugene Kenny
4c75e89a89
Merge pull request #38891 from jonathanhefner/fix-activejob-delay-test
Fix random CI fail due to cross-second time delay
2020-04-18 23:55:36 +01:00
Ryuta Kamizono
6f2126c760 Fixup CHANGELOGs [ci skip] 2020-04-15 21:23:24 +09:00
Étienne Barrié
f123e5e40a Fix perform_enqueued_jobs without a block with other helpers
assert_enqueued_with with a block ignores all the jobs enqueued before
the block for its assertions by counting the number of jobs and dropping
the n first elements from the Array, but since we're now mutating the
Array in perform_enqueued_jobs without a block, it's broken.

This uses another implementation which is correct when the array is
mutated, by getting a duplicated array of jobs, then removing them from
the original array.

Similarly assert_enqueued_jobs with a block was using counts only, now
keeps track of the specific jobs to count them at the end.
2020-04-13 13:13:38 -04:00
Rafael Mendonça França
855c9897eb
Move CHANGELOG entry to the top 2020-04-08 17:22:38 -04:00
Rafael Mendonça França
d16396e1ce
Merge pull request #38605 from dmitry/issue-38584
While using perform_enqueued_jobs enqueued jobs must be stored as well
2020-04-08 17:21:57 -04:00
Jonathan Hefner
fa2584fc2b Fix random CI fail due to cross-second time delay
Example failure: https://buildkite.com/rails/rails/builds/68010#749ce70d-1e01-4419-90e5-ee4531f66466/1057-1069

Support for testing jobs with a relative time delay was added in #36767.
It was implemented by truncating the `usec` portion of the `at` time in
order to allow for sub-second time differences.  However, sub-second
time differences can occur across seconds.  Thus, this commit changes
`at` time matching to use an explicit time range.
2020-04-07 13:53:50 -05:00
Jonathan Hefner
056b252010 Fix random CI fail due to auto-updating timestamp
Example failure: https://buildkite.com/rails/rails/builds/68074#0fe7ca54-fcce-4a47-85db-a784275c8f51/1115-1125

Each time a job is serialized, `enqueued_at` is updated.  Thus, separate
serializations of the same job can have different `enqueued_at`
timestamps if the serializations do not occur within the same second.
2020-04-06 23:31:29 -05:00
Abhay Nikam
bdfffd1355 Update the Rails mailing list URLs to new discuss discourse URL [ci skip] 2020-04-02 22:00:28 +05:30
Dino Maric
df26d123f6 Sanitize and add missing docs on ActiveJob::QueueAdapters class. [ci skip]
I guess `NO` is pretty self-explanatory here. But, to be consistent, this commit
describes what does **NO** mean in the context of: `retries, timeaout and priorities`.
2020-03-22 11:28:03 +01:00
Rafael Mendonça França
d9dc569d5b
Merge pull request #38749 from etiennebarrie/perform_enqueued_jobs_only_once
Perform enqueued jobs only once
2020-03-19 18:30:16 -04:00
dmitry
70fdc9e132 Move responsibility of the filtered? back to enqueue methods 2020-03-16 20:49:30 +01:00
Étienne Barrié
c6d621d132 Remove jobs from queue when performing in tests
This makes sure jobs don't run twice if `perform_enqueued_jobs` is
called twice without a block.

This also mimics the behavior of using `perform_enqueued_jobs` with a
block, where at the end of the block performed jobs are not in
`enqueued_jobs` but instead in `performed_jobs`.
2020-03-16 15:09:33 -04:00
Étienne Barrié
78aa157d79 Add assertions to show test was mistaken 2020-03-16 14:09:34 -04:00
utilum
06dcc3c927 Fix warning: method redefined 2020-03-13 18:19:14 +01:00
Rafael França
f0048109fc
Merge pull request #38676 from kwerle/correct_object_serializer_doc
Correct the parameter name for deserialize
2020-03-12 15:24:55 -04:00
Rafael França
d2d8b2892d
Merge pull request #38702 from aditya-vector/activejob-jitter-documentation-update
Updates to ActiveJob::Exceptions.retry_on with jitter documentation [skip ci]
2020-03-11 13:43:19 -04:00
Aditya Narsapurkar
895c39fe96 Updates to ActiveJob::Exceptions.retry_on with jitter documentation [skip ci]
- Changelog updated
- Method documentation updated
2020-03-11 21:49:55 +05:30
Edouard CHIN
17e304def8 AJ perform_enqueued_jobs shouldn't perform job retries:
- ### Problem

  If we use `perform_enqueued_jobs` without a block, a job that
  uses a retry mechanism to reeenqueue itself would get performed
  right away.
  This behaviour make sense when using `perform_enqueued_jobs` with
  a block.

  However I'm expecting `perform_enqueued_jobs` without a block to
  perform jobs that are **already** in the queue not the ones that
  will get enqueued afterwards.

  ### Solution

  Dup the array of jobs given to avoid future mutation.
2020-03-09 19:46:11 -04:00
Edouard CHIN
13cb5b78a8 Fix AJ TestAdapter#performed_jobs not properly counting job:
- ### Problem

  If we use `perform_enqueued_jobs` without a block,
  a job that raises an error wouldn't be appended to
  the list of `performed_jobs`.

  ### Solution

  Push the job in the array before it is actually performed.
2020-03-09 19:46:11 -04:00
Kurt Werle
6852a53821 Correct the parameter name for deserialize
deserialize expects a JSON primitive
2020-03-07 11:48:48 -08:00
Aditya Narsapurkar
3aacd855cd parent 6d0895a4894724e1a923a514daad8fb3c9ac2c28
author Aditya Narsapurkar <adityanarsapurkar@yahoo.com> 1582316102 +0530
committer Aditya Narsapurkar <adityanarsapurkar@yahoo.com> 1583159505 +0530

parent 6d0895a4894724e1a923a514daad8fb3c9ac2c28
author Aditya Narsapurkar <adityanarsapurkar@yahoo.com> 1582316102 +0530
committer Aditya Narsapurkar <adityanarsapurkar@yahoo.com> 1583159327 +0530

Randomize jitter
- This PR attempts to fix a problem with ActiveJob jitter where the `determine_jitter_for_delay` value may not always be randomized. Especially when the jitter delay multplier is between 1 and 2 it always returns 0.
- With this change, we pass a range to `Kernel.rand` beginning with 0 to the `jitter_multiplier`. With positive float values, the return value will be a random float number from the range.
- Includes test cases to verify random wait time when the jitter_multiplier is between 1 and 2.
- Updated relevant test cases stubbing the `Kernel.rand` method, refactored some by removing unwanted stubs for `Kernel.rand` method where jitter is falsey.

Fixed rubocop issue - used assert_not_equal instead of refute_equal in test case

Fixed rubocop issue - used assert_not_equal instead of refute_equal in test case

Fixed rubocop issue - used assert_not_equal instead of refute_equal in test case

Review updates - separated test cases for random wait time with default and exponentially retrying jobs
- Another test case added to make sure negative wait time does not affect the randomization

Review updates
- Instead of using Kernel.rand with range, used simple multiplication with Kernel.rand for calculating delay for jitter
- Updates to the tests according to changes
2020-03-05 07:45:33 -08:00
Wojciech Wnętrzak
d12a31b193
Add support for que name to Que adapter.
Co-authored-by: Brad Nauta <bradnauta@gmail.com>
2020-03-04 13:50:13 +01:00
Rafael França
40b7d93c5b
Merge pull request #38576 from Edouard-chin/ec-activejob-deprecation
Fix AJ wrong deprecation message on `after_callbacks_if_terminated`:
2020-03-03 15:28:50 -05:00
Edouard CHIN
0cdeee428e Fix AJ wrong deprecation message on after_callbacks_if_terminated:
- ### Problem

  In some cirumstances, the deprecation message to warn that AJ won't
  run `after_(enqueue/perform)` callbacks when the chain is halted
  by a `throw(:abort)` will be thrown even though no `throw(:abort)`
  was thrown.

  ```ruby
    run_callback(:foo) do
      ...
    end
  ```

  There is two possible way for the callback body to not be executed:

  1) `before` callback throw a `abort`
  2) `before` callback raises an error which is rescued by an
     around callback (See associated test in this commit for
     an example)

  When 2) happen we don't want to output a deprecation message,
  because what the message says isn't true and doesn't apply.

  ### Solution

  In order to differentiate between 1) and 2), I have added
  a `halted_callback_hook` which is called by ActiveSupport callback
  whenever the callback chain is halted.
2020-03-03 16:17:55 -04:00
Edouard CHIN
06dd162fb3 ActiveSupport::Calbacks#halted_callback_hook receive callback name:
- The `halted_callback_hook` method is called whenever the
  `terminator` halt the callback execution.
  Usually, this translate to when a `before` callback throw
  an `:abort`.

  <details>
    <summary> Example </summary>

    ```ruby
      class Foo
        include ActiveSupport::Callbacks

	define_callbacks :save
	set_callback(:save, :before) { throw(:abort) }

	def run
	  run_callbacks(:save) do
	    'hello'
	  end
	end

	def halted_callback_hook(filter)
	  # filter is the proc passed to `set_callback` above
	end
      end
    ```
  </details>

  ### Problem

  When a class has multiple callbacks, (i.e. `save`, `validate` ...),
  it's impossible to tell in the halted_callback_hook which type of
  callback halted the execution.
  This is useful to take different action based on the callback.

  <details>
    <summary> Use Case </summary>

    ```ruby
      class Foo
        include ActiveSupport::Callbacks

	define_callbacks :save
	define_callbacks :validate

	set_callback(:save, :before) { throw(:abort) }
	set_callback(:validate, :before) { throw(:abort) }

	def run
	  run_callbacks(:validate) do
	    ...
	  end

	  run_callbacks(:save) do
	    ...
	  end
	end

	def halted_callback_hook(filter)
	  Rails.logger.warn("Couldn't save the record, the ??? callback halted the execution")
	end
      end
    ```
  </details>

  ### Solution

  Allow `halted_callback_hook` to receive a second argument which is
  the name of the callback being run.
2020-03-03 16:17:55 -04:00
dmitry
7e6e6eb56f While using perform_enqueued_jobs enqueued jobs must be stored as well #38584 2020-02-28 13:32:06 +01:00
Ryuta Kamizono
1d3eb7be13 Fixup CHANGELOGs [ci skip] 2020-02-25 14:14:54 +09:00
Edouard CHIN
d35cf4c05d Fix ActiveJob Test adapter not respecting retry attempts:
- ### Problem

  Given the below example the test adapter will retry the job
  indefinitely:

  ```ruby
    class BuggyJob < ActiveJob::Base
      retry_on(Exception,  attempts: 2)

      def perform
        raise "error"
      end
    end

    BuggyJob.perform_later
    perform_enqueued_jobs
  ```

  The problem is that when the job get retried, the
  `exception_executions` variable is not serialized/deserialized,
  resulting in ActiveJob to not be able to determine how many time
  this job was retried.

  The solution in this PR is to deserialize the whole job in the test
  adapter, and reserialize it before retrying.

  Fix #38391
2020-02-10 17:59:20 -04:00
Ryuta Kamizono
8e452c710d Fix keyword arguments warnings in Action Mailer 2020-01-21 09:23:15 +09:00
Ryuta Kamizono
fc67857596 Fix keyword arguments warnings in Active Job
Related #38053, #38187, #38105, #38260.

This is a reattempt to fix keyword arguments warnings in Active Job.

Now Ruby (master) has `Hash.ruby2_keywords_hash{?,}` and that will be
backported to 2.7.1.

https://github.com/ruby/ruby/pull/2818
https://bugs.ruby-lang.org/issues/16486

I've emulated that for 2.7.0 and older versions.
2020-01-21 05:53:23 +09:00
Ryuta Kamizono
99f18b618b Revert "Merge pull request #38260 from kamipo/fix_kwargs_warning_for_activejob"
This reverts commit 80e72c5eb7d4f79cc8eaa63237e5a60dff48f4f4, reversing
changes made to 0dad1e3e770273631e5ab454d2d3003d90060e17.
2020-01-21 05:26:44 +09:00
Ryuta Kamizono
950b39482d Fix keyword arguments warnings in Active Job
Related #38053, #38187, #38105.

This is a reattempt to fix keyword arguments warnings in Active Job.

Now Ruby (master) has `Hash.ruby2_keywords_hash{?,}` and that will be
backported to 2.7.1.

https://github.com/ruby/ruby/pull/2818
https://bugs.ruby-lang.org/issues/16486

I've emulated that for 2.7.0 and older versions.
2020-01-19 12:09:54 +09:00
Douglas Lara
10803d6985 fix typos 2020-01-15 00:11:40 -03:00
Rafael Mendonça França
a901372e61
Revert "Merge pull request #38053 from Shopify/actionmailer-6-0-stable-ruby-2.7-warnings"
This reverts commit 59e4c996b3e9af4004eca6486ffd178b42fe2bc9.

Closes #38187.
2020-01-08 12:01:02 -03:00
Edouard CHIN
ca61139fae Fix wrong logging message in AJ in case a job returns a falsey value:
- I made a change in 0d3aec49695 to output a log if a job was aborted
  in a before callbacks. I didn't take in consideration that a job
  could return a falsy value and thus it would wrongly log
  that the job was aborted.

  This fixes the problem by checking if the callback chain was halted
  rather than the return value of the job.
2020-01-03 17:14:56 +01:00
Edouard CHIN
2cd05dda2b Fix regression where AJ#perform_now doesn't return the job's value:
- Fix #38040
2020-01-03 17:14:56 +01:00
Abhay Nikam
d8beb77252 Bump license years from 2019 to 2020 [ci skip] 2020-01-01 15:10:31 +05:30
Edouard CHIN
be60970be7 Set AJ return_false_on_aborted_enqueue true in the test suite:
- Since this is going to be the default in 6.1, let's set it in the
  test suite to avoid deprecation warning.
  Otherwise one has to do `AS::Deprecation.silence { }` everytime we
  add a new test.

  Fix #38107
2019-12-28 16:56:54 +01:00
Ryuta Kamizono
59e4c996b3 Merge pull request #38053 from Shopify/actionmailer-6-0-stable-ruby-2.7-warnings
Fix Ruby 2.7 warnings in Action Mailer 6.0
2019-12-20 22:40:31 +09:00
Rafael França
3c7c2d220e
Merge pull request #37994 from bibstha/remove_activejob_logging_backtrace
Remove stacktrace from ActiveJob logging callback halts
2019-12-19 12:27:22 -03:00
Cliff Pruitt
db0bc5e6d5 Disable ActiveJob retry jitter when given zero/falsey value (#38003)
* Add failing ActiveJob exceptions test for "disable retry jitter"

Thanks to @kaspth for the starting point.

* Update ActiveJob retry jitter to correctly use zero value

* Simplify "disable retry jitter" test

We don't need to repeat this many times. Fewer is shorter.

* Refactor determine_delay with jitter

* Fix indentation

* Close the curtains and give JITTER_DEFAULT some privacy

* Use .zero? instead of == to check jitter value

* Add ActiveJob test for explicit zero jitter

Co-authored-by: Kasper Timm Hansen <hey@kaspth.com>
Co-authored-by: Cliff Pruitt <cliff.pruitt@cliffpruitt.com>
2019-12-17 15:23:52 -03:00
Bibek Shrestha
210bf6aa39 Remove stacktrace from ActiveJob logging callback halts
ActiveJob will log the entire backtrace when one of the enqueue
callbacks fail. This is not necessary and increases noise. When there is
an Exception object, the object gets bubbled up eventually anyways.

Remove `Array(ex.backtrace).join("\n")` from `def enqueue` and `def
enqueue_at`.
2019-12-16 18:46:04 -05:00
Kasper Timm Hansen
a8cbb96b3d
make that a private instance method instead 2019-12-15 03:46:02 +01:00
Kasper Timm Hansen
d258bfe8ac
Consolidate and give context to after callback deprecation
The existing message only mentioned one type of before/after callback,
but the config was named generally. That mismatch is confusing and users
wouldn't necessarily know what the total effect of the config would be.

So instead of handwriting the deprecation warning in the specific instances,
consolidate it in one place and give the appropriate context. That context
is the above, but also that users shouldn't update their app config,
they should uncomment the line in the new defaults file, which now also
has more context.

I'm not totally convinced that we can't move this to when
`after_enqueue`/`after_perform` is called in the job class. Doesn't
seem worth it to blare this after every job enqueue/perform, when we
the score at boot time.

cc @Edouard-chin
2019-12-15 03:20:07 +01:00
Edouard CHIN
9eb4b4ed01 Fix deprecation being thrown at boot time:
-
  ### Problem

  In rails/rails@bbfab0b33a I introduced a change which outputs
  a deprecation whenever a class inherits from ActiveJob::Base.

  This has the negative effect to trigger a massive amount of
  deprecation at boot time especially if your app is eagerloaded
  (like it's usually the case on CI).

  Another issue with this approach was that the deprecation will
  be output no matter if a job define a `after_perform` callbacks
  i.e.
  ```ruby
    class MyJob < AJ::Base
      before_enqueue { throw(:abort) }
    end

    # This shouldn't trigger a deprecation since no after callbacks are defined
    # The change in 6.2 will be already safe for the app.
  ```

  ### Solution

  Trigger the deprecation only when a job is abort
  (during enqueuing or performing) AND a `after_perform`
  callback is defined on the job.
2019-12-13 03:25:03 +01:00