Fix example code in ActiveJob::Core [ci skip]

1) It seems that it raise error on example code in `ActiveJob::Core`.

Before:

```ruby
class DeliverWebhookJob < ActiveJob::Base
  def serialize
    super.merge('attempt_number' => (@attempt_number || 0) + 1)
  end

  def deserialize(job_data)
    super
    @attempt_number = job_data['attempt_number']
  end

  rescue_from(Timeout::Error) do |exception|
    raise exception if @attempt_number > 5
    retry_job(wait: 10)
  end

  def perform
    raise Timeout::Error
  end
end
```

Then it run `DeliverWebhookJob.perform_now` in `rails console`. And raise error:

NoMethodError: undefined method `>' for nil:NilClass
from /app/jobs/deliver_webhook_job.rb:12:in `block in <class:DeliverWebhookJob>'

So I thought it's necessary to fix it.

After:

```ruby
class DeliverWebhookJob < ActiveJob::Base
  attr_writer :attempt_number

  def attempt_number
    @attempt_number ||= 0
  end

  def serialize
    super.merge('attempt_number' => attempt_number + 1)
  end

  def deserialize(job_data)
    super
    self.attempt_number = job_data['attempt_number']
  end

  rescue_from(Timeout::Error) do |exception|
    raise exception if attempt_number > 5
    retry_job(wait: 10)
  end

  def perform
    raise Timeout::Error
  end
end
```

Then it run `DeliverWebhookJob.perform_now` in `rails console`. And it does'nt raise error NoMethodError.

2) Use `Timeout::Error` instead of `TimeoutError` (`TimeoutError` is deprecated).
This commit is contained in:
Yoshiyuki Hirano 2017-12-05 15:25:12 +09:00
parent c383c4142a
commit 649f19cab1
1 changed files with 10 additions and 4 deletions

View File

@ -97,17 +97,23 @@ module ActiveJob
# ==== Examples
#
# class DeliverWebhookJob < ActiveJob::Base
# attr_writer :attempt_number
#
# def attempt_number
# @attempt_number ||= 0
# end
#
# def serialize
# super.merge('attempt_number' => (@attempt_number || 0) + 1)
# super.merge('attempt_number' => attempt_number + 1)
# end
#
# def deserialize(job_data)
# super
# @attempt_number = job_data['attempt_number']
# self.attempt_number = job_data['attempt_number']
# end
#
# rescue_from(TimeoutError) do |exception|
# raise exception if @attempt_number > 5
# rescue_from(Timeout::Error) do |exception|
# raise exception if attempt_number > 5
# retry_job(wait: 10)
# end
# end