Class: Afterparty::Worker

Inherits:
Object
  • Object
show all
Includes:
QueueHelpers
Defined in:
lib/afterparty/worker.rb

Instance Attribute Summary

Attributes included from QueueHelpers

#options

Instance Method Summary collapse

Methods included from QueueHelpers

#authenticate, #clear, #completed, #config_login, #handle_exception, #initialize, #jobs, #jobs_empty?, #last_completed, #next_valid_job, #run, #total_jobs_count, #valid_jobs

Instance Method Details

#consumeObject



5
6
7
8
9
10
11
# File 'lib/afterparty/worker.rb', line 5

def consume
  @stopped = false
  @thread = Thread.new {
    consume_sync
  }
  @thread
end

#consume_nextObject



13
14
15
16
17
# File 'lib/afterparty/worker.rb', line 13

def consume_next
  if (job = next_valid_job)
    run job
  end
end

#consume_syncObject



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/afterparty/worker.rb', line 19

def consume_sync
  while !@stopped
    job = next_valid_job
    if job
      puts "Executing job: #{job.id}" if job.respond_to? :id
      run job
    else
      sleep(@options[:sleep])
    end
  end
end

#stopObject



31
32
33
34
# File 'lib/afterparty/worker.rb', line 31

def stop
  @stopped = true
  @thread.join(0) if @thread
end