Class: Arj::Test::Job
- Inherits:
-
ActiveJob::Base
- Object
- ActiveJob::Base
- Arj::Test::Job
- Includes:
- Persistence, Query
- Defined in:
- lib/arj/test/support.rb
Overview
A test job which does one of the following:
-
If the first argument is an
Exception
, raises it, using the second argument (if given) as the message. -
If the first argument is a
Proc
, invokes it and returns the result. -
Otherwise, returns the specified argument(s).
Example usage:
# Do nothing
job = Arj::Test::Job.perform_later('some arg')
job.perform_now # returns nil
# Return a value
job = Arj::Test::Job.perform_later('some arg')
job.perform_now # returns 'some arg'
# Return multiple values
job = Arj::Test::Job.perform_later('some arg', 'other arg')
job.perform_now # returns ['some arg', 'other arg']
# Raise an error
job = Arj::Test::Job.perform_later(StandardError)
job.perform_now # raises StandardError.new
# Raise an error with a message
job = Arj::Test::Job.perform_later(StandardError, 'oh, hi')
job.perform_now # raises StandardError.new('oh, hi')
# Cause a retry
job = Arj::Test::JobWithRetry.perform_later(Arj::Test::Error)
job.perform_now # re-enqueues and returns an Arj::Test::Error
# Override perform
job = Arj::Test::Job.perform_later(-> { 'some val' })
job.perform_now # returns 'some val'
job.on_perform { 'other val' }
job.perform_now # returns 'other val'
Direct Known Subclasses
Class Method Summary collapse
-
.perform_later(*args, **kwargs) ⇒ Arj::Test::Job, FalseClass
Overridden to add support for
Proc
arguments. -
.reset ⇒ NilClass
Clears #on_perform.
-
.total_executions ⇒ Integer
Returns the total number of executions across all jobs.
Instance Method Summary collapse
-
#global_executions ⇒ Integer
Returns the total number of executions for this job, even if this instance has not been reloaded from the database.
-
#on_perform(&block) ⇒ NilClass
Sets a
Proc
which will be invoked by #perform. -
#perform(*args, **kwargs) ⇒ Object
Does one of the following: - If the first argument is an
Exception
, raises it, using the second argument (if given) as the message.
Methods included from Persistence
#destroy!, #destroyed?, enqueue, #exists?, from_record, job_data, record_attributes, #reload, #save!, #update!
Methods included from Query
Class Method Details
.perform_later(*args, **kwargs) ⇒ Arj::Test::Job, FalseClass
Overridden to add support for Proc
arguments.
85 86 87 88 |
# File 'lib/arj/test/support.rb', line 85 def self.perform_later(*args, **kwargs, &) proc = args[0].is_a?(Proc) ? args.shift : nil super(*args, **kwargs, &).tap { |job| Job.on_perform[job.job_id] = proc if proc } end |
.reset ⇒ NilClass
Clears #on_perform.
100 101 102 103 104 |
# File 'lib/arj/test/support.rb', line 100 def self.reset Job.on_perform.clear Job.global_executions.clear nil end |
.total_executions ⇒ Integer
Returns the total number of executions across all jobs.
93 94 95 |
# File 'lib/arj/test/support.rb', line 93 def self.total_executions Job.global_executions.values.sum end |
Instance Method Details
#global_executions ⇒ Integer
Returns the total number of executions for this job, even if this instance has not been reloaded from the database. Useful, for instance when a job has been deleted and only a reference to the original job exists.
138 139 140 |
# File 'lib/arj/test/support.rb', line 138 def global_executions Job.global_executions[job_id] || 0 end |
#on_perform(&block) ⇒ NilClass
Sets a Proc
which will be invoked by #perform.
129 130 131 132 |
# File 'lib/arj/test/support.rb', line 129 def on_perform(&block) Job.on_perform[job_id] = block nil end |
#perform(*args, **kwargs) ⇒ Object
Does one of the following:
-
If the first argument is an
Exception
, raises it, using the second argument (if given) as the message. -
If the first argument is a
Proc
, invokes it and returns the result. -
Otherwise, returns the specified argument or arguments, or
nil
if none were specified.
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/arj/test/support.rb', line 110 def perform(*args, **kwargs) # rubocop:disable Lint/UnusedMethodArgument Job.global_executions[job_id] = (Job.global_executions[job_id] || 0) + 1 return Job.on_perform[job_id].call if Job.on_perform[job_id] raise(args[0], args[1] || 'error') if args[0].is_a?(Class) && args[0] < Exception case arguments.size when 0 nil when 1 arguments.sole else arguments end end |