Class: RQ::MainHelper

Inherits:
Object
  • Object
show all
Includes:
Logging, Util
Defined in:
lib/rq/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.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/rq/mainhelper.rb', line 39

def initialize main
#--{{{
  @main = main
  @logger = main.logger
  @argv = main.argv
  @env = main.env
  @program = main.program
  @stdin = main.stdin
  @data = main.data
  @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
  @loops = main.loops
  @q = nil 
#--}}}
end

Instance Attribute Details

#argvObject (readonly)

Returns the value of attribute argv.



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

def argv
  @argv
end

#cmdObject (readonly)

Returns the value of attribute cmd.



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

def cmd
  @cmd
end

#dot_rq_dirObject (readonly)

Returns the value of attribute dot_rq_dir.



33
34
35
# File 'lib/rq/mainhelper.rb', line 33

def dot_rq_dir
  @dot_rq_dir
end

#envObject (readonly)

Returns the value of attribute env.



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

def env
  @env
end

#fieldsObject (readonly)

Returns the value of attribute fields.



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

def fields
  @fields
end

#job_stdinObject (readonly) Also known as: job_stdin?

Returns the value of attribute job_stdin.



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

def job_stdin
  @job_stdin
end

#loopsObject (readonly)

Returns the value of attribute loops.



34
35
36
# File 'lib/rq/mainhelper.rb', line 34

def loops
  @loops
end

#mainObject (readonly)

Returns the value of attribute main.



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

def main
  @main
end

#modeObject (readonly)

Returns the value of attribute mode.



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

def mode
  @mode
end

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

#programObject (readonly)

Returns the value of attribute program.



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

def program
  @program
end

#qObject (readonly)

Returns the value of attribute q.



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

def q
  @q
end

#qpathObject (readonly)

Returns the value of attribute qpath.



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

def qpath
  @qpath
end

#quietObject (readonly) Also known as: quiet?

Returns the value of attribute quiet.



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

def quiet
  @quiet
end

#stdinObject (readonly) Also known as: stdin?

Returns the value of attribute stdin.



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

def stdin
  @stdin
end

Instance Method Details

#dumping_yaml_tuplesObject

–}}}



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/rq/mainhelper.rb', line 137

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

–}}}



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/rq/mainhelper.rb', line 158

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

–}}}



173
174
175
176
177
178
179
180
181
182
183
# File 'lib/rq/mainhelper.rb', line 173

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

–}}}



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

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

–}}}



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/rq/mainhelper.rb', line 117

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

–}}}



60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/rq/mainhelper.rb', line 60

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