Class: BlackStack::MyProcess
- Inherits:
-
Object
- Object
- BlackStack::MyProcess
- Defined in:
- lib/myprocess.rb
Direct Known Subclasses
Constant Summary collapse
- DEFAULT_MINIMUM_ENLAPSED_SECONDS =
60
Instance Attribute Summary collapse
-
#assigned_division_changed ⇒ Object
Returns the value of attribute assigned_division_changed.
-
#assigned_process ⇒ Object
Returns the value of attribute assigned_process.
-
#assigned_process_changed ⇒ Object
Returns the value of attribute assigned_process_changed.
-
#division_name ⇒ Object
Returns the value of attribute division_name.
-
#email ⇒ Object
Returns the value of attribute email.
-
#id ⇒ Object
Returns the value of attribute id.
-
#id_client ⇒ Object
Returns the value of attribute id_client.
-
#id_division ⇒ Object
Returns the value of attribute id_division.
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#minimum_enlapsed_seconds ⇒ Object
Returns the value of attribute minimum_enlapsed_seconds.
-
#password ⇒ Object
Returns the value of attribute password.
-
#verify_configuration ⇒ Object
Returns the value of attribute verify_configuration.
-
#worker_name ⇒ Object
Returns the value of attribute worker_name.
-
#ws_port ⇒ Object
Returns the value of attribute ws_port.
-
#ws_url ⇒ Object
Returns the value of attribute ws_url.
Class Method Summary collapse
- .fullWorkerName(name) ⇒ Object
-
.kill(the_pid) ⇒ Object
ejecuta TASKKILL /F /PID #the_pid y retorna el output del comando.
-
.macaddress ⇒ Object
This function works in windows only TODO: Esta funcion no retorna la mac address completa TODO: Validar que no se retorne una macaddress virtual, con todos valores en 0.
Instance Method Summary collapse
-
#canRun? ⇒ Boolean
retorna true si el proceso hijo (child) esta habilitado para trabajar.
-
#division ⇒ Object
Get the data object of the divison assigned to this worker.
-
#doSleep(the_start_time) ⇒ Object
obtiene la diferencia en segundos entre la hora actual y el parametro the_start_time.
- #fullWorkerName ⇒ Object
-
#get ⇒ Object
obtiene sus parametros de la central.
-
#hello ⇒ Object
saluda a la central.
-
#initialize(the_worker_name, the_division_name, the_minimum_enlapsed_seconds = MyProcess::DEFAULT_MINIMUM_ENLAPSED_SECONDS, the_verify_configuration = true, the_email = nil, the_password = nil) ⇒ MyProcess
constructor
constructor.
-
#list ⇒ Object
Retorna un array de hashes.
-
#notify ⇒ Object
se notifica al dispatcher de la division.
-
#notifyError(uid, description, oid = nil, screenshot_file = nil, url = nil, assigned_process = nil) ⇒ Object
Sube un registro a la tabla boterrorlog, con el id del worker, el proceso asignado, on id de objeto relacionado (opcional) y un screenshot (opcional).
-
#pid ⇒ Object
retrieves the id of the current process.
-
#ping ⇒ Object
ping the central database.
-
#process(argv) ⇒ Object
este metodo ejecuta el trabajo para el que fue creado el objeto.
-
#run ⇒ Object
ejecuta el proceso, en modo parent, bot o child segun la clase que se implemente.
-
#set(new_assigned_process, new_id_client) ⇒ Object
notifico mis parametros (assigned_process, id_client) a la division asignada.
-
#updateWorker ⇒ Object
update worker configuration in the division.
- #whyCantRun ⇒ Object
-
#worker ⇒ Object
Get the data object of worker linked to this process.
Constructor Details
#initialize(the_worker_name, the_division_name, the_minimum_enlapsed_seconds = MyProcess::DEFAULT_MINIMUM_ENLAPSED_SECONDS, the_verify_configuration = true, the_email = nil, the_password = nil) ⇒ MyProcess
constructor
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/myprocess.rb', line 11 def initialize( the_worker_name, the_division_name, the_minimum_enlapsed_seconds=MyProcess::DEFAULT_MINIMUM_ENLAPSED_SECONDS, the_verify_configuration=true, the_email=nil, the_password=nil ) self.assigned_process_changed = false self.assigned_division_changed = false self.assigned_process = File.($0) self.worker_name = "#{the_worker_name}" self.division_name = the_division_name self.minimum_enlapsed_seconds = the_minimum_enlapsed_seconds self.verify_configuration = the_verify_configuration self.email = the_email self.password = the_password end |
Instance Attribute Details
#assigned_division_changed ⇒ Object
Returns the value of attribute assigned_division_changed.
6 7 8 |
# File 'lib/myprocess.rb', line 6 def assigned_division_changed @assigned_division_changed end |
#assigned_process ⇒ Object
Returns the value of attribute assigned_process.
7 8 9 |
# File 'lib/myprocess.rb', line 7 def assigned_process @assigned_process end |
#assigned_process_changed ⇒ Object
Returns the value of attribute assigned_process_changed.
6 7 8 |
# File 'lib/myprocess.rb', line 6 def assigned_process_changed @assigned_process_changed end |
#division_name ⇒ Object
Returns the value of attribute division_name.
7 8 9 |
# File 'lib/myprocess.rb', line 7 def division_name @division_name end |
#email ⇒ Object
Returns the value of attribute email.
8 9 10 |
# File 'lib/myprocess.rb', line 8 def email @email end |
#id ⇒ Object
Returns the value of attribute id.
7 8 9 |
# File 'lib/myprocess.rb', line 7 def id @id end |
#id_client ⇒ Object
Returns the value of attribute id_client.
7 8 9 |
# File 'lib/myprocess.rb', line 7 def id_client @id_client end |
#id_division ⇒ Object
Returns the value of attribute id_division.
7 8 9 |
# File 'lib/myprocess.rb', line 7 def id_division @id_division end |
#logger ⇒ Object
Returns the value of attribute logger.
7 8 9 |
# File 'lib/myprocess.rb', line 7 def logger @logger end |
#minimum_enlapsed_seconds ⇒ Object
Returns the value of attribute minimum_enlapsed_seconds.
7 8 9 |
# File 'lib/myprocess.rb', line 7 def minimum_enlapsed_seconds @minimum_enlapsed_seconds end |
#password ⇒ Object
Returns the value of attribute password.
8 9 10 |
# File 'lib/myprocess.rb', line 8 def password @password end |
#verify_configuration ⇒ Object
Returns the value of attribute verify_configuration.
6 7 8 |
# File 'lib/myprocess.rb', line 6 def verify_configuration @verify_configuration end |
#worker_name ⇒ Object
Returns the value of attribute worker_name.
7 8 9 |
# File 'lib/myprocess.rb', line 7 def worker_name @worker_name end |
#ws_port ⇒ Object
Returns the value of attribute ws_port.
7 8 9 |
# File 'lib/myprocess.rb', line 7 def ws_port @ws_port end |
#ws_url ⇒ Object
Returns the value of attribute ws_url.
7 8 9 |
# File 'lib/myprocess.rb', line 7 def ws_url @ws_url end |
Class Method Details
.fullWorkerName(name) ⇒ Object
123 124 125 |
# File 'lib/myprocess.rb', line 123 def self.fullWorkerName(name) "#{Socket.gethostname}.#{MyProcess.macaddress}.#{name}" end |
.kill(the_pid) ⇒ Object
ejecuta TASKKILL /F /PID #the_pid y retorna el output del comando
100 101 102 |
# File 'lib/myprocess.rb', line 100 def self.kill(the_pid) system("TASKKILL /F /PID #{the_pid}") end |
.macaddress ⇒ Object
This function works in windows only TODO: Esta funcion no retorna la mac address completa TODO: Validar que no se retorne una macaddress virtual, con todos valores en 0
119 120 121 |
# File 'lib/myprocess.rb', line 119 def self.macaddress() BlackStack::SimpleHostMonitoring.macaddress end |
Instance Method Details
#canRun? ⇒ Boolean
retorna true si el proceso hijo (child) esta habilitado para trabajar.
296 297 298 299 |
# File 'lib/myprocess.rb', line 296 def canRun?() self.assigned_process_changed == false && self.assigned_division_changed == false end |
#division ⇒ Object
Get the data object of the divison assigned to this worker. Needs database connections. So it’s available for ChildProcess only.
285 286 287 |
# File 'lib/myprocess.rb', line 285 def division() raise "This is an abstract method." end |
#doSleep(the_start_time) ⇒ Object
obtiene la diferencia en segundos entre la hora actual y el parametro the_start_time. si la diferencia es mayor al atributo minimum_enlapsed_seconds, entonces duerme el tiempo restante.
106 107 108 109 110 111 112 113 114 |
# File 'lib/myprocess.rb', line 106 def doSleep(the_start_time) # si el proceso tardo menos del minimum_enlapsed_seconds, entonces duermo el tiempo restante end_time = Time.now elapsed_seconds = end_time - the_start_time # in seconds if (elapsed_seconds < self.minimum_enlapsed_seconds) sleep_seconds = self.minimum_enlapsed_seconds - elapsed_seconds sleep(sleep_seconds) end end |
#fullWorkerName ⇒ Object
127 128 129 |
# File 'lib/myprocess.rb', line 127 def fullWorkerName() MyProcess.fullWorkerName(self.worker_name) end |
#get ⇒ Object
obtiene sus parametros de la central
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
# File 'lib/myprocess.rb', line 169 def get() # me notifico a la central. obtengo asignacion que tenga url = "#{BlackStack::Pampa::api_url}/api1.3/pampa/get.json" #puts #puts #puts "url: #{url}" #puts #puts res = BlackStack::Netting::call_post(url, { 'api_key' => BlackStack::Pampa::api_key, 'name' => self.fullWorkerName }.merge( BlackStack::RemoteHost.new.poll ) ) parsed = JSON.parse(res.body) if (parsed['status'] != BlackStack::Netting::SUCCESS) raise parsed['status'].to_s else if self.verify_configuration # si ya tenia un proceso asignado, y ahora se le asigna un nuevo proceso if self.assigned_process.to_s.size > 0 a = File.(self.assigned_process) b = File.(parsed['assigned_process']) if a != b self.assigned_process_changed = true else self.assigned_process_changed = false end end # si ya tenia un proceso asignado, y ahora se le asigna un nuevo proceso if self.id_division.to_s.size > 0 if self.id_division.to_guid != parsed['id_division'].to_guid self.assigned_division_changed = true else self.assigned_division_changed = false end end end # verify_configuration # si ya tenia asignada una division, entonces le notifico mi nueva configuracion self.set(parsed['assigned_process'], parsed['id_client']) self.id = parsed['id'] self.assigned_process = parsed['assigned_process'] self.id_client = parsed['id_client'] self.id_division = parsed['id_division'] self.division_name = parsed['division_name'] self.ws_url = parsed['ws_url'] self.ws_port = parsed['ws_port'] # le notifico a la nueva division asignada mi nueva configuracion self.set(parsed['assigned_process'], parsed['id_client']) end end |
#hello ⇒ Object
saluda a la central
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/myprocess.rb', line 132 def hello() # me notifico a la central. obtengo asignacion si ya la tenia url = "#{BlackStack::Pampa::api_url}/api1.3/pampa/hello.json" #puts #puts #puts "url: #{url}" #puts #puts res = BlackStack::Netting::call_post(url, { 'api_key' => BlackStack::Pampa::api_key, 'name' => self.fullWorkerName }.merge( BlackStack::RemoteHost.new.poll ) ) parsed = JSON.parse(res.body) if (parsed['status'] != BlackStack::Netting::SUCCESS) raise parsed['status'].to_s end end |
#list ⇒ Object
Retorna un array de hashes.
> Cada elemento del hash tiene la forma: :pid, :ppid,
> donde imagename es el patch completo del proceso, pid es el id del proceso
> y ppid es el id del proceso padre.
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/myprocess.rb', line 81 def list() a = [] s = `wmic process get executablepath,processid,parentprocessid` s.split(/\n+/).each { |e| aux = e.strip.scan(/^(.+)\s+(\d+)\s+(\d+)$/)[0] if (aux!=nil) if (aux.size>=3) a << { :executablepath => aux[0].strip.to_s, :pid => aux[2].to_s, # TODO: deberia ser aux[1], pero por algo que no entiendo ahora el pid viene en aux[2] :ppid => aux[1].to_s, # TODO: deberia ser aux[2], pero por algo que no entiendo ahora el pid viene en aux[1] } end end } a end |
#notify ⇒ Object
se notifica al dispatcher de la division
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 |
# File 'lib/myprocess.rb', line 257 def notify() if (self.ws_url==nil || self.ws_port==nil) raise "Cannot notify. Worker not exists, or it has not parameters, or it is belong another client. Check your api_key, and check the name of the worker." end # me notifico a la division. obtengo trabajo url = "#{BlackStack::Pampa::api_protocol}://#{self.ws_url}:#{self.ws_port}/api1.3/pampa/notify.json" #puts #puts #puts "url: #{url}" #puts #puts res = BlackStack::Netting::call_post(url, { 'api_key' => BlackStack::Pampa::api_key, 'name' => self.fullWorkerName, 'assigned_process' => self.assigned_process, 'id_client' => self.id_client, 'id_division' => self.id_division }.merge( BlackStack::RemoteHost.new.poll ) ) parsed = JSON.parse(res.body) if (parsed['status'] != "success") raise parsed['status'].to_s end end |
#notifyError(uid, description, oid = nil, screenshot_file = nil, url = nil, assigned_process = nil) ⇒ Object
Sube un registro a la tabla boterrorlog, con el id del worker, el proceso asignado, on id de objeto relacionado (opcional) y un screenshot (opcional).
uid: id de un registro en la tabla lnuser. description: backtrace de la excepcion.
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/myprocess.rb', line 35 def notifyError(uid, description, oid=nil, screenshot_file=nil, url=nil, assigned_process=nil) url = !url.nil? ? url : "#{BlackStack::Pampa::api_protocol}://#{self.ws_url}:#{self.ws_port}/api1.3/bots/boterror.json" # subo el error nTries = 0 bSuccess = false parsed = nil sError = "" while (nTries < 5 && bSuccess == false) begin nTries = nTries + 1 res = BlackStack::Netting::call_post(url, # TODO: migrar a RestClient para poder hacer file upload 'api_key' => BlackStack::Pampa::api_key, 'id_lnuser' => uid, 'id_object' => oid, 'worker_name' => PROCESS.fullWorkerName, 'process' => !assigned_process.nil? ? assigned_process : PROCESS.worker.assigned_process, 'description' => description, 'screenshot' => screenshot_file, ) parsed = JSON.parse(res.body) if (parsed['status']=='success') bSuccess = true else sError = parsed['status'] end rescue Errno::ECONNREFUSED => e sError = "Errno::ECONNREFUSED:" + e.to_console rescue => e2 sError = "Exception:" + e2.to_console end end # while if (bSuccess==false) raise "#{sError}" end end |
#pid ⇒ Object
retrieves the id of the current process
73 74 75 |
# File 'lib/myprocess.rb', line 73 def pid() Process.pid.to_s end |
#ping ⇒ Object
ping the central database
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 |
# File 'lib/myprocess.rb', line 230 def ping() # me notifico a la central. url = "#{BlackStack::Pampa::api_url}/api1.3/pampa/ping.json" res = BlackStack::Netting::call_post(url, { 'api_key' => BlackStack::Pampa::api_key, 'name' => self.fullWorkerName }.merge( BlackStack::RemoteHost.new.poll ) ) parsed = JSON.parse(res.body) if (parsed['status'] != BlackStack::Netting::SUCCESS) raise parsed['status'].to_s end # me notifico a la division. if (self.ws_url != nil && self.ws_port != nil) url = "#{BlackStack::Pampa::api_protocol}://#{self.ws_url.to_s}:#{self.ws_port.to_s}/api1.3/pampa/ping.json" res = BlackStack::Netting::call_post(url, { 'api_key' => BlackStack::Pampa::api_key, 'name' => self.fullWorkerName }.merge( BlackStack::RemoteHost.new.poll ) ) parsed = JSON.parse(res.body) if (parsed['status'] != "success") raise parsed['status'].to_s end end # if end |
#process(argv) ⇒ Object
este metodo ejecuta el trabajo para el que fue creado el objeto.
313 314 315 |
# File 'lib/myprocess.rb', line 313 def process(argv) raise "This is an abstract method." end |
#run ⇒ Object
ejecuta el proceso, en modo parent, bot o child segun la clase que se implemente. en modo parent, hace un loop infinito. en modo bot o child, hace un loop hasta que el metodo canRun? retorne false. en modo bot o child, invoca al metodo process() en cada ciclo.
321 322 323 |
# File 'lib/myprocess.rb', line 321 def run() #raise "This is an abstract method" end |
#set(new_assigned_process, new_id_client) ⇒ Object
notifico mis parametros (assigned_process, id_client) a la division asignada
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/myprocess.rb', line 151 def set(new_assigned_process, new_id_client) if (self.ws_url.to_s.size > 0 && self.ws_port.to_s.size > 0) url = "#{BlackStack::Pampa::api_protocol}://#{self.ws_url.to_s}:#{self.ws_port.to_s}/api1.3/pampa/notify.json" #puts #puts #puts "url: #{url}" #puts #puts res = BlackStack::Netting::call_post(url, { 'api_key' => BlackStack::Pampa::api_key, 'name' => self.fullWorkerName, 'assigned_process' => new_assigned_process, 'id_client' => new_id_client }.merge( BlackStack::RemoteHost.new.poll ) ) end end |
#updateWorker ⇒ Object
update worker configuration in the division
225 226 227 |
# File 'lib/myprocess.rb', line 225 def updateWorker() raise "Abstract Method." end |
#whyCantRun ⇒ Object
301 302 303 304 305 306 307 308 309 |
# File 'lib/myprocess.rb', line 301 def whyCantRun() if self.assigned_process_changed == true return "Assigned process has changed." elsif self.assigned_division_changed == true return "Assigned division has changed." else return "unknown" end end |
#worker ⇒ Object
Get the data object of worker linked to this process. Needs database connections. So it’s available for ChildProcess only.
291 292 293 |
# File 'lib/myprocess.rb', line 291 def worker() raise "This is an abstract method." end |