Module: Resque::Delayed

Defined in:
lib/resque-delayed/worker.rb,
lib/resque-delayed/version.rb,
lib/resque-delayed/resque-delayed.rb

Defined Under Namespace

Classes: Worker

Constant Summary collapse

VERSION =
"1.3.0"

Class Method Summary collapse

Class Method Details

.clearObject



10
11
12
# File 'lib/resque-delayed/resque-delayed.rb', line 10

def clear
  Resque.redis.del @queue
end

.countObject



14
15
16
# File 'lib/resque-delayed/resque-delayed.rb', line 14

def count
  Resque.redis.zcard @queue
end

.create_at(time, *args) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/resque-delayed/resque-delayed.rb', line 18

def create_at(time, *args)
  klass, *args = args
  queue = Resque.queue_from_class klass
  # validate here so that the Resque::Delayed worker doesn't have
  # to worry about it before enqueueing
  Resque.validate(klass, queue)
  Resque.redis.zadd @queue, time.to_i, encode(queue, klass, *args)
end

.create_in(offset, *args) ⇒ Object



27
28
29
# File 'lib/resque-delayed/resque-delayed.rb', line 27

def create_in(offset, *args)
  create_at Time.now + offset, *args
end

.decode(encoded_job) ⇒ Object



35
36
37
# File 'lib/resque-delayed/resque-delayed.rb', line 35

def decode(encoded_job)
  Resque.decode encoded_job.split('|', 2).last
end

.encode(queue, klass, *args) ⇒ Object



31
32
33
# File 'lib/resque-delayed/resque-delayed.rb', line 31

def encode(queue, klass, *args)
  "#{random_uuid}|#{Resque.encode([queue, klass.to_s, *args])}"
end

.nextObject



39
40
41
# File 'lib/resque-delayed/resque-delayed.rb', line 39

def next
  next_at Time.now
end

.next_at(time) ⇒ Object



43
44
45
46
47
48
49
50
51
52
# File 'lib/resque-delayed/resque-delayed.rb', line 43

def next_at(time)
  job = peek_at_serialized(time)

  # it is possible that another process will pull this job out of the
  # queue before this process has a chance. if that happens, return nil
  # so that we don't end up duplicating the job.
  return unless job and Resque.redis.zrem(@queue, job)

  Resque::Delayed.decode job
end

.peekObject



54
55
56
# File 'lib/resque-delayed/resque-delayed.rb', line 54

def peek
  peek_at Time.now
end

.peek_at(time) ⇒ Object



58
59
60
61
# File 'lib/resque-delayed/resque-delayed.rb', line 58

def peek_at(time)
  job = peek_at_serialized(time)
  job and Resque::Delayed.decode(job)
end

.peek_at_serialized(time) ⇒ Object



63
64
65
66
# File 'lib/resque-delayed/resque-delayed.rb', line 63

def peek_at_serialized(time)
  Resque.redis.
    zrangebyscore(@queue, '-inf', time.to_i, :limit => [0, 1]).first
end

.random_uuidObject



6
7
8
# File 'lib/resque-delayed/resque-delayed.rb', line 6

def random_uuid
  UUIDTools::UUID.random_create.to_s.gsub('-', '')
end