averell23-bj 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,133 @@
1
+ class Bj
2
+ module Util
3
+ module ModuleMethods
4
+ def constant_get const, &block
5
+ begin
6
+ ancestors = const.split(%r/::/)
7
+ parent = Object
8
+ while((child = ancestors.shift))
9
+ klass = parent.const_get child
10
+ parent = klass
11
+ end
12
+ klass
13
+ rescue
14
+ block ? block.call : raise
15
+ end
16
+ end
17
+
18
+ def const_or_env const, &block
19
+ constant_get(const){ ENV[const] || block.call }
20
+ end
21
+
22
+ def spawn cmd, options = {}
23
+ options.to_options!
24
+ logger = options.has_key?(:logger) ? options[:logger] : Bj.logger
25
+ verbose = options.has_key? :verbose
26
+ logger.info{ "cmd <#{ cmd }>" } if logger
27
+ status = systemu cmd, 1=>(stdout=""), 2=>(stderr="")
28
+ logger.info{ "status <#{ status.exitstatus }>" } if logger
29
+ if verbose
30
+ if logger
31
+ if stdout.empty?
32
+ logger.info{ "stdout <>" }
33
+ else
34
+ logger.info{ "stdout <" }
35
+ logger << stdout.strip.gsub(%r/^/, ' ')
36
+ logger << "\n>\n"
37
+ end
38
+ if stderr.empty?
39
+ logger.info{ "stderr <>" }
40
+ else
41
+ logger.info{ "stderr <" }
42
+ logger << stderr.strip.gsub(%r/^/, ' ')
43
+ logger << "\n>\n"
44
+ end
45
+ else
46
+ STDOUT.puts stdout
47
+ STDERR.puts stderr
48
+ end
49
+ end
50
+ status.exitstatus.zero? or raise "#{ cmd.inspect } failed with #{ $?.inspect }"
51
+ [ stdout, stderr ]
52
+ end
53
+
54
+ def start *a
55
+ q = Queue.new
56
+ thread = Thread.new do
57
+ Thread.current.abort_on_exception = true
58
+ systemu(*a){|pid| q << pid}
59
+ end
60
+ pid = q.pop
61
+ thread.singleton_class{ define_method(:pid){ pid } }
62
+ thread
63
+ end
64
+
65
+ def alive pid
66
+ return false unless pid
67
+ pid = Integer pid.to_s
68
+ Process::kill 0, pid
69
+ true
70
+ rescue Errno::ESRCH, Errno::EPERM
71
+ false
72
+ end
73
+ alias_method "alive?", "alive"
74
+
75
+ def which_ruby
76
+ c = ::Config::CONFIG
77
+ ruby = File::join(c['bindir'], c['ruby_install_name']) << c['EXEEXT']
78
+ raise "ruby @ #{ ruby } not executable!?" unless test(?e, ruby)
79
+ ruby
80
+ end
81
+
82
+ def which_rake
83
+ tmp = Tempfile.new Process.pid
84
+ tmp.write "task(:foobar){ puts 42 }"
85
+ tmp.close
86
+ bat = spawn("rake.bat -f #{ tmp.path.inspect } foobar", :logger => false) rescue false
87
+ bat ? "rake.bat" : "rake"
88
+ ensure
89
+ tmp.close! rescue nil
90
+ end
91
+
92
+ def ipc_signals_supported
93
+ @ipc_signals_supported ||=
94
+ IO.popen 'ruby', 'r+' do |ruby|
95
+ pid = ruby.pid
96
+ begin
97
+ Process.kill 'TERM', pid
98
+ true
99
+ rescue Exception
100
+ false
101
+ end
102
+ end
103
+ end
104
+ alias_method "ipc_signals_supported?", "ipc_signals_supported"
105
+
106
+ def find_script basename
107
+ path = ENV["PATH"] || ENV["path"] || Bj.default_path
108
+ raise "no env PATH" unless path
109
+ path = path.split File::PATH_SEPARATOR
110
+ path.unshift File.join(Bj.rails_root, "script")
111
+ path.each do |directory|
112
+ script = File.join directory, basename
113
+ return File.expand_path(script) if(test(?s, script) and test(?r, script))
114
+ end
115
+ raise "no #{ basename } found in #{ path.inspect }"
116
+ end
117
+
118
+ def valid_rails_root root = ".", expected = %w[ config script app ]
119
+ directories = expected.flatten.compact.map{|dir| dir.to_s}
120
+ directories.all?{|dir| test(?d, File.join(root, dir))}
121
+ end
122
+ alias_method "valid_rails_root?", "valid_rails_root"
123
+
124
+ def emsg e
125
+ m = e.message rescue ""
126
+ c = e.class rescue Exception
127
+ b = e.backtrace.join("\n") rescue ""
128
+ "#{ m }(#{ c })\n#{ b }"
129
+ end
130
+ end
131
+ send :extend, ModuleMethods
132
+ end
133
+ end
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: averell23-bj
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Ara T. Howard (ara.t.howard@noaa.gov)
8
+ - Daniel Hahn (JRuby modifications)
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2009-09-11 00:00:00 -07:00
14
+ default_executable: bj
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: main
18
+ type: :runtime
19
+ version_requirement:
20
+ version_requirements: !ruby/object:Gem::Requirement
21
+ requirements:
22
+ - - ">="
23
+ - !ruby/object:Gem::Version
24
+ version: 2.6.0
25
+ version:
26
+ - !ruby/object:Gem::Dependency
27
+ name: averell23-systemu
28
+ type: :runtime
29
+ version_requirement:
30
+ version_requirements: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: 1.2.0
35
+ version:
36
+ - !ruby/object:Gem::Dependency
37
+ name: orderedhash
38
+ type: :runtime
39
+ version_requirement:
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: 0.0.3
45
+ version:
46
+ description: Special version for JRuby compatibility. Backgroundjob (Bj) is a brain dead simple zero admin background priority queue for Rails. Bj is robust, platform independent (including windows), and supports internal or external manangement of the background runner process.
47
+ email: hahn@netseven.it
48
+ executables:
49
+ - bj
50
+ extensions: []
51
+
52
+ extra_rdoc_files:
53
+ - README
54
+ files:
55
+ - VERSION.yml
56
+ - bin/bj
57
+ - lib/bj
58
+ - lib/bj/api.rb
59
+ - lib/bj/attributes.rb
60
+ - lib/bj/bj.rb
61
+ - lib/bj/errors.rb
62
+ - lib/bj/joblist.rb
63
+ - lib/bj/logger.rb
64
+ - lib/bj/runner.rb
65
+ - lib/bj/stdext.rb
66
+ - lib/bj/table.rb
67
+ - lib/bj/util.rb
68
+ - lib/bj.rb
69
+ - README
70
+ has_rdoc: true
71
+ homepage: http://codeforpeople.com/lib/ruby/
72
+ post_install_message:
73
+ rdoc_options:
74
+ - --inline-source
75
+ - --charset=UTF-8
76
+ require_paths:
77
+ - lib
78
+ required_ruby_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: "0"
83
+ version:
84
+ required_rubygems_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: "0"
89
+ version:
90
+ requirements: []
91
+
92
+ rubyforge_project:
93
+ rubygems_version: 1.2.0
94
+ signing_key:
95
+ specification_version: 2
96
+ summary: Backgroundjob
97
+ test_files: []
98
+