Class: Trinidad::Extensions::Resque::ResqueLifecycleListener

Inherits:
Object
  • Object
show all
Includes:
Tomcat::LifecycleListener
Defined in:
lib/resque_lifecycle_listener.rb

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ ResqueLifecycleListener

Returns a new instance of ResqueLifecycleListener.



12
13
14
# File 'lib/resque_lifecycle_listener.rb', line 12

def initialize(options)
  @options = options
end

Instance Method Details

#configure_workersObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/resque_lifecycle_listener.rb', line 39

def configure_workers
  task = 'resque:work'

  if @options[:count]
    ENV['COUNT'] = @options[:count].to_s
    task = 'resque:workers'
  end

  ENV['QUEUES'] ||= @options[:queues]

  ::Resque.redis = @options[:redis_host]

  load @options[:setup] if @options[:setup]
  task
end

#invoke_workers(task) ⇒ Object



55
56
57
58
59
60
# File 'lib/resque_lifecycle_listener.rb', line 55

def invoke_workers(task)
  Rake::Task[task].invoke
rescue Errno::ECONNREFUSED
  puts "WARN: Cannot connect with Redis. Please restart the server when Redis is up again."
  @redis_econnref = true
end

#lifecycle_event(event) ⇒ Object



16
17
18
19
20
21
22
23
# File 'lib/resque_lifecycle_listener.rb', line 16

def lifecycle_event(event)
  case event.type
  when Trinidad::Tomcat::Lifecycle::BEFORE_START_EVENT
    start_workers
  when Trinidad::Tomcat::Lifecycle::BEFORE_STOP_EVENT
    stop_workers
  end
end

#load_tasksObject



33
34
35
36
37
# File 'lib/resque_lifecycle_listener.rb', line 33

def load_tasks
  Dir.glob(File.join(@options[:path], '**', '*.rb')).each do |path|
    load path
  end
end

#start_workersObject



25
26
27
28
29
30
31
# File 'lib/resque_lifecycle_listener.rb', line 25

def start_workers
  Thread.new do
    load_tasks
    task = configure_workers
    invoke_workers task
  end
end

#stop_workersObject



62
63
64
65
# File 'lib/resque_lifecycle_listener.rb', line 62

def stop_workers
  return if @redis_econnref # double check redis is connected, otherwise return
  ::Resque.workers.each { |w| w.shutdown! }
end