diff options
Diffstat (limited to 'activejob/test/jobs')
-rw-r--r-- | activejob/test/jobs/application_job.rb | 4 | ||||
-rw-r--r-- | activejob/test/jobs/callback_job.rb | 4 | ||||
-rw-r--r-- | activejob/test/jobs/gid_job.rb | 5 | ||||
-rw-r--r-- | activejob/test/jobs/hello_job.rb | 4 | ||||
-rw-r--r-- | activejob/test/jobs/inherited_job.rb | 7 | ||||
-rw-r--r-- | activejob/test/jobs/kwargs_job.rb | 4 | ||||
-rw-r--r-- | activejob/test/jobs/logging_job.rb | 3 | ||||
-rw-r--r-- | activejob/test/jobs/multiple_kwargs_job.rb | 9 | ||||
-rw-r--r-- | activejob/test/jobs/nested_job.rb | 3 | ||||
-rw-r--r-- | activejob/test/jobs/overridden_logging_job.rb | 11 | ||||
-rw-r--r-- | activejob/test/jobs/provider_jid_job.rb | 9 | ||||
-rw-r--r-- | activejob/test/jobs/queue_adapter_job.rb | 5 | ||||
-rw-r--r-- | activejob/test/jobs/queue_as_job.rb | 12 | ||||
-rw-r--r-- | activejob/test/jobs/rescue_job.rb | 14 | ||||
-rw-r--r-- | activejob/test/jobs/retry_job.rb | 41 | ||||
-rw-r--r-- | activejob/test/jobs/timezone_dependent_job.rb | 22 | ||||
-rw-r--r-- | activejob/test/jobs/translated_hello_job.rb | 12 |
17 files changed, 155 insertions, 14 deletions
diff --git a/activejob/test/jobs/application_job.rb b/activejob/test/jobs/application_job.rb new file mode 100644 index 0000000000..d92ffddcb5 --- /dev/null +++ b/activejob/test/jobs/application_job.rb @@ -0,0 +1,4 @@ +# frozen_string_literal: true + +class ApplicationJob < ActiveJob::Base +end diff --git a/activejob/test/jobs/callback_job.rb b/activejob/test/jobs/callback_job.rb index 891ed9464e..436cb55492 100644 --- a/activejob/test/jobs/callback_job.rb +++ b/activejob/test/jobs/callback_job.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + class CallbackJob < ActiveJob::Base before_perform ->(job) { job.history << "CallbackJob ran before_perform" } after_perform ->(job) { job.history << "CallbackJob ran after_perform" } @@ -17,7 +19,6 @@ class CallbackJob < ActiveJob::Base job.history << "CallbackJob ran around_enqueue_stop" end - def perform(person = "david") # NOTHING! end @@ -25,5 +26,4 @@ class CallbackJob < ActiveJob::Base def history @history ||= [] end - end diff --git a/activejob/test/jobs/gid_job.rb b/activejob/test/jobs/gid_job.rb index e485bfa2dd..2136f57e05 100644 --- a/activejob/test/jobs/gid_job.rb +++ b/activejob/test/jobs/gid_job.rb @@ -1,8 +1,9 @@ -require_relative '../support/job_buffer' +# frozen_string_literal: true + +require_relative "../support/job_buffer" class GidJob < ActiveJob::Base def perform(person) JobBuffer.add("Person with ID: #{person.id}") end end - diff --git a/activejob/test/jobs/hello_job.rb b/activejob/test/jobs/hello_job.rb index 022fa58e4a..404df6150a 100644 --- a/activejob/test/jobs/hello_job.rb +++ b/activejob/test/jobs/hello_job.rb @@ -1,4 +1,6 @@ -require_relative '../support/job_buffer' +# frozen_string_literal: true + +require_relative "../support/job_buffer" class HelloJob < ActiveJob::Base def perform(greeter = "David") diff --git a/activejob/test/jobs/inherited_job.rb b/activejob/test/jobs/inherited_job.rb new file mode 100644 index 0000000000..14f852ed06 --- /dev/null +++ b/activejob/test/jobs/inherited_job.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +require_relative "application_job" + +class InheritedJob < ApplicationJob + self.queue_adapter = :inline +end diff --git a/activejob/test/jobs/kwargs_job.rb b/activejob/test/jobs/kwargs_job.rb index 2df17d15ae..b86b06bada 100644 --- a/activejob/test/jobs/kwargs_job.rb +++ b/activejob/test/jobs/kwargs_job.rb @@ -1,4 +1,6 @@ -require_relative '../support/job_buffer' +# frozen_string_literal: true + +require_relative "../support/job_buffer" class KwargsJob < ActiveJob::Base def perform(argument: 1) diff --git a/activejob/test/jobs/logging_job.rb b/activejob/test/jobs/logging_job.rb index d84ed8589b..4605fa6937 100644 --- a/activejob/test/jobs/logging_job.rb +++ b/activejob/test/jobs/logging_job.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + class LoggingJob < ActiveJob::Base def perform(dummy) logger.info "Dummy, here is it: #{dummy}" @@ -7,4 +9,3 @@ class LoggingJob < ActiveJob::Base "LOGGING-JOB-ID" end end - diff --git a/activejob/test/jobs/multiple_kwargs_job.rb b/activejob/test/jobs/multiple_kwargs_job.rb new file mode 100644 index 0000000000..b355c4ce1a --- /dev/null +++ b/activejob/test/jobs/multiple_kwargs_job.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +require_relative "../support/job_buffer" + +class MultipleKwargsJob < ActiveJob::Base + def perform(argument1:, argument2:) + JobBuffer.add("Job with argument1: #{argument1}, argument2: #{argument2}") + end +end diff --git a/activejob/test/jobs/nested_job.rb b/activejob/test/jobs/nested_job.rb index 8c4ec549a6..aafad0dba9 100644 --- a/activejob/test/jobs/nested_job.rb +++ b/activejob/test/jobs/nested_job.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + class NestedJob < ActiveJob::Base def perform LoggingJob.perform_later "NestedJob" @@ -7,4 +9,3 @@ class NestedJob < ActiveJob::Base "NESTED-JOB-ID" end end - diff --git a/activejob/test/jobs/overridden_logging_job.rb b/activejob/test/jobs/overridden_logging_job.rb new file mode 100644 index 0000000000..2ee363637d --- /dev/null +++ b/activejob/test/jobs/overridden_logging_job.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +class OverriddenLoggingJob < ActiveJob::Base + def perform(dummy) + logger.info "Dummy, here is it: #{dummy}" + end + + def logger + @logger ||= ActiveSupport::TaggedLogging.new(ActiveSupport::Logger.new(nil)) + end +end diff --git a/activejob/test/jobs/provider_jid_job.rb b/activejob/test/jobs/provider_jid_job.rb new file mode 100644 index 0000000000..dacd09afdc --- /dev/null +++ b/activejob/test/jobs/provider_jid_job.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +require_relative "../support/job_buffer" + +class ProviderJidJob < ActiveJob::Base + def perform + JobBuffer.add("Provider Job ID: #{provider_job_id}") + end +end diff --git a/activejob/test/jobs/queue_adapter_job.rb b/activejob/test/jobs/queue_adapter_job.rb new file mode 100644 index 0000000000..1c31a60ba2 --- /dev/null +++ b/activejob/test/jobs/queue_adapter_job.rb @@ -0,0 +1,5 @@ +# frozen_string_literal: true + +class QueueAdapterJob < ActiveJob::Base + self.queue_adapter = :inline +end diff --git a/activejob/test/jobs/queue_as_job.rb b/activejob/test/jobs/queue_as_job.rb new file mode 100644 index 0000000000..0feee99e87 --- /dev/null +++ b/activejob/test/jobs/queue_as_job.rb @@ -0,0 +1,12 @@ +# frozen_string_literal: true + +require_relative "../support/job_buffer" + +class QueueAsJob < ActiveJob::Base + MY_QUEUE = :low_priority + queue_as MY_QUEUE + + def perform(greeter = "David") + JobBuffer.add("#{greeter} says hello") + end +end diff --git a/activejob/test/jobs/rescue_job.rb b/activejob/test/jobs/rescue_job.rb index f1b9c9349e..d142cec1ea 100644 --- a/activejob/test/jobs/rescue_job.rb +++ b/activejob/test/jobs/rescue_job.rb @@ -1,17 +1,19 @@ -require_relative '../support/job_buffer' +# frozen_string_literal: true + +require_relative "../support/job_buffer" class RescueJob < ActiveJob::Base class OtherError < StandardError; end rescue_from(ArgumentError) do - JobBuffer.add('rescued from ArgumentError') + JobBuffer.add("rescued from ArgumentError") arguments[0] = "DIFFERENT!" retry_job end rescue_from(ActiveJob::DeserializationError) do |e| - JobBuffer.add('rescued from DeserializationError') - JobBuffer.add("DeserializationError original exception was #{e.original_exception.class.name}") + JobBuffer.add("rescued from DeserializationError") + JobBuffer.add("DeserializationError original exception was #{e.cause.class.name}") end def perform(person = "david") @@ -19,9 +21,9 @@ class RescueJob < ActiveJob::Base when "david" raise ArgumentError, "Hair too good" when "other" - raise OtherError + raise OtherError, "Bad hair" else - JobBuffer.add('performed beautifully') + JobBuffer.add("performed beautifully") end end end diff --git a/activejob/test/jobs/retry_job.rb b/activejob/test/jobs/retry_job.rb new file mode 100644 index 0000000000..68dc17e16c --- /dev/null +++ b/activejob/test/jobs/retry_job.rb @@ -0,0 +1,41 @@ +# frozen_string_literal: true + +require_relative "../support/job_buffer" +require "active_support/core_ext/integer/inflections" + +class DefaultsError < StandardError; end +class FirstRetryableErrorOfTwo < StandardError; end +class SecondRetryableErrorOfTwo < StandardError; end +class LongWaitError < StandardError; end +class ShortWaitTenAttemptsError < StandardError; end +class ExponentialWaitTenAttemptsError < StandardError; end +class CustomWaitTenAttemptsError < StandardError; end +class CustomCatchError < StandardError; end +class DiscardableError < StandardError; end +class FirstDiscardableErrorOfTwo < StandardError; end +class SecondDiscardableErrorOfTwo < StandardError; end +class CustomDiscardableError < StandardError; end + +class RetryJob < ActiveJob::Base + retry_on DefaultsError + retry_on FirstRetryableErrorOfTwo, SecondRetryableErrorOfTwo + retry_on LongWaitError, wait: 1.hour, attempts: 10 + retry_on ShortWaitTenAttemptsError, wait: 1.second, attempts: 10 + retry_on ExponentialWaitTenAttemptsError, wait: :exponentially_longer, attempts: 10 + retry_on CustomWaitTenAttemptsError, wait: ->(executions) { executions * 2 }, attempts: 10 + retry_on(CustomCatchError) { |job, error| JobBuffer.add("Dealt with a job that failed to retry in a custom way after #{job.arguments.second} attempts. Message: #{error.message}") } + retry_on(ActiveJob::DeserializationError) { |job, error| JobBuffer.add("Raised #{error.class} for the #{job.executions} time") } + + discard_on DiscardableError + discard_on FirstDiscardableErrorOfTwo, SecondDiscardableErrorOfTwo + discard_on(CustomDiscardableError) { |job, error| JobBuffer.add("Dealt with a job that was discarded in a custom way. Message: #{error.message}") } + + def perform(raising, attempts) + if executions < attempts + JobBuffer.add("Raised #{raising} for the #{executions.ordinalize} time") + raise raising.constantize + else + JobBuffer.add("Successfully completed job") + end + end +end diff --git a/activejob/test/jobs/timezone_dependent_job.rb b/activejob/test/jobs/timezone_dependent_job.rb new file mode 100644 index 0000000000..41f473d533 --- /dev/null +++ b/activejob/test/jobs/timezone_dependent_job.rb @@ -0,0 +1,22 @@ +# frozen_string_literal: true + +require_relative "../support/job_buffer" + +class TimezoneDependentJob < ActiveJob::Base + def perform(now) + now = now.in_time_zone + new_year = localtime(2018, 1, 1) + + if now >= new_year + JobBuffer.add("Happy New Year!") + else + JobBuffer.add("Just #{(new_year - now).div(3600)} hours to go") + end + end + + private + + def localtime(*args) + Time.zone ? Time.zone.local(*args) : Time.utc(*args) + end +end diff --git a/activejob/test/jobs/translated_hello_job.rb b/activejob/test/jobs/translated_hello_job.rb new file mode 100644 index 0000000000..a0a68b4040 --- /dev/null +++ b/activejob/test/jobs/translated_hello_job.rb @@ -0,0 +1,12 @@ +# frozen_string_literal: true + +require_relative "../support/job_buffer" + +class TranslatedHelloJob < ActiveJob::Base + def perform(greeter = "David") + translations = { en: "Hello", de: "Guten Tag" } + hello = translations[I18n.locale] + + JobBuffer.add("#{greeter} says #{hello}") + end +end |