Module: BackgroundQueue::Worker::Calling

Defined in:
lib/background_queue/worker/calling.rb

Overview

this module is mixed into the controller that will service the worker calls. the action that will recive the worker calls should only call run_worker

Instance Method Summary collapse

Instance Method Details

#call_worker(worker, env) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/background_queue/worker/calling.rb', line 37

def call_worker(worker, env)
  headers['X-Accel-Buffering'] = 'no' #passenger standalone uses nginx. This will turn buffering off in nginx
  render :text => lambda { |response,output| 
    env.set_output(output)
    begin
      worker.run
    rescue Exception=>e
      logger.error("Error calling worker: #{e.message}")
      logger.error(e.backtrace.join("\n"))
    ensure
      worker.set_environment(nil)
    end
  }, :type=>"text/text"
end

#check_secretObject



52
53
54
55
56
# File 'lib/background_queue/worker/calling.rb', line 52

def check_secret
  return true if params[:auth] == BackgroundQueue::Worker::Config.secret
  render :text=>"Invalid auth (#{params[:auth]})", :status=>401
  false
end

#init_environmentObject



31
32
33
34
35
# File 'lib/background_queue/worker/calling.rb', line 31

def init_environment
  env = BackgroundQueue::Worker::Environment.new
  env.init_from_controller(self)
  env
end

#run_workerObject

call this method from within the controller action that receives the worker calls. pass the shared secret in to validate the call: this should equal the secret in the server config.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/background_queue/worker/calling.rb', line 8

def run_worker
  return unless check_secret()
  worker = nil
  env = nil
  begin
    #setup worker environment
    env = init_environment
    #get worker
    worker = BackgroundQueue::Worker::WorkerLoader.get_worker(env.worker)
    worker.set_environment(env)
  rescue Exception=>e
    logger.error("Error initializing worker: #{e.message}")
    logger.debug(e.backtrace.join("\n"))
    render :text=>"Error initializing worker: #{e.message}", :status=>500
    raise e
  end
  #puts worker
  #call worker
  call_worker(worker, env)
  true

end