Class: Workforce::Controller
- Inherits:
-
Object
- Object
- Workforce::Controller
- Includes:
- Singleton
- Defined in:
- lib/workforce/controller.rb
Constant Summary collapse
- ACTIONS =
%w(list run status start stop restart schedule dispose)
Instance Method Summary collapse
- #dispatch(action, *args) ⇒ Object
- #dispose(*workers) ⇒ Object
- #list ⇒ Object
- #restart(*workers) ⇒ Object
- #run(*workers) ⇒ Object
- #schedule(*workers) ⇒ Object
- #start(*workers) ⇒ Object
- #status(*workers) ⇒ Object
- #stop(*workers) ⇒ Object
Instance Method Details
#dispatch(action, *args) ⇒ Object
9 10 11 12 13 14 15 |
# File 'lib/workforce/controller.rb', line 9 def dispatch(action, *args) if ACTIONS.include?(action.to_s) send(action, *args) else raise ArgumentError, "Invalid action" end end |
#dispose(*workers) ⇒ Object
96 97 98 99 100 101 102 103 104 105 |
# File 'lib/workforce/controller.rb', line 96 def dispose(*workers) perform_action(:dispose, workers) do |worker_klass| unless running?(worker_klass) puts "#{worker_klass}'s manager not running yet" exit(1) end Process.kill(:USR2, running_pid(worker_klass)) end end |
#list ⇒ Object
17 18 19 20 21 22 23 |
# File 'lib/workforce/controller.rb', line 17 def list puts "Available workers:" available_workers.each do |worker| puts " * #{worker}" end end |
#restart(*workers) ⇒ Object
68 69 70 71 72 73 |
# File 'lib/workforce/controller.rb', line 68 def restart(*workers) perform_action(:restart, workers) do |worker_klass| stop(worker_klass) if running?(worker_klass) start(worker_klass) end end |
#run(*workers) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/workforce/controller.rb', line 25 def run(*workers) perform_action(:start, workers) do |worker_klass| if running?(worker_klass) puts "#{worker_klass}'s manager already running" else manager = Workforce::Manager.new(worker_klass) store_pid(worker_klass, Process.pid) manager.run remove_pid_file(worker_klass) end end end |
#schedule(*workers) ⇒ Object
85 86 87 88 89 90 91 92 93 94 |
# File 'lib/workforce/controller.rb', line 85 def schedule(*workers) perform_action(:schedule, workers) do |worker_klass| unless running?(worker_klass) puts "#{worker_klass}'s manager not running yet" else puts "Scheduling new instance of #{worker_klass}" Process.kill(:USR1, running_pid(worker_klass)) end end end |
#start(*workers) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/workforce/controller.rb', line 38 def start(*workers) perform_action(:start, workers) do |worker_klass| if running?(worker_klass) puts "#{worker_klass}'s manager already running" else puts "Starting #{worker_klass}" manager = Workforce::Manager.new(worker_klass) store_pid(worker_klass, manager.launch) num = Config.instance.schedule(worker_klass.name) puts "Scheduling #{num} instances" num.times { sleep 0.005; schedule(worker_klass) } end end end |
#status(*workers) ⇒ Object
75 76 77 78 79 80 81 82 83 |
# File 'lib/workforce/controller.rb', line 75 def status(*workers) perform_action(:status, workers) do |worker_klass| if running?(worker_klass) puts "#{worker_klass}: Running (#{running_pid(worker_klass)})" else puts "#{worker_klass}: Not running" end end end |
#stop(*workers) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/workforce/controller.rb', line 53 def stop(*workers) perform_action(:stop, workers) do |worker_klass| if running?(worker_klass) puts "Stopping #{worker_klass}" begin Process.kill(:QUIT, running_pid(worker_klass)) rescue Errno::ESRCH end remove_pid_file(worker_klass) else puts "#{worker_klass} is not running" end end end |