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



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/background_queue/worker/calling.rb', line 49

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)
    set_process_name(env)
    begin
      case env.step
      when "start"
        worker.start
      when "finish"
        worker.finish
      else
        worker.run
      end
    rescue Exception=>e
      logger.error("Error calling worker: #{e.message}")
      logger.error(e.backtrace.join("\n"))
      worker.logger.error("Error calling worker: #{e.message}")
      worker.logger.error(e.backtrace.join("\n"))
    ensure
      worker.set_environment(nil)
      revert_process_name
    end
  }, :type=>"text/text"
end

#check_secretObject



75
76
77
78
79
# File 'lib/background_queue/worker/calling.rb', line 75

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

#revert_process_nameObject



43
44
45
46
47
# File 'lib/background_queue/worker/calling.rb', line 43

def revert_process_name
  unless BackgroundQueue::Worker::Config.process_name_prefix.nil?
    $0 = "#{BackgroundQueue::Worker::Config.process_name_prefix}:idle"
  end
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

#set_process_name(env) ⇒ Object



37
38
39
40
41
# File 'lib/background_queue/worker/calling.rb', line 37

def set_process_name(env)
  unless BackgroundQueue::Worker::Config.process_name_prefix.nil?
    $0 = "#{BackgroundQueue::Worker::Config.process_name_prefix}:#{env.worker}:#{env.owner_id}:#{env.job_id}:#{env.task_id}"
  end
end