Module: Faktory::Job

Includes:
Trackable
Included in:
ActiveJob::QueueAdapters::FaktoryAdapter::JobWrapper
Defined in:
lib/faktory/job.rb,
lib/faktory/testing.rb

Overview

Include this module in your Job class and you can easily create asynchronous jobs:

class HardJob

include Faktory::Job

def perform(*args)
  # do some work
end

end

Then in your Rails app, you can do this:

HardJob.perform_async(1, 2, 3)

Note that perform_async is a class method, perform is an instance method.

Defined Under Namespace

Modules: ClassMethods, Util Classes: Setter

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Trackable

#track_progress

Instance Attribute Details

#_customObject

Returns the value of attribute _custom.



26
27
28
# File 'lib/faktory/job.rb', line 26

def _custom
  @_custom
end

#bidObject

Returns the value of attribute bid.



25
26
27
# File 'lib/faktory/job.rb', line 25

def bid
  @bid
end

#jidObject

Returns the value of attribute jid.



24
25
26
# File 'lib/faktory/job.rb', line 24

def jid
  @jid
end

Class Method Details

.clear_allObject

Clear all queued jobs across all workers



314
315
316
# File 'lib/faktory/testing.rb', line 314

def clear_all
  Queues.clear_all
end

.drain_allObject

Drain all queued jobs across all workers



319
320
321
322
323
324
325
326
327
# File 'lib/faktory/testing.rb', line 319

def drain_all
  while jobs.any?
    worker_classes = jobs.map { |job| job["jobtype"] }.uniq

    worker_classes.each do |worker_class|
      Faktory::Testing.constantize(worker_class).drain
    end
  end
end

.included(base) ⇒ Object

Raises:

  • (ArgumentError)


30
31
32
33
34
35
# File 'lib/faktory/job.rb', line 30

def self.included(base)
  raise ArgumentError, "You cannot include Faktory::Job in an ActiveJob: #{base.name}" if base.ancestors.any? { |c| c.name == "ActiveJob::Base" }

  base.extend(ClassMethods)
  base.faktory_class_attribute :faktory_options_hash
end

.jobsObject

:nodoc:



309
310
311
# File 'lib/faktory/testing.rb', line 309

def jobs # :nodoc:
  Queues.jobs_by_queue.values.flatten
end

.set(options) ⇒ Object



37
38
39
# File 'lib/faktory/job.rb', line 37

def self.set(options)
  Setter.new(options)
end

Instance Method Details

#batchObject

this method is typically called within a batch job to reopen the batch in order to add more jobs.



43
44
45
46
47
# File 'lib/faktory/job.rb', line 43

def batch
  if bid
    @batch ||= Faktory::Batch.new(bid)
  end
end

#loggerObject



61
62
63
# File 'lib/faktory/job.rb', line 61

def logger
  Faktory.logger
end

#parent_batchObject

this method is typically called within a child batch callback to reopen the parent batch in order to add more jobs or workflow steps.



51
52
53
54
55
# File 'lib/faktory/job.rb', line 51

def parent_batch
  if parent_bid
    @parent_batch ||= Faktory::Batch.new(parent_bid)
  end
end

#parent_bidObject



57
58
59
# File 'lib/faktory/job.rb', line 57

def parent_bid
  _custom["_pbid"]
end