rq 0.1.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,143 @@
1
+ #!/usr/bin/env ruby
2
+ require 'rbconfig'
3
+ require 'find'
4
+ require 'ftools'
5
+ require 'tempfile'
6
+ include Config
7
+
8
+ LIBDIR = "lib"
9
+ LIBDIR_MODE = 0644
10
+
11
+ BINDIR = "bin"
12
+ BINDIR_MODE = 0755
13
+
14
+
15
+ $srcdir = CONFIG["srcdir"]
16
+ $version = CONFIG["MAJOR"]+"."+CONFIG["MINOR"]
17
+ $libdir = File.join(CONFIG["libdir"], "ruby", $version)
18
+ $archdir = File.join($libdir, CONFIG["arch"])
19
+ $site_libdir = $:.find {|x| x =~ /site_ruby$/}
20
+ $bindir = CONFIG["bindir"]
21
+ $ruby_install_name = CONFIG['ruby_install_name'] || CONFIG['RUBY_INSTALL_NAME']
22
+ $ruby = File.join($bindir, $ruby_install_name || 'ruby')
23
+
24
+ if !$site_libdir
25
+ $site_libdir = File.join($libdir, "site_ruby")
26
+ elsif $site_libdir !~ %r/#{Regexp.quote($version)}/
27
+ $site_libdir = File.join($site_libdir, $version)
28
+ end
29
+
30
+ def install_rb(srcdir=nil, destdir=nil, mode=nil, bin=nil)
31
+ #{{{
32
+ path = []
33
+ dir = []
34
+ Find.find(srcdir) do |f|
35
+ next unless FileTest.file?(f)
36
+ next if (f = f[srcdir.length+1..-1]) == nil
37
+ next if (/CVS$/ =~ File.dirname(f))
38
+ path.push f
39
+ dir |= [File.dirname(f)]
40
+ end
41
+ for f in dir
42
+ next if f == "."
43
+ next if f == "CVS"
44
+ File::makedirs(File.join(destdir, f))
45
+ end
46
+ for f in path
47
+ next if (/\~$/ =~ f)
48
+ next if (/^\./ =~ File.basename(f))
49
+ unless bin
50
+ File::install(File.join(srcdir, f), File.join(destdir, f), mode, true)
51
+ else
52
+ from = File.join(srcdir, f)
53
+ to = File.join(destdir, f)
54
+ shebangify(from) do |sf|
55
+ $deferr.print from, " -> ", File::catname(from, to), "\n"
56
+ $deferr.printf "chmod %04o %s\n", mode, to
57
+ File::install(sf, to, mode, false)
58
+ end
59
+ end
60
+ end
61
+ #}}}
62
+ end
63
+ def shebangify f
64
+ #{{{
65
+ open(f) do |fd|
66
+ buf = fd.read 42
67
+ if buf =~ %r/^\s*#\s*!.*ruby/o
68
+ ftmp = Tempfile::new("#{ $$ }_#{ File::basename(f) }")
69
+ begin
70
+ fd.rewind
71
+ ftmp.puts "#!#{ $ruby }"
72
+ while((buf = fd.read(8192)))
73
+ ftmp.write buf
74
+ end
75
+ ftmp.close
76
+ yield ftmp.path
77
+ ensure
78
+ ftmp.close!
79
+ end
80
+ else
81
+ yield f
82
+ end
83
+ end
84
+ #}}}
85
+ end
86
+ def ARGV.switch
87
+ #{{{
88
+ return nil if self.empty?
89
+ arg = self.shift
90
+ return nil if arg == '--'
91
+ if arg =~ /^-(.)(.*)/
92
+ return arg if $1 == '-'
93
+ raise 'unknown switch "-"' if $2.index('-')
94
+ self.unshift "-#{$2}" if $2.size > 0
95
+ "-#{$1}"
96
+ else
97
+ self.unshift arg
98
+ nil
99
+ end
100
+ #}}}
101
+ end
102
+ def ARGV.req_arg
103
+ #{{{
104
+ self.shift || raise('missing argument')
105
+ #}}}
106
+ end
107
+
108
+
109
+ #
110
+ # main program
111
+ #
112
+
113
+ libdir = $site_libdir
114
+ bindir = $bindir
115
+
116
+ begin
117
+ while switch = ARGV.switch
118
+ case switch
119
+ when '-d', '--destdir'
120
+ libdir = ARGV.req_arg
121
+ when '-l', '--libdir'
122
+ libdir = ARGV.req_arg
123
+ when '-b', '--bindir'
124
+ bindir = ARGV.req_arg
125
+ when '-r', '--ruby'
126
+ $ruby = ARGV.req_arg
127
+ else
128
+ raise "unknown switch #{switch.dump}"
129
+ end
130
+ end
131
+ rescue
132
+ STDERR.puts $!.to_s
133
+ STDERR.puts File.basename($0) +
134
+ " -d <destdir>" +
135
+ " -l <libdir>" +
136
+ " -b <bindir>"
137
+ " -r <ruby>"
138
+ exit 1
139
+ end
140
+
141
+ install_rb(LIBDIR, libdir, LIBDIR_MODE)
142
+ install_rb(BINDIR, bindir, BINDIR_MODE, bin=true)
143
+
@@ -0,0 +1,82 @@
1
+ unless defined? $__rq__
2
+ module RQ
3
+ #{{{
4
+ AUTHOR = 'ara.t.howard@noaa.gov'
5
+ LIBNAME = 'rq'
6
+ VERSION = '0.1.7'
7
+ LIBVER = "#{ LIBNAME }-#{ VERSION }"
8
+ DIRNAME = File::dirname(File::expand_path(__FILE__)) + File::SEPARATOR
9
+ ROOTDIR = File::dirname(DIRNAME)
10
+ LIBDIR = File::join(DIRNAME, LIBVER) + File::SEPARATOR
11
+ EXIT_SUCCESS = 0
12
+ EXIT_FAILURE = 1
13
+ #
14
+ # builtin
15
+ #
16
+ require 'optparse'
17
+ require 'logger'
18
+ require 'socket'
19
+ require 'rbconfig'
20
+ require 'optparse'
21
+ require 'logger'
22
+ require 'yaml'
23
+ require 'pp'
24
+ require 'socket'
25
+ require 'pathname'
26
+ require 'tempfile'
27
+ require 'fileutils'
28
+ require 'tmpdir'
29
+ require 'drb/drb'
30
+ #
31
+ # depends - http://raa.ruby-lang.org
32
+ #
33
+ begin
34
+ require 'arrayfields'
35
+ rescue LoadError
36
+ abort "require arrayfields - http://raa.ruby-lang.org/project/arrayfields/"
37
+ end
38
+ begin
39
+ require 'sqlite'
40
+ rescue LoadError
41
+ abort "require sqlite - http://raa.ruby-lang.org/project/sqlite-ruby/"
42
+ end
43
+ begin
44
+ require 'posixlock'
45
+ rescue LoadError
46
+ abort "require posixlock - http://raa.ruby-lang.org/project/posixlock/"
47
+ end
48
+ begin
49
+ require 'lockfile'
50
+ rescue LoadError
51
+ abort "require lockfile - http://raa.ruby-lang.org/project/lockfile/"
52
+ end
53
+ #
54
+ # rq support libs
55
+ #
56
+ require LIBDIR + 'util'
57
+ require LIBDIR + 'logging'
58
+ require LIBDIR + 'sleepcycle'
59
+ require LIBDIR + 'qdb'
60
+ require LIBDIR + 'jobqueue'
61
+ require LIBDIR + 'job'
62
+ require LIBDIR + 'jobrunner'
63
+ require LIBDIR + 'jobrunnerdaemon'
64
+ require LIBDIR + 'usage'
65
+ require LIBDIR + 'mainhelper'
66
+ require LIBDIR + 'creator'
67
+ require LIBDIR + 'submitter'
68
+ require LIBDIR + 'lister'
69
+ require LIBDIR + 'statuslister'
70
+ require LIBDIR + 'deleter'
71
+ require LIBDIR + 'updater'
72
+ require LIBDIR + 'querier'
73
+ require LIBDIR + 'executor'
74
+ require LIBDIR + 'configurator'
75
+ require LIBDIR + 'snapshotter'
76
+ require LIBDIR + 'locker'
77
+ require LIBDIR + 'backer'
78
+ require LIBDIR + 'feeder'
79
+ #}}}
80
+ end # module rq
81
+ $__rq__ = __FILE__
82
+ end
@@ -0,0 +1,27 @@
1
+ unless defined? $__rq_backer__
2
+ module RQ
3
+ #{{{
4
+ LIBDIR = File::dirname(File::expand_path(__FILE__)) + File::SEPARATOR unless
5
+ defined? LIBDIR
6
+
7
+ require LIBDIR + 'mainhelper'
8
+
9
+ class Backer < MainHelper
10
+ #{{{
11
+ def backup
12
+ #{{{
13
+ set_q
14
+ bak = @argv.shift
15
+ bak ||= "#{ File::basename(@qpath) }.#{ Util::timestamp.gsub(/[:\s\.-]/,'_') }.bak"
16
+ raise "<#{ bak }> exists" if bak and test(?e, bak)
17
+ debug{ "bak <#{ bak }>" }
18
+ @q.lock{ FileUtils::cp_r @qpath, bak }
19
+ info{ "created backup <#{ bak }>" }
20
+ #}}}
21
+ end
22
+ #}}}
23
+ end # class Backer
24
+ #}}}
25
+ end # module RQ
26
+ $__rq_backer__ = __FILE__
27
+ end
@@ -0,0 +1,78 @@
1
+ unless defined? $__rq_configfile__
2
+ module RQ
3
+ #{{{
4
+ LIBDIR = File::dirname(File::expand_path(__FILE__)) + File::SEPARATOR unless
5
+ defined? LIBDIR
6
+
7
+ require 'yaml'
8
+
9
+ class ConfigFile
10
+ #{{{
11
+ DEFAULT_CONFIG = LIBDIR + 'defaultconfig.txt'
12
+
13
+ class << self
14
+ def gen_template(arg = nil)
15
+ #{{{
16
+ @data ||= IO::read(DEFAULT_CONFIG)
17
+ case arg
18
+ when IO
19
+ arg.write @data
20
+ when String
21
+ open(arg, 'w'){|f| f.write @data}
22
+ else
23
+ STDOUT.write @data
24
+ end
25
+ self
26
+ #}}}
27
+ end
28
+ def load_default
29
+ #{{{
30
+ @data ||= IO::read(DEFAULT_CONFIG)
31
+ @default ||= YAML::load(munge(@data)) || {}
32
+ #}}}
33
+ end
34
+ def any(basename, *dirnames)
35
+ #{{{
36
+ config = nil
37
+ dirnames.each do |dirname|
38
+ path = File::join dirname, basename
39
+ if test ?e, path
40
+ config = self::new(path)
41
+ break
42
+ end
43
+ end
44
+ config || self::new('default')
45
+ #}}}
46
+ end
47
+ def munge buf
48
+ #{{{
49
+ buf.gsub(%r/\t/o,' ')
50
+ #}}}
51
+ end
52
+ end
53
+ attr :path
54
+ def initialize path
55
+ #{{{
56
+ @path = nil
57
+ yaml = nil
58
+ if path.nil? or path and path =~ /^\s*default/io
59
+ yaml = self.class.load_default
60
+ @path = 'DEFAULT'
61
+ else path
62
+ yaml = YAML::load(self.class.munge(open(path).read))
63
+ @path = path
64
+ end
65
+ self.update yaml
66
+ #}}}
67
+ end
68
+ def to_hash
69
+ #{{{
70
+ {}.update self
71
+ #}}}
72
+ end
73
+ #}}}
74
+ end # class ConfigFile
75
+ #}}}
76
+ end # module RQ
77
+ $__rq_configfile__ = __FILE__
78
+ end
@@ -0,0 +1,36 @@
1
+ unless defined? $__rq_configurator__
2
+ module RQ
3
+ #{{{
4
+ LIBDIR = File::dirname(File::expand_path(__FILE__)) + File::SEPARATOR unless
5
+ defined? LIBDIR
6
+
7
+ require LIBDIR + 'mainhelper'
8
+
9
+ class Configurator < MainHelper
10
+ #{{{
11
+ #}}}
12
+ def configure
13
+ #{{{
14
+ set_q
15
+ attributes = {}
16
+ unless @argv.empty?
17
+ kv_pat = %r/^\s*([^\s]+)\s*=+\s*([^\s]+)\s*$/o
18
+ @q.transaction do
19
+ @argv.each do |arg|
20
+ match = kv_pat.match arg
21
+ if match
22
+ k, v = match[1], match[2]
23
+ @q[k] = v
24
+ end
25
+ end
26
+ attributes = @q.attributes
27
+ end
28
+ end
29
+ y attributes
30
+ #}}}
31
+ end
32
+ end # class Configurator
33
+ #}}}
34
+ end # module RQ
35
+ $__rq_configurator__ = __FILE__
36
+ end
@@ -0,0 +1,23 @@
1
+ unless defined? $__rq_creator__
2
+ module RQ
3
+ #{{{
4
+ LIBDIR = File::dirname(File::expand_path(__FILE__)) + File::SEPARATOR unless
5
+ defined? LIBDIR
6
+
7
+ require LIBDIR + 'mainhelper'
8
+
9
+ class Creator < MainHelper
10
+ #{{{
11
+ def create
12
+ #{{{
13
+ raise "q <#{ @qpath }> exists!" if test ?e, @qpath
14
+ @q = JobQueue::create @qpath, 'logger' => @logger
15
+ info{ "created <#{ @q.path }>" }
16
+ #}}}
17
+ end
18
+ #}}}
19
+ end # class Creator
20
+ #}}}
21
+ end # module RQ
22
+ $__rq_creator__ = __FILE__
23
+ end
@@ -0,0 +1,5 @@
1
+ #
2
+ # rq config
3
+ #
4
+
5
+ key : value
@@ -0,0 +1,39 @@
1
+ unless defined? $__rq_deleter__
2
+ module RQ
3
+ #{{{
4
+ LIBDIR = File::dirname(File::expand_path(__FILE__)) + File::SEPARATOR unless
5
+ defined? LIBDIR
6
+
7
+ require LIBDIR + 'mainhelper'
8
+
9
+ class Deleter < MainHelper
10
+ #{{{
11
+ def delete
12
+ #{{{
13
+ set_q
14
+ jids = @argv
15
+ if jids.empty? or not STDIN.tty?
16
+ pat = %r/^(?:\s*jid\s*:)?\s*(\d+)\s*$/io
17
+ while((line = STDIN.gets))
18
+ match = pat.match line
19
+ next unless match
20
+ jids << Integer(match[1])
21
+ end
22
+ end
23
+ jids.map!{|jid| jid =~ %r/^\s*\d+\s*$/o ? Integer(jid) : jid}
24
+ raise "no jids" if jids.empty?
25
+ if @options['quiet']
26
+ @q.delete(*jids)
27
+ else
28
+ @q.delete(*jids)
29
+ puts '---'
30
+ jids.each{|jid| puts "- #{ jid }"}
31
+ end
32
+ #}}}
33
+ end
34
+ #}}}
35
+ end # class Deleter
36
+ #}}}
37
+ end # module RQ
38
+ $__rq_deleter__ = __FILE__
39
+ end
@@ -0,0 +1,41 @@
1
+ unless defined? $__rq_executor__
2
+ module RQ
3
+ #{{{
4
+ LIBDIR = File::dirname(File::expand_path(__FILE__)) + File::SEPARATOR unless
5
+ defined? LIBDIR
6
+
7
+ require LIBDIR + 'mainhelper'
8
+
9
+ class Executor < MainHelper
10
+ #{{{
11
+ def execute
12
+ #{{{
13
+ set_q
14
+ sql = @argv.join ' '
15
+ if sql.empty? or not STDIN.tty?
16
+ debug{ "reading sql from STDIN" }
17
+ while((buf = STDIN.gets))
18
+ buf.strip!
19
+ buf.gsub! %r/#.*$/o, ''
20
+ next if buf.empty?
21
+ sql << "#{ buf } "
22
+ end
23
+ end
24
+ @q.qdb.transaction_retries = 0
25
+ tuples = @q.transaction{@q.execute sql}
26
+ puts '---'
27
+ fields = nil
28
+ tuples.each do |tuple|
29
+ puts '-'
30
+ fields ||= tuple.fields
31
+ fields.each{|f| puts "#{ f } : #{ tuple[f] }"}
32
+ end
33
+ tuples
34
+ #}}}
35
+ end
36
+ #}}}
37
+ end # class Executor
38
+ #}}}
39
+ end # module RQ
40
+ $__rq_executor__ = __FILE__
41
+ end