Class: PgQueue::Worker

Inherits:
Object
  • Object
show all
Defined in:
lib/pg_queue/worker.rb

Instance Method Summary collapse

Instance Method Details

#running?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/pg_queue/worker.rb', line 33

def running?
  @running
end

#startObject



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/pg_queue/worker.rb', line 3

def start
  @running = true

  PgQueue.connection.listen(:pg_queue_jobs)
  while running?
    job = PgQueue.dequeue
    if job
      perform(job)
      sleep(PgQueue.interval) if PgQueue.interval > 0
      next
    end

    PgQueue.logger.debug("waiting for jobs")
    PgQueue.connection.wait_for_notify do |event, pid, payload|
      if payload == "stop"
        PgQueue.logger.debug("stop notify received")
        stop
      else
        PgQueue.logger.debug("let's perform some jobs")
      end
    end
  end
  PgQueue.connection.unlisten(:pg_queue_jobs)
end

#stopObject



28
29
30
31
# File 'lib/pg_queue/worker.rb', line 28

def stop
  @running = false
  PgQueue.connection.new_connection.notify(:pg_queue_jobs, "stop")
end