Class: RSFServices

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

Defined Under Namespace

Classes: Package, PackageMethod

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(reg = nil, package_basepath: '', log: nil, debug: false, app_rsf: nil) ⇒ RSFServices

Returns a new instance of RSFServices.



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

def initialize(reg=nil, package_basepath: '', log: nil, debug: false, 
               app_rsf: nil)
  
  @log, @debug = log, debug
  
  puts 'inside RSF_services' if @debug

  super(log: log, debug: debug)

  @package_basepath, @services = package_basepath, {}
  
  @initialized = {}

  if reg then

    @registry = @services['registry'] = if reg.is_a? String then
      reg = DWSRegistry.new reg_path
    else
      reg
    end
    
    # load the system/startup RSF jobs

    startup = reg.get_key('system/startup')
    
    jobs =  startup.xpath('*[load="1"]').inject({}) do |r, job|
      settings = reg.get_keys("system/packages/#{job.name}/*")\
                        .inject({}){|r,x| r.merge(x.name.to_sym => x.text) }
      r.merge(job.name => settings)
    end
    
    jobs.each do |package, settings| 
      r = run_job(package.to_s, 'load', settings, 
                                              package_path: settings[:url])
    end
  end
  
  @app = AppMgr.new(rsf: app_rsf, reg: reg, rsc: self) if app_rsf

end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object (private)



210
211
212
213
214
215
# File 'lib/rsf_services.rb', line 210

def method_missing(method_name, *args)
  
  puts 'inside method_missing' if @debug
  self.type = :get
  Package.new self, method_name.to_s, debug: @debug
end

Instance Attribute Details

#package_basepathObject (readonly)

Returns the value of attribute package_basepath.



83
84
85
# File 'lib/rsf_services.rb', line 83

def package_basepath
  @package_basepath
end

#registryObject (readonly)

Returns the value of attribute registry.



83
84
85
# File 'lib/rsf_services.rb', line 83

def registry
  @registry
end

#servicesObject (readonly)

Returns the value of attribute services.



83
84
85
# File 'lib/rsf_services.rb', line 83

def services
  @services
end

Instance Method Details

#deleteObject



127
128
129
# File 'lib/rsf_services.rb', line 127

def delete()
  PackageMethod.new self, type: :delete
end

#getObject



131
132
133
# File 'lib/rsf_services.rb', line 131

def get()
  PackageMethod.new self
end

#package_methods(package) ⇒ Object



200
201
202
203
204
205
206
# File 'lib/rsf_services.rb', line 200

def package_methods(package)
  
  url = File.join(@package_basepath, package + '.rsf')    
  doc = Rexle.new open(url, 'UserAgent' => 'ClientRscript').read    
  doc.root.xpath('job/attribute::id').map {|x| x.value.to_s.gsub('-','_') }

end

#postObject



139
140
141
# File 'lib/rsf_services.rb', line 139

def post()
  PackageMethod.new self, type: :post
end

#putObject



135
136
137
# File 'lib/rsf_services.rb', line 135

def put()
  PackageMethod.new self, type: :put
end

#run_job(package, jobs, params = {}, *qargs) {|params| ... } ⇒ Object

Yields:

  • (params)


143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
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
# File 'lib/rsf_services.rb', line 143

def run_job(package, jobs, params={}, *qargs)
  
  puts 'inside run_job' #if @debug

  package_path = "%s/%s.rsf" % [@package_basepath, package]
                 
  if @log or @debug then
    msg = 'RSFServices/run job: ' + 
        ("package: %s jobs: %s params: %s qargs: %s " + \
          " package_path: %s" % [package, jobs, params, qargs, package_path])
    @log.info msg if @log
    puts msg if @debug
  end
  
  yield(params) if block_given?    
  
  a = [package_path, jobs.split(/\s/).map{|x| "//job:%s" % x} ,qargs].flatten(2)
  
  puts 'a: ' + a.inspect if @debug

  c, args, _ = read(a)
  puts 'c: ' + c.inspect if @debug
  rws, reg, app = self, @registry, @app

  begin
    
    @log.info 'RSFServices/run job: code: ' + c if @log and c.is_a? String

    # if there is a job id called *initialize* then execute if it hasn't 
    # already been executed

    if self.jobs(package_path).include? :initialize and 
        !@initialized[package_path]  and jobs != 'initialize' then
      run_job(package, 'initialize')
      @initialized[package_path] = true
    end

    r = eval c
    puts 'r: ' + r.inspect if @debug
    #thread = Thread.new {Thread.current[:v] = eval c}
    #thread.join
    #r = thread[:v] 
    
    @log.info 'RSFServices/run job: result: ' + r if @log and r.is_a? String
    
    return r

  rescue Exception => e  
    
    params = {}
    err_label = e.message.to_s + " :: \n" + e.backtrace.join("\n")            
    @log.debug 'RSFServices/run_job/error: ' + err_label if @log

  end

end