Module: OmfRc::ResourceProxy::ScheduledApplication

Includes:
OmfRc::ResourceProxyDSL, Util::CommonTools, Util::PlatformTools
Defined in:
lib/omf_rc/resource_proxy/scheduled_application.rb

Constant Summary collapse

MAX_PARAMETER_NUMBER =
1000
DEFAULT_MANDATORY_PARAMETER =
false

Instance Method Summary collapse

Instance Method Details

#after_initial_configuredObject



123
124
125
126
127
128
129
130
# File 'lib/omf_rc/resource_proxy/scheduled_application.rb', line 123

hook :after_initial_configured do |res|
  # if state was set to scheduled from the create we need
  # to make sure that this happens!
  if res.property.state.to_s.downcase.to_sym == :scheduled
    res.property.state = :unscheduled
    res.switch_to_scheduled
  end
end

#build_command_lineString

Build the command line, which will be used to start this app.

This command line will be of the form: “env -i VAR1=value1 … application_path parameterA valueA …”

The environment variables and the parameters in that command line are taken respectively from the ‘environments’ and ‘parameters’ properties of this Application Resource Proxy. If the ‘use_oml’ property is set, then add to the command line the necessary oml parameters.

Returns:

  • (String)

    the full command line



442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
# File 'lib/omf_rc/resource_proxy/scheduled_application.rb', line 442

work('build_command_line') do |res|
  # TODO is this necessary?
  #cmd_line = "env -i " # Start with a 'clean' environment
  cmd_line = ""
  res.property.environments.each do |e,v|
    val = v.kind_of?(String) ? "'#{v}'" : v
    cmd_line += "#{e.to_s.upcase}=#{val} "
  end
  cmd_line += res.property.binary_path + " "
  # Add command line parameter in their specified order if any
  sorted_parameters = res.property.parameters.sort_by {|k,v| v[:order] || -1}
  sorted_parameters.each do |param,att|
    needed = false
    needed = att[:mandatory] if res.boolean?(att[:mandatory])
    # For mandatory parameter without a value, take the default one
    val = (needed && att[:value].nil?) ? att[:default] : att[:value]
    # Finally add the parameter if is value/default is not nil
    unless val.nil?
      if att[:type] == "Boolean"
        # for Boolean param, only the command is printed if value==true
        cmd_line += "#{att[:cmd]} " if val == true
      else
        # for all other type of param, we print "cmd value"
        # with a user-provided prefix/suffix if defined
        cmd_line += "#{att[:cmd]} "
        cmd_line += att[:prefix].nil? ? "#{val}" : "#{att[:prefix]}#{val}"
        cmd_line += att[:suffix].nil? ? " " : "#{att[:suffix]} "
      end
    end
  end
  # Add OML parameters if required
  cmd_line = res.build_oml_config(cmd_line) if res.property.use_oml
  cmd_line
end

#build_oml_configString

Add the required OML parameter to the command line for this application

  • if the ‘oml_configfile’ property is set with a filename, then we use that file as the OML Configuration file. Thus we add the parameter “–oml-config filename” to this application’s command line

  • if the ‘oml’ property is set with a Hash holding an OML configuration, then we write turn it into OML’s XML configuration representation, write it to a temporary file, and add the parameter “–oml-config tmpfile” to this application’s command line. The OML configuration hash is based on the liboml2.conf man page, an example of which is:

    <omlc domain="my_experiment" id="my_source_id">
      <collect url="tcp://10.0.0.200">
        <stream mp="radiotap" interval="2">
          <filter field="sig_strength_dBm" />
          <filter field="noise_strength_dBm" />
          <filter field="power" />
        </stream>
        <stream mp="udp" samples="10">
          <filter field="udp_len" />
        </stream>
      </collect>
    </omlc>
    

The ‘oml_configfile’ case takes precedence over the ‘oml’ case above.

Regardless of which case is performed, we will always set the ‘–oml-log-level’ and ‘–oml-log-file’ parameter on the command line if the corresponsding ‘oml_logfile’ and ‘oml_loglevel’ properties are set for this application resource.

Parameters:

  • cmd (String)

    the String to which OML parameters will be added

Returns:

  • (String)

    the resulting command line



511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
# File 'lib/omf_rc/resource_proxy/scheduled_application.rb', line 511

work('build_oml_config') do |res, cmd|
  if !res.property.oml_configfile.nil?
    if File.exist?(res.property.oml_configfile)
      cmd += "--oml-config #{res.property.oml_configfile} "
    else
      res.log_inform_warn "OML enabled but OML config file does not exist"+
      "(file: '#{res.property.oml_configfile}')"
    end
  elsif !res.property.oml.collection.nil?
    o = res.property.oml
    ofile = "/tmp/#{res.uid}-#{Time.now.to_i}.xml"
    of = File.open(ofile,'w')
    oid = o.id.nil? ? res.property.parent_id : o.id
    of << "<omlc experiment='#{o.experiment}' id='#{oid}'>\n"
    o.collection.each do |c|
      of << "  <collect url='#{c.url}'>\n"
      c.streams.each do |m|
        # samples as precedence over interval
        s = ''
        s = "interval='#{m.interval}'" if m.interval
        s = "samples='#{m.samples}'" if m.samples
        of << "    <stream mp='#{m.mp}' #{s}>\n"
        unless m.filters.nil?
          m.filters.each do |f|
            line = "      <filter field='#{f.field}' "
            line += "operation='#{f.operation}' " unless f.operation.nil?
            line += "rename='#{f.rename}' " unless f.rename.nil?
            line += "/>\n"
            of << line
          end
        end
        of << "    </stream>\n"
      end
      of << "  </collect>\n"
    end
    of << "</omlc>\n"
    of.close
    cmd += "--oml-config #{ofile}"
  else
    res.log_inform_warn "OML enabled but no OML configuration was given"+
      "(file: '#{res.property.oml_configfile}' - "+
      "config: '#{res.property.oml.inspect}')"
  end
  cmd += "--oml-log-level #{res.property.oml_loglevel} " unless res.property.oml_loglevel.nil?
  cmd += "--oml-log-file #{res.property.oml_logfile} " unless res.property.oml_logfile.nil?
  cmd
end

#configure_parametersObject

Configure the parameters property of this Application RP

See Also:

  • Application


212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/omf_rc/resource_proxy/scheduled_application.rb', line 212

configure :parameters do |res, params|
  if params.kind_of? Hash
    params.each do |p,v|
      if v.kind_of? Hash
        # if this param has no set order, then assign the highest number to it
        # this will allow sorting the parameters later
        v[:order] = MAX_PARAMETER_NUMBER if v[:order].nil?
        # if this param has no set mandatory field, assign it a default one
        v[:mandatory] = DEFAULT_MANDATORY_PARAMETER if v[:mandatory].nil?
        new_val = res.property.parameters[p].nil? ? v : res.property.parameters[p].merge(v)
        # only set this new parameter if it passes the type check
        if res.pass_type_checking?(new_val)
          res.property.parameters[p] = new_val
          #res.dynamic_parameter_update(p,new_val)
        else
          res.log_inform_error "Configuration of parameter '#{p}' failed "+
            "type checking. Defined type is #{new_val[:type]} while assigned "+
            "value/default are #{new_val[:value].inspect} / "+
            "#{new_val[:default].inspect}"
        end
      else
        res.log_inform_error "Configuration of parameter '#{p}' failed!"+
          "Options not passed as Hash (#{v.inspect})"
      end
    end
  else
    res.log_inform_error "Parameter configuration failed! Parameters not "+
      "passed as Hash (#{params.inspect})"
  end
  res.property.parameters
end

#configure_stateObject

Configure the state of this Application RP. The valid states are unscheduled, scheduled, paused. The semantic of each states are:

  • unscheduled: the initial state for an Application RP

  • completed: the final state for an application RP. When the application has been executed and its execution is finished, it enters this state. When the application is completed it cannot change state again TODO: maybe in future OMF, we could consider allowing an app to be reset?

  • scheduled: upon entering in this state, a new instance of the application is started, the Application RP stays in this state until the application instance is finished or paused. The Application RP can only enter this state from a previous paused or unscheduled state.

  • paused: upon entering this state, the currently scheduled instance of this application should be paused (it is the responsibility of specialised Application Proxy to ensure that! This default Application Proxy does nothing to the application instance when entering this state). The Application RP can only enter this state from a previous scheduled state.

Parameters:

  • value (String)

    the state to set this app into



266
267
268
269
270
271
272
273
274
275
276
# File 'lib/omf_rc/resource_proxy/scheduled_application.rb', line 266

configure :state do |res, value|
  OmfCommon.eventloop.after(0) do
    case value.to_s.downcase.to_sym
    when :unscheduled then res.switch_to_unscheduled
    when :scheduled then res.switch_to_scheduled
    else
      res.log_inform_warn "Cannot switch application to unknown state '#{value.to_s}'!"
    end
  end
  res.property.state
end

#conifgure_environmentsObject

Configure the environments and oml property of this Application RP

See Also:

  • Application


194
195
196
197
198
199
200
201
202
203
204
# File 'lib/omf_rc/resource_proxy/scheduled_application.rb', line 194

%w(environments oml).each do |prop|
  configure(prop) do |res, value|
    if value.kind_of? Hash
      res.property[prop] = res.property[prop].merge(value)
    else
      res.log_inform_error "Configuration failed for '#{prop}'! "+
        "Value not passed as Hash (#{value.inspect})"
    end
    res.property[prop]
  end
end

#conifgure_omlObject

Configure the environments and oml property of this Application RP

See Also:

  • Application


194
195
196
197
198
199
200
201
202
203
204
# File 'lib/omf_rc/resource_proxy/scheduled_application.rb', line 194

%w(environments oml).each do |prop|
  configure(prop) do |res, value|
    if value.kind_of? Hash
      res.property[prop] = res.property[prop].merge(value)
    else
      res.log_inform_error "Configuration failed for '#{prop}'! "+
        "Value not passed as Hash (#{value.inspect})"
    end
    res.property[prop]
  end
end

#pass_type_checking?Boolean

Check if a configured value or default for a parameter has the same type as the type defined for that parameter The checking procedure is as follows:

  • first check if a type was set for this parameter, if not then return true (thus if no type was defined for this parameter then return true regardless of the type of the given value or default)

  • second check if a value is given, if so check if it has the same type as the defined type, if so then return true, if not then return false.

  • third if no value is given but a default is given, then perform the same check as above but using the default in-place of the value

Parameters:

  • att (Hash)

    the Hash holding the parameter’s attributs

Returns:

  • (Boolean)

    true or false



399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
# File 'lib/omf_rc/resource_proxy/scheduled_application.rb', line 399

work('pass_type_checking?') do |res,att|
  passed = false
  unless att[:type].nil?
    if att[:type] == 'Boolean' # HACK: as Ruby does not have a Boolean type
      if !att[:default].nil? && !att[:value].nil?
        passed = true if res.boolean?(att[:default]) && res.boolean?(att[:value])
      elsif att[:default].nil? && att[:value].nil?
        passed = true
      elsif att[:default].nil?
        passed = true if res.boolean?(att[:value])
      elsif att[:value].nil?
        passed = true if res.boolean?(att[:default])
      end
    else # Now for all other types...
      klass = Module.const_get(att[:type].capitalize.to_sym)
      if !att[:default].nil? && !att[:value].nil?
        passed = true if att[:default].kind_of?(klass) && att[:value].kind_of?(klass)
      elsif att[:default].nil? && att[:value].nil?
        passed = true
      elsif att[:default].nil?
        passed = true if att[:value].kind_of?(klass)
      elsif att[:value].nil?
        passed = true if att[:default].kind_of?(klass)
      end
    end
  else
    passed = true
  end
  passed
end

#process_event(res, event_type, app_id, msg) ⇒ Object

This method processes an event coming from the application instance, which was started by this Resource Proxy (RP). It is a callback, which is usually called by the ExecApp class in OMF

Parameters:

  • res (AbstractResource)

    this RP

  • event_type (String)

    the type of event from the app instance (STARTED, EXIT, STDOUT, STDERR)

  • app_id (String)

    the id of the app instance

  • msg (String)

    the message carried by the event



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
# File 'lib/omf_rc/resource_proxy/scheduled_application.rb', line 143

def process_event(res, event_type, app_id, msg)
    logger.info "App Event from '#{app_id}' "+
                "(##{res.property.event_sequence}) - "+
                "#{event_type}: '#{msg}'"
    res.property.event_sequence += 1
    if event_type == 'EXIT'
      res.property.state = :unscheduled
      res.inform(:status, {
                      status_type: 'APP_EVENT',
                      event: event_type.to_s.upcase,
                      app: app_id,
                      exit_code: msg,
                      msg: msg,
                      state: res.property.state,
                      seq: res.property.event_sequence,
                      uid: res.uid # do we really need this? Should be identical to 'src'
                    }, :ALL)
    else
      res.inform(:status, {
                    status_type: 'APP_EVENT',
                    event: event_type.to_s.upcase,
                    app: app_id,
                    msg: msg,
                    seq: res.property.event_sequence,
                    uid: res.uid
                  }, :ALL)
    end
end

#request_platformObject

Request the platform property of this Application RP

See Also:

  • Application


179
180
181
182
# File 'lib/omf_rc/resource_proxy/scheduled_application.rb', line 179

request :platform do |res|
  res.property.platform = detect_platform if res.property.platform.nil?
  res.property.platform.to_s
end

#switch_to_scheduledObject

Switch this Application RP into the ‘scheduled’ state (see the description of configure :state)



311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
# File 'lib/omf_rc/resource_proxy/scheduled_application.rb', line 311

work('switch_to_scheduled') do |res|
  if res.property.state == :unscheduled
    # start a new instance of this app
    res.property.app_id = res.hrn.nil? ? res.uid : res.hrn
    # we need at least a defined binary path to run an app...
    if res.property.binary_path.nil?
      res.log_inform_warn "Binary path not set! No Application to run!"
    elsif res.property.schedule.nil?
      res.log_inform_warn "No schedule given!"
    else
      # "in X" schedules job to run once, X minutes from now
      if res.property.schedule[0,3] == "in "
        delay = res.property.schedule.split(' ')[1].to_i * 60
        t = Time.now() + delay
        cron_schedule = t.strftime("%-M %-H %-d %-m *")
      else
        cron_schedule = res.property.schedule
      end
      Dir.mkdir(res.property.app_log_dir) if !Dir.exist?(res.property.app_log_dir)
      stderr_file = "#{res.property.app_log_dir}/#{res.property.app_id}.#{res.uid}.err.log"
      stdout_file = "#{res.property.app_log_dir}/#{res.property.app_id}.#{res.uid}.out.log"
      pid_file = "#{res.property.app_log_dir}/#{res.property.app_id}.#{res.uid}.pid.log"
      File.delete(stderr_file) if File.exist?(stderr_file)
      File.delete(stdout_file) if File.exist?(stdout_file)
      File.delete(pid_file) if File.exist?(pid_file)

      app_wrapper_path = File.expand_path("#{File.dirname(__FILE__)}/../../../bin/cronjob_app_wrapper")

      cmd = "#{app_wrapper_path} #{res.property.instrument_launch.domain} #{res.property.instrument_launch.url}"
      cmd = "#{cmd} #{stdout_file} #{stderr_file} #{pid_file} #{res.property.timeout} #{res.property.timeout_kill_signal} #{res.build_command_line}"
      cmd = "#{res.property.ruby_path} #{cmd}" if res.property.ruby_path

      info "Adding cron job for '#{res.property.app_id}' with schedule '#{cron_schedule}' and command '#{cmd}'"

      CronEdit::Crontab.Add res.property.app_id, "#{cron_schedule} #{cmd}"
      restart_cron = `/etc/init.d/cron restart &>/dev/null` # Vixie Cron on Angstrom requires restart!
      res.property.file_change_callback = Proc.new do |modified, added, removed|
        removed.each do |file|
          res.property.file_read_offset[file]=nil
        end
        files = added + modified
        files.each do |file|
          if file.include? stderr_file or file.include? stdout_file
            res.property.file_read_offset[file]=0 if res.property.file_read_offset[file].nil?
            data = IO.read(file,nil,res.property.file_read_offset[file])
            res.property.file_read_offset[file]+=data.length
            data.split(/\r?\n/).each do |line|
              event_type = "STDOUT"
              if file.include? ".err.log"
                event_type = "STDERR"
                if line.include? "exited with status:"
                  if line.split(":").last.to_i == 0
                    event_type = "DONE.OK"
                  else
                    event_type = "DONE.ERROR"
                  end
                end
                event_type = "DONE.OK" if line.include? "killed by signal:"
              end
              res.process_event(res, event_type, res.property.app_id, line)
            end
          end
        end
      end
      res.property.file_change_listener = Listen.to(res.property.app_log_dir).change(&res.property.file_change_callback)
      res.property.file_change_listener.start
      res.property.state = :scheduled
    end
  else
    res.log_inform_warn "Cannot switch to scheduled state as current state is '#{res.property.state}'"
  end
end

#switch_to_unscheduledObject

Switch this Application RP into the ‘unscheduled’ state (see the description of configure :state)



287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# File 'lib/omf_rc/resource_proxy/scheduled_application.rb', line 287

work('switch_to_unscheduled') do |res|
  if res.property.state == :scheduled
    info "Removing cron job for '#{res.property.app_id}'"
    CronEdit::Crontab.Remove res.property.app_id
    restart_cron = `/etc/init.d/cron restart &>/dev/null` # Vixie Cron on Angstrom requires restart!
    pid_file = "#{res.property.app_log_dir}/#{res.property.app_id}.#{res.uid}.pid.log"
    begin
      File.readlines(pid_file).each do |line|
        Process.kill(res.property.timeout_kill_signal, line.to_i)
        info "Killing process #{line.to_i}"
      end
    rescue Exception => e
      warn "Could not kill scheduled app #{res.property.app_id}.#{res.uid}"
    end
    res.property.file_change_listener.stop
    res.property.state = :unscheduled
  end
end