Class: BlackStack::MyParentProcess

Inherits:
MyProcess
  • Object
show all
Defined in:
lib/myparentprocess.rb

Overview

es un proceso sin conexion a base de datos, que itera infinitamente. en cada iteracion saluda a la central (hello), obtiene parametros (get)

Constant Summary

Constants inherited from MyProcess

BlackStack::MyProcess::DEFAULT_MINIMUM_ENLAPSED_SECONDS

Instance Attribute Summary

Attributes inherited from MyProcess

#assigned_division_changed, #assigned_process, #assigned_process_changed, #division_name, #email, #id, #id_client, #id_division, #logger, #minimum_enlapsed_seconds, #password, #verify_configuration, #worker_name, #ws_port, #ws_url

Instance Method Summary collapse

Methods inherited from MyProcess

#canRun?, #division, #doSleep, #fullWorkerName, fullWorkerName, #get, #hello, #initialize, kill, #list, macaddress, #notify, #pid, #ping, #process, #set, #updateWorker, #whyCantRun, #worker

Constructor Details

This class inherits a constructor from BlackStack::MyProcess

Instance Method Details

#runObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/myparentprocess.rb', line 6

def run()
    super
    
    # creo el objeto logger
    self.logger = BlackStack::RemoteLogger.new(
      "#{self.fullWorkerName}.log",
      BlackStack::Pampa::api_protocol, 
      BlackStack::Pampa::api_domain, 
      BlackStack::Pampa::api_port, 
      BlackStack::Pampa::api_key,
      self.id_client # ID of the client that has this thread assigned
    )
  
    # 
    pid = nil
    while (true)
      begin
        GC.start # 331 - avoid lack of memory
        #DB.disconnect # este proceso esta desacoplado de la conexion a la base de datos
  
        # reseteo en contador nested del logger
        self.logger.reset()
  
        # get the start time 
        start_time = Time.now
        
        # consulto a la central por la division asignada
        url = "#{BlackStack::Pampa::api_url}/api1.3/pampa/hello.json"
        logger.logs("Hello to the central... ")
        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)
          self.logger.logf("Error: " + parsed['status'].to_s)
        else
          self.logger.done
  
          logger.logs("Get worker data... ")
          url = "#{BlackStack::Pampa::api_url}/api1.3/pampa/get.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)
            self.logger.logf("Error: " + parsed['status'].to_s)
          else
            # map response
            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']
            self.logger.logf "done (#{self.division_name})"
  
            # 
            self.logger.logs "Notify division... "
            if self.division_name.to_s.size == 0
              self.logger.logf "no division assigned"                                
            else
              self.notify # notifico a la division
              self.logger.done
  
              # 
              self.logger.logs "Spawn child process... "    
              # lanzo el proceso
              if self.assigned_process.to_s.size > 0
                command = "ruby #{self.assigned_process} name=#{self.worker_name} division=#{self.division_name}"
                pid = Process.spawn(command)
                logger.logf "done (pid=#{pid.to_s})" 
                
                logger.log("Wait to child process to finish.")
                Process.wait(pid)
              else
                if self.assigned_process.to_s.size == 0
                  self.logger.logf "no process assigned"                                
                end
              end # if self.assigned_process.to_s.size > 0
            end # if self.division_name.to_s.size == 0
          end # if (parsed['status'] != "success") <-- #{BlackStack::Pampa::api_url}/api1.3/pampa/get.json
        end # if (parsed['status'] != "success") <-- #{BlackStack::Pampa::api_url}/api1.3/pampa/hello.json
  
        #  
        logger.logs "Sleep... "
        self.doSleep(start_time)
        logger.done
  
        logger.log "-------------------------------------------"
      
      rescue Interrupt => e
        logger.reset
        
        logger.log "Interrupt signal!"

        logger.logs "Kill process... "        
        if (pid!=nil)
          system("taskkill /im #{pid.to_s} /f /t >nul 2>&1")
        end
        logger.done
        
        logger.logs "Disconnect to Database... "
        begin
#          DB.disconnect()
          logger.done
        rescue => e
          logger.error(e)
        end
        
        logger.log "Process is out."
        exit(0)

      rescue => e
        begin
          logger.log "Unhandled exception: #{e.to_s}\r\n#{e.backtrace.join("\r\n").to_s}"
          logger.logs "Sleep #{self.minimum_enlapsed_seconds.to_s} seconds... "
          sleep(self.minimum_enlapsed_seconds)
          logger.done
        rescue => e
          puts "Fatal error: #{e.to_s}"
          print "Sleep #{self.minimum_enlapsed_seconds.to_s} seconds... "
          sleep(self.minimum_enlapsed_seconds)
          puts          
        end

      end # rescue
  
    end # while
  
end