Class: ProcessManager
- Inherits:
-
Object
- Object
- ProcessManager
- Includes:
- Singleton
- Defined in:
- lib/sinatra/extensions/processmanager.rb
Instance Attribute Summary collapse
-
#handlers ⇒ Object
readonly
Returns the value of attribute handlers.
Instance Method Summary collapse
- #clean(clazz, user_id) ⇒ Object
- #find(clazz, user_id) ⇒ Object
- #finish(handler) ⇒ Object
-
#initialize ⇒ ProcessManager
constructor
A new instance of ProcessManager.
- #run(*args) ⇒ Object
- #running?(clazz, user_id) ⇒ Boolean
Constructor Details
#initialize ⇒ ProcessManager
Returns a new instance of ProcessManager.
75 76 77 |
# File 'lib/sinatra/extensions/processmanager.rb', line 75 def initialize @handlers = [] end |
Instance Attribute Details
#handlers ⇒ Object (readonly)
Returns the value of attribute handlers.
73 74 75 |
# File 'lib/sinatra/extensions/processmanager.rb', line 73 def handlers @handlers end |
Instance Method Details
#clean(clazz, user_id) ⇒ Object
102 103 104 105 106 107 |
# File 'lib/sinatra/extensions/processmanager.rb', line 102 def clean(clazz,user_id) @handlers.select { |handler| handler.clazz == clazz && handler.user_id == user_id }.each do |handler| handler.interrupt self.finish(handler) end end |
#find(clazz, user_id) ⇒ Object
79 80 81 |
# File 'lib/sinatra/extensions/processmanager.rb', line 79 def find(clazz,user_id) @handlers.find { |handler| handler.clazz == clazz && handler.user_id == user_id } end |
#finish(handler) ⇒ Object
98 99 100 |
# File 'lib/sinatra/extensions/processmanager.rb', line 98 def finish(handler) @handlers -= [handler] end |
#run(*args) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/sinatra/extensions/processmanager.rb', line 83 def run(*args) user_id = args[1][:user_id] clean(args.first.to_s,user_id) if !running?(args.first.to_s,user_id) handler = ProcessHandler.new(rand(1..100000000), args.first.name, args.first.to_s, user_id) clazz = args.first args[0] = handler clazz.perform_async(*args) @handlers << handler handler else find(args.first.to_s,user_id) end end |
#running?(clazz, user_id) ⇒ Boolean
109 110 111 |
# File 'lib/sinatra/extensions/processmanager.rb', line 109 def running?(clazz,user_id) !@handlers.select { |handler| handler.clazz == clazz && handler.user_id == user_id }.empty? end |