Class: QC::Queue

Inherits:
Object
  • Object
show all
Defined in:
lib/queue_classic_batches/queue.rb

Instance Method Summary collapse

Instance Method Details

#enqueue_batch(method, batch_id, *args) ⇒ Object



5
6
7
8
9
10
11
12
13
# File 'lib/queue_classic_batches/queue.rb', line 5

def enqueue_batch(method, batch_id, *args)
  batch_method = 'QC::Batches::Batch.perform_job'
  args = args.unshift(batch_id)
  args = args.unshift(method)
  QC.log_yield(:measure => 'queue.enqueue_batch') do
    s="INSERT INTO #{TABLE_NAME} (q_name, method, batch_id, args) VALUES ($1, $2, $3, $4)"
    res = conn_adapter.execute(s, name, batch_method, batch_id, JSON.dump(args))
  end
end

#lockObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/queue_classic_batches/queue.rb', line 15

def lock
  #we have to patch the entire lock method to get it to return batch_id
  QC.log_yield(:measure => 'queue.lock') do
    s = "SELECT * FROM lock_head($1, $2)"
    if r = conn_adapter.execute(s, name, top_bound)
      {}.tap do |job|
        job[:id] = r["id"]
        job[:method] = r["method"]
        job[:batch_id] = r["batch_id"]
        job[:args] = JSON.parse(r["args"])
        if r["created_at"]
          job[:created_at] = Time.parse(r["created_at"])
          ttl = Integer((Time.now - job[:created_at]) * 1000)
          QC.measure("time-to-lock=#{ttl}ms source=#{name}")
        end
      end
    end
  end
end