Class: ProcessManager

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/sinatra/extensions/processmanager.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeProcessManager

Returns a new instance of ProcessManager.



75
76
77
# File 'lib/sinatra/extensions/processmanager.rb', line 75

def initialize
  @handlers = []
end

Instance Attribute Details

#handlersObject (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

Returns:

  • (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