Module: Resque::Plugins::Approval

Defined in:
lib/resque-approval.rb,
lib/resque-approval/version.rb

Constant Summary collapse

VERSION =
'1.2.1'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.pending_job_keysObject



7
8
9
10
11
# File 'lib/resque-approval.rb', line 7

def self.pending_job_keys
  keys = Resque.redis.hkeys('pending_jobs')
  keys.map! { |key| Resque.decode(key) }
  keys.sort! { |a, b| a['id'] <=> b['id'] }
end

Instance Method Details

#approve(key) ⇒ Object



56
57
58
59
60
61
62
63
64
# File 'lib/resque-approval.rb', line 56

def approve(key)
  job = remove_from_pending(key)

  return false if job.nil?

  Resque.push(Resque.queue_from_class(self), job)

  true
end

#before_delayed_enqueue(args) ⇒ Object



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

def before_delayed_enqueue(args)
  key = extract_value(args, :approval_key)
  Resque.redis.hdel('pending_jobs', key)
end

#before_enqueue_approval(*args) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/resque-approval.rb', line 13

def before_enqueue_approval(*args)
  args = args[0] || {}

  requires_approval = extract_value(args, :requires_approval)
  if requires_approval
    enqueue_for_approval(args)
    allow_enqueue = false
  else
    allow_enqueue = true
  end

  allow_enqueue
end

#enqueue_for_approval(*args) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/resque-approval.rb', line 32

def enqueue_for_approval(*args)
  args = args[0] || {}

  message = extract_value(args, :approval_message)
  timeout = extract_value(args, :approval_timeout)

  id = Resque.redis.hlen('pending_jobs')
  key = build_key(id, message, timeout)

  if timeout.kind_of?(Numeric) && timeout > 0 && Resque.respond_to?(:enqueue_in)
    approval_args = args.merge(:approval_key => key)
    Resque.enqueue_in(timeout, self, approval_args)

    queue = Resque.queue_from_class(self)
    value = build_value(queue, approval_args)
  else
    Resque.enqueue_to(:approval_required, self, args)

    value = build_value(nil, args)
  end

  Resque.redis.hset('pending_jobs', key, value)
end

#reject(key) ⇒ Object



66
67
68
# File 'lib/resque-approval.rb', line 66

def reject(key)
  !!remove_from_pending(key)
end

#remove_from_pending(key) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/resque-approval.rb', line 70

def remove_from_pending(key)
  value = Resque.redis.hget('pending_jobs', key)

  return if value.nil?

  encoded_job = value
  job = Resque.decode(value)

  Resque.redis.hdel('pending_jobs', key)

  decoded_key = Resque.decode(key)
  if decoded_key.has_key? 'approval_timeout'
    Array(Resque.redis.keys("delayed:*")).each do |key|
      destroyed = Resque.redis.lrem(key, 1, encoded_job)
      break if destroyed > 0
    end
  else
    Resque.redis.lrem('queue:approval_required', 1, encoded_job)
  end

  job
end