Module: Creeper::Worker::ClassMethods

Includes:
Legacy::WorkerMethods
Defined in:
lib/creeper/testing.rb,
lib/creeper/worker.rb

Overview

The Creeper testing infrastructure overrides perform_async so that it does not actually touch the network. Instead it stores the asynchronous jobs in a per-class array so that their presence/absence can be asserted by your tests.

This is similar to ActionMailer’s :test delivery_method and its ActionMailer::Base.deliveries array.

Example:

require 'creeper/testing'

assert_equal 0, HardWorker.jobs.size
HardWorker.perform_async(:something)
assert_equal 1, HardWorker.jobs.size
assert_equal :something, HardWorker.jobs[0]['args'][0]

assert_equal 0, Creeper::Extensions::DelayedMailer.jobs.size
MyMailer.delayed.send_welcome_email('[email protected]')
assert_equal 1, Creeper::Extensions::DelayedMailer.jobs.size

Constant Summary collapse

DEFAULT_OPTIONS =
{ 'retry' => true, 'queue' => 'default' }

Instance Method Summary collapse

Methods included from Legacy::WorkerMethods

#creeper_legacy_queue

Instance Method Details

#client_push(opts) ⇒ Object

:nodoc:



74
75
76
# File 'lib/creeper/worker.rb', line 74

def client_push(*args) # :nodoc:
  Creeper::Client.push(*args)
end

#client_push_oldObject

:nodoc:



27
28
29
# File 'lib/creeper/testing.rb', line 27

def client_push(*args) # :nodoc:
  Creeper::Client.push(*args)
end

#creeper_options(opts = {}) ⇒ Object

Allows customization for this type of Worker. Legal options:

:queue - use a named queue for this Worker, default 'default'
:retry - enable the RetryJobs middleware for this Worker, default *true*
:timeout - timeout the perform method after N seconds, default *nil*
:backtrace - whether to save any error backtrace in the retry payload to display in web UI,
   can be true, false or an integer number of lines to save, default *false*


57
58
59
# File 'lib/creeper/worker.rb', line 57

def creeper_options(opts={})
  self.creeper_options_hash = get_creeper_options.merge(stringify_keys(opts || {}))
end

#drainObject



37
38
39
40
41
# File 'lib/creeper/testing.rb', line 37

def drain
  while job = jobs.shift do
    new.perform(*job['args'])
  end
end

#get_creeper_optionsObject

:nodoc:



63
64
65
# File 'lib/creeper/worker.rb', line 63

def get_creeper_options # :nodoc:
  self.creeper_options_hash ||= DEFAULT_OPTIONS
end

#jobsObject



33
34
35
# File 'lib/creeper/testing.rb', line 33

def jobs
  @pushed ||= []
end

#perform_async(*args) ⇒ Object



37
38
39
# File 'lib/creeper/worker.rb', line 37

def perform_async(*args)
  client_push('class' => self, 'args' => args)
end

#perform_in(interval, *args) ⇒ Object Also known as: perform_at



41
42
43
44
45
# File 'lib/creeper/worker.rb', line 41

def perform_in(interval, *args)
  int = interval.to_f
  ts = (int < 1_000_000_000 ? Time.now.to_f + int : int)
  client_push('class' => self, 'args' => args, 'at' => ts)
end

#stringify_keys(hash) ⇒ Object

:nodoc:



67
68
69
70
71
72
# File 'lib/creeper/worker.rb', line 67

def stringify_keys(hash) # :nodoc:
  hash.keys.each do |key|
    hash[key.to_s] = hash.delete(key)
  end
  hash
end