Class: SPSClientM2M

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

Instance Method Summary collapse

Constructor Details

#initialize(rsc, reg, keywords, px_url, log: nil, sps_host: 'sps', sps_port: '59000', topic: '#', reload_keyword: /^reload$/) ⇒ SPSClientM2M



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/spsclient_m2m.rb', line 13

def initialize(rsc, reg, keywords, px_url, log: nil, \
          sps_host: 'sps', sps_port: '59000', topic: '#', 
               reload_keyword: /^reload$/)
        
  @rsc, @log, @topic = rsc, log, topic
  
  log.info 'SPSCLientM2M/initialize: active' if log
  
  @sps_address = "%s:%s" % [sps_host, sps_port]    
  @sps = SPSSub.new host: sps_host, port: sps_port, log: log
  
  px = Polyrex.new px_url

  log.info 'SPSCLientM2M/initialize: before @ste' if log
  @ste = SPSTriggerExecute.new keywords, reg, px, log: log
  log.info 'SPSCLientM2M/initialize: after @ste' if log
  @keywords, @reload_keyword, @reg = keywords, reload_keyword, reg

end

Instance Method Details

#runObject



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
# File 'lib/spsclient_m2m.rb', line 33

def run()
 
  log.info 'SPSCLientM2M/run: active' if log
  
  rsc, ste, keywords, reg, reload_keyword  = @rsc, @ste, @keywords, @reg, 
      @reload_keyword

  @sps.subscribe(topic: @topic) do |raw_message, topic|
    
    log.info 'SPSCLientM2M/run: received something' if log
    
    if reg and topic == 'system/clock' then
      reg.set_key 'hkey_services/spsclient_m2m/last_seen', 
          "#%s#" % Time.now.to_s
    end
    
    if raw_message.strip =~ reload_keyword then
      
      log.info 'SPSClientM2M/run: reloading' if log        
      ste = SPSTriggerExecute.new keywords, reg=nil, px=nil, log: log
      
    end
    
    if log then
      log.info "SPSClientM2M/run: received %s: %s" % [topic, raw_message]
    end
    
    a = ste.mae topic: topic, message: raw_message
    log.info 'SPSClientM2M/run: a: ' + a.inspect if log

    # obj is the DRb object, r is the result from find_match, 
    # a is the Dynarex lookup array

    if a.length > 0 then
      
      h = {
      
        rse: ->(x, rsc, params){
        
          job = x.shift[/\/\/job:(.*)/,1]                  
          package_path = x.shift 
          package = package_path[/([^\/]+)\.rsf$/,1]
          
          if log then
            log.info "SPSClientM2M/run: job: %s path: %s package: %s" % \
                       [job, package_path, package]
          end
                       
          rsc.run_job package, job, params, args=x, package_path: package_path
        }, 
        sps: ->(x, rsc, _){ @sps.notice x },
        ste: ->(x, rsc, _){ 
          log.info 'SPSClientM2M/run: before ste run' if log
          ste.run x 
        }
      }

    end

    EM.defer do          
      
      a.each do |type, x, params| 
        
        Thread.new do
          
          begin

            h[type].call x, rsc, params
          rescue
            
            err_msg = 'SPSClientM2M/run/error: ' + ($!).inspect              
            log ? log.debug(err_msg) :  puts(err_msg)

          end
          
        end # /thread
        
      end # /each
      
    end      
    
  end        
  
end