Module: Zizq::Job
- Defined in:
- lib/zizq/job.rb
Overview
Mixin which all valid job classes must include.
This module must be included in a class to make it a valid Zizq job. The class name becomes the job type, and the worker resolves types back to classes via ‘Object.const_get` (which naturally triggers any autoload logic).
class SendEmailJob
include Zizq::Job
zizq_queue "emails" # optional, defaults to "default"
def perform(user_id, template:)
puts "Sending #{template} email to user #{user_id}"
end
end
The job can be configured through class methods to set the queue, priority etc. Classes can also override ‘::zizq_enqueue_options` to implement dynamically configured jobs based on their arguments.
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
-
.call(job) ⇒ Object
Default dispatcher for Zizq jobs.
-
.included(base) ⇒ Object
: (Class) -> void.
Instance Method Summary collapse
-
#perform(*args, **kwargs) ⇒ Object
This is your job’s main entrypoint when it is run by the worker.
-
#set_zizq_job(job) ⇒ Object
private
Set by the worker before calling #perform.
-
#zizq_attempts ⇒ Object
How many times this job has previously been attempted (0 on the first run, 1 on the second, etc…).
-
#zizq_dequeued_at ⇒ Object
Time at which this job was dequeued (fractional seconds since the Unix epoch).
-
#zizq_id ⇒ Object
The unique job ID assigned by the server.
-
#zizq_priority ⇒ Object
The priority this job was enqueued with.
-
#zizq_queue ⇒ Object
The queue this job was dequeued from.
Class Method Details
.call(job) ⇒ Object
Default dispatcher for Zizq jobs.
Resolves the job class from the type string, deserializes the payload, and calls ‘#perform`. Any object that responds to `#call(job)` can replace this as a custom dispatcher via `Zizq.configure { |c| c.dispatcher = MyDispatcher.new }`.
The contract is simple: return normally → ack, raise → nack.
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/zizq/job.rb', line 46 def self.call(job) job_class = Object.const_get(job.type) unless job_class.is_a?(Class) && job_class.include?(Zizq::Job) raise "#{job.type} does not include Zizq::Job" end zizq_job_class = job_class #: Zizq::job_class instance = zizq_job_class.new instance.set_zizq_job(job) args, kwargs = zizq_job_class.zizq_deserialize( job.payload || { "args" => [], "kwargs" => {} } ) instance.perform(*args, **kwargs) end |
.included(base) ⇒ Object
: (Class) -> void
31 32 33 |
# File 'lib/zizq/job.rb', line 31 def self.included(base) #: (Class) -> void base.extend(ClassMethods) end |
Instance Method Details
#perform(*args, **kwargs) ⇒ Object
This is your job’s main entrypoint when it is run by the worker.
Override this method in your job class to define the work to perform. Declare any positional and keyword arguments your job needs.
Strong recommendation: stick to keyword arguments because they are much easier to evolve over time in a backwards compatible way with any already enqueued jobs.
152 153 154 |
# File 'lib/zizq/job.rb', line 152 def perform(*args, **kwargs) #: (*untyped, **untyped) -> void raise NotImplementedError, "#{self.class.name}#perform must be implemented" end |
#set_zizq_job(job) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Set by the worker before calling #perform. Receives the full Resources::Job object so all metadata is available through delegation.
184 185 186 |
# File 'lib/zizq/job.rb', line 184 def set_zizq_job(job) #: (Resources::Job) -> void @zizq_job = job end |
#zizq_attempts ⇒ Object
How many times this job has previously been attempted (0 on the first run, 1 on the second, etc…).
167 |
# File 'lib/zizq/job.rb', line 167 def zizq_attempts = @zizq_job&.attempts #: () -> Integer? |
#zizq_dequeued_at ⇒ Object
Time at which this job was dequeued (fractional seconds since the Unix epoch). This can be converted to ‘Time` by using `Time.at(dequeued_at)` but that is intentionally left to the caller due to time zone considerations.
179 |
# File 'lib/zizq/job.rb', line 179 def zizq_dequeued_at = @zizq_job&.dequeued_at #: () -> Float? |
#zizq_id ⇒ Object
The unique job ID assigned by the server.
163 |
# File 'lib/zizq/job.rb', line 163 def zizq_id = @zizq_job&.id #: () -> String? |
#zizq_priority ⇒ Object
The priority this job was enqueued with.
173 |
# File 'lib/zizq/job.rb', line 173 def zizq_priority = @zizq_job&.priority #: () -> Integer? |
#zizq_queue ⇒ Object
The queue this job was dequeued from.
170 |
# File 'lib/zizq/job.rb', line 170 def zizq_queue = @zizq_job&.queue #: () -> String? |