Class: JobLog

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

Instance Method Summary collapse

Constructor Details

#initialize(dxfilepath = 'joblog.xml', host: 'sps2', port: 59100) ⇒ JobLog

Returns a new instance of JobLog.



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/job_log.rb', line 11

def initialize(dxfilepath='joblog.xml', host: 'sps2', port: 59100)

  @dxfilepath = dxfilepath
  @sps = SPSSub.new host: host, port: port    
  
  @dx = if File.exists? dxfilepath then      
    Dynarex.new(dxfilepath)
  else
    Dynarex.new 'jobs/job(title, desc, started, ended, status, tags)'
  end
end

Instance Method Details

#listenObject



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

def listen

  topics = %w(started done aborted).map {|x| "log/+/#{x}/+"}.join(' | ')
  @sps.subscribe(topic: topics) do |msg, topic|

    title = topic[/(?<=log\/)\w+/]

    case topic
    when /\/started\//

      desc, raw_tags = msg.split(/(?= +#)/,2)
      tags = raw_tags.scan(/(?<=#)\w+/).join ' '
      @dx.create title: title, started: Time.now.to_s, desc: desc, tags: tags

    when /\/done\//

      r = @dx.find_by_title title
      r.ended = Time.now.to_s if r
      r.status = 'success'

    when /\/aborted\//

      r = @dx.find_by_title title
      r.ended = Time.now.to_s if r
      r.status = msg
    end

    # write the entry to file
    @dx.save @dxfilepath
  end
end