Class: RQ::MainHelper

Inherits:
Object
  • Object
show all
Includes:
Logging, Util
Defined in:
lib/rq-3.0.0/mainhelper.rb

Overview

the MainHelper class abstracts some of the common functions the various Main delegates require

Constant Summary

Constants included from Logging

Logging::DIV0, Logging::DIV1, Logging::DIV2, Logging::DIV3, Logging::EOL, Logging::SEC0, Logging::SEC1, Logging::SEC2, Logging::SEC3

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

append_features

Methods included from Logging::LogMethods

#debug, #error, #fatal, #info, #logerr, #logger, #logger=, #warn

Methods included from Util

#alive?, append_features, #btrace, #columnize, #defval, #emsg, #erreq, #errmsg, #escape, #escape!, #exec, export, #fork, #getopt, #hashify, #hms, #host, #hostname, #klass, #maim, #mcp, #realpath, #stamptime, #system, #timestamp, #tmpnam, #uncache, #which_ruby

Constructor Details

#initialize(main) ⇒ MainHelper

Returns a new instance of MainHelper.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/rq-3.0.0/mainhelper.rb', line 37

def initialize main
#--{{{
  @main = main
  @logger = main.logger
  @argv = main.argv
  @env = main.env
  @stdin = main.stdin
  @job_stdin = main.job_stdin
  @cmd = main.cmd
  @options = main.options
  @qpath = main.qpath
  @mode = main.mode
  @quiet = main.quiet
  @fields = main.fields
  @dot_rq_dir = main.dot_rq_dir
  @q = nil 
#--}}}
end

Instance Attribute Details

#argvObject (readonly)

Returns the value of attribute argv.



21
22
23
# File 'lib/rq-3.0.0/mainhelper.rb', line 21

def argv
  @argv
end

#cmdObject (readonly)

Returns the value of attribute cmd.



25
26
27
# File 'lib/rq-3.0.0/mainhelper.rb', line 25

def cmd
  @cmd
end

#dot_rq_dirObject (readonly)

Returns the value of attribute dot_rq_dir.



32
33
34
# File 'lib/rq-3.0.0/mainhelper.rb', line 32

def dot_rq_dir
  @dot_rq_dir
end

#envObject (readonly)

Returns the value of attribute env.



22
23
24
# File 'lib/rq-3.0.0/mainhelper.rb', line 22

def env
  @env
end

#fieldsObject (readonly)

Returns the value of attribute fields.



31
32
33
# File 'lib/rq-3.0.0/mainhelper.rb', line 31

def fields
  @fields
end

#job_stdinObject (readonly) Also known as: job_stdin?

Returns the value of attribute job_stdin.



24
25
26
# File 'lib/rq-3.0.0/mainhelper.rb', line 24

def job_stdin
  @job_stdin
end

#mainObject (readonly)

Returns the value of attribute main.



20
21
22
# File 'lib/rq-3.0.0/mainhelper.rb', line 20

def main
  @main
end

#modeObject (readonly)

Returns the value of attribute mode.



28
29
30
# File 'lib/rq-3.0.0/mainhelper.rb', line 28

def mode
  @mode
end

#optionsObject (readonly)

Returns the value of attribute options.



26
27
28
# File 'lib/rq-3.0.0/mainhelper.rb', line 26

def options
  @options
end

#qObject (readonly)

Returns the value of attribute q.



30
31
32
# File 'lib/rq-3.0.0/mainhelper.rb', line 30

def q
  @q
end

#qpathObject (readonly)

Returns the value of attribute qpath.



27
28
29
# File 'lib/rq-3.0.0/mainhelper.rb', line 27

def qpath
  @qpath
end

#quietObject (readonly) Also known as: quiet?

Returns the value of attribute quiet.



29
30
31
# File 'lib/rq-3.0.0/mainhelper.rb', line 29

def quiet
  @quiet
end

#stdinObject (readonly) Also known as: stdin?

Returns the value of attribute stdin.



23
24
25
# File 'lib/rq-3.0.0/mainhelper.rb', line 23

def stdin
  @stdin
end

Instance Method Details

#dumping_yaml_tuplesObject

–}}}



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/rq-3.0.0/mainhelper.rb', line 132

def dumping_yaml_tuples
#--{{{
  fields = nil
  dump = lambda do |tuple|
    puts '---'
    if fields.nil?
      if @fields
        fields = field_match @fields, tuple.fields
      else
        fields = tuple.fields
      end
    end
    dump = lambda do |tuple|
      puts '-'
      fields.each{|f| puts " #{ f }: #{ tuple[ f ] }"}
    end
    dump[tuple]
  end
  lambda{|tuple| dump[tuple]}
#--}}}
end

#field_match(srclist, dstlist) ⇒ Object

–}}}



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/rq-3.0.0/mainhelper.rb', line 153

def field_match srclist, dstlist
#--{{{
  fields = dstlist.select do |dst|
    srclist.map do |src|
      re =
        if src =~ %r/^[a-zA-Z0-9_-]+$/
          %r/^#{ src }/i
        else
          %r/#{ src }/i
        end
      src == dst or dst =~ re
    end.any?
  end.uniq
#--}}}
end

#init_job_stdin!Object

–}}}



168
169
170
171
172
173
174
175
176
177
178
# File 'lib/rq-3.0.0/mainhelper.rb', line 168

def init_job_stdin!
#--{{{
  if @job_stdin == '-'
    tmp = Tempfile::new "#{ Process::pid  }_#{ rand 42 }"
    while((buf = STDIN.read(8192))); tmp.write buf; end
    tmp.close
    @job_stdin = tmp.path 
  end
  @job_stdin
#--}}}
end

#loadio(io, path, jobs) ⇒ Object

–}}}



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
# File 'lib/rq-3.0.0/mainhelper.rb', line 67

def loadio io, path, jobs
#--{{{
  while((line = io.gets))
    if line =~ %r/^---\s*$/o
      loaded = YAML::load io 
      raise "no jobs in <#{ path }>" unless 
        Array === loaded and 
        Hash === loaded.first and Hash === loaded.last
      loaded.each{|job| jobs << job}
      loaded = nil
    else
      # line.gsub!(%r/(?:^\s+)|(?:\s+$)|(?:#.*$)/o, '')
      line.strip!
      next if line.empty?
      job = Job::new
      if((m =  %r/^\s*(?:jid\s*=\s*)?(\d+)\s*$/io.match(line)))
        job['jid'] = Integer(m[1])
      else
        job['command'] = line
      end
      jobs << job
    end
  end
#--}}}
end

#loadyaml(io, path, jobs) ⇒ Object

–}}}



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/rq-3.0.0/mainhelper.rb', line 112

def loadyaml io, path, jobs
#--{{{
  h = nil
  while((line = io.gets))
    line.strip!
    next if line.empty?
    case line
      when %r/^\s*-\s*$/
        jobs << h if h
        h = {}
      else
        k, v = line.split %r/:/, 2
        k.strip!
        v.strip!
        h[k] = v
    end
  end
  jobs << h if h
#--}}}
end

#set_qObject

–}}}



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/rq-3.0.0/mainhelper.rb', line 55

def set_q
#--{{{
  raise "q <#{ @qpath }> does not exist" unless test ?d, @qpath
  @q = JobQueue::new @qpath, 'logger' => @logger
  if @options['snapshot']
    ss = "#{ $0 }_#{ Process::pid }_#{ Thread::current.object_id.abs }_#{ rand Time::now.to_i  }".gsub(%r|/|o,'_')
    qtmp = File::join Dir::tmpdir, ss
    @q = @q.snapshot qtmp, @options['retries']
    at_exit{ FileUtils::rm_rf qtmp }
  end
#--}}}
end