Class: SimpleJob::Worker

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

Instance Method Summary collapse

Methods inherited from Client

#bind, #exchange, #orig_send, #publish, #queue, #send, start, #subscribe

Instance Method Details

#handle(key, &proc) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/simplejob/worker.rb', line 26

def handle(key, &proc)
  # bind the message to the queue
  bind(@queue, key)

  # register the handler
  install_handler(key, proc)
end

#start(opts, &proc) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/simplejob/worker.rb', line 3

def start(opts, &proc)
  super do
    # only fetch one message at a time instead of being greedy
    @channel.prefetch(1)

    # create the queue
    @queue = queue(opts[:queue_name] || "job_queue")

    # defer to instance to set up handlers
    instance_exec(&proc)

    # start processing jobs
    start_handler_loop

    log.info "[worker] Started"
  end
end

#stop ⇒ Object



21
22
23
24
# File 'lib/simplejob/worker.rb', line 21

def stop
  super
  log.info "[worker] Stopped"
end