ThiagoLelis-backgroundjob 1.0.2 → 1.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/lib/bj/util.rb ADDED
@@ -0,0 +1,111 @@
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
+ logger.info{ "cmd <#{ cmd }>" } if logger
26
+ status = systemu cmd, 1=>(stdout=""), 2=>(stderr="")
27
+ logger.info{ "status <#{ status.exitstatus }>" } if logger
28
+ status.exitstatus.zero? or raise "#{ cmd.inspect } failed with #{ $?.inspect }"
29
+ [ stdout, stderr ]
30
+ end
31
+
32
+ def start *a
33
+ q = Queue.new
34
+ thread = Thread.new do
35
+ Thread.current.abort_on_exception = true
36
+ systemu(*a){|pid| q << pid}
37
+ end
38
+ pid = q.pop
39
+ thread.singleton_class{ define_method(:pid){ pid } }
40
+ thread
41
+ end
42
+
43
+ def alive pid
44
+ return false unless pid
45
+ pid = Integer pid.to_s
46
+ Process::kill 0, pid
47
+ true
48
+ rescue Errno::ESRCH, Errno::EPERM
49
+ false
50
+ end
51
+ alias_method "alive?", "alive"
52
+
53
+ def which_ruby
54
+ c = ::Config::CONFIG
55
+ ruby = File::join(c['bindir'], c['ruby_install_name']) << c['EXEEXT']
56
+ raise "ruby @ #{ ruby } not executable!?" unless test(?e, ruby)
57
+ ruby
58
+ end
59
+
60
+ def which_rake
61
+ tmp = Tempfile.new Process.pid
62
+ tmp.write "task(:foobar){ puts 42 }"
63
+ tmp.close
64
+ bat = spawn("rake.bat -f #{ tmp.path.inspect } foobar", :logger => false) rescue false
65
+ bat ? "rake.bat" : "rake"
66
+ ensure
67
+ tmp.close! rescue nil
68
+ end
69
+
70
+ def ipc_signals_supported
71
+ @ipc_signals_supported ||=
72
+ IO.popen 'ruby', 'r+' do |ruby|
73
+ pid = ruby.pid
74
+ begin
75
+ Process.kill 'TERM', pid
76
+ true
77
+ rescue Exception
78
+ false
79
+ end
80
+ end
81
+ end
82
+ alias_method "ipc_signals_supported?", "ipc_signals_supported"
83
+
84
+ def find_script basename
85
+ path = ENV["PATH"] || ENV["path"] || Bj.default_path
86
+ raise "no env PATH" unless path
87
+ path = path.split File::PATH_SEPARATOR
88
+ path.unshift File.join(Bj.rails_root, "script")
89
+ path.each do |directory|
90
+ script = File.join directory, basename
91
+ return File.expand_path(script) if(test(?s, script) and test(?r, script))
92
+ end
93
+ raise "no #{ basename } found in #{ path.inspect }"
94
+ end
95
+
96
+ def valid_rails_root root = ".", expected = %w[ config script app ]
97
+ directories = expected.flatten.compact.map{|dir| dir.to_s}
98
+ directories.all?{|dir| test(?d, File.join(root, dir))}
99
+ end
100
+ alias_method "valid_rails_root?", "valid_rails_root"
101
+
102
+ def emsg e
103
+ m = e.message rescue ""
104
+ c = e.class rescue Exception
105
+ b = e.backtrace.join("\n") rescue ""
106
+ "#{ m }(#{ c })\n#{ b }"
107
+ end
108
+ end
109
+ send :extend, ModuleMethods
110
+ end
111
+ end
data/rakefile ADDED
@@ -0,0 +1,3 @@
1
+ task :foobar do
2
+ puts 42
3
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ThiagoLelis-backgroundjob
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ara T. Howard
@@ -21,8 +21,24 @@ extensions: []
21
21
 
22
22
  extra_rdoc_files: []
23
23
 
24
- files: []
25
-
24
+ files:
25
+ - HISTORY
26
+ - install.rb
27
+ - rakefile
28
+ - README
29
+ - TODO
30
+ - bin/bj
31
+ - lib/bj/api.rb
32
+ - lib/bj/attributes.rb
33
+ - lib/bj.rb
34
+ - lib/bj/bj.rb
35
+ - lib/bj/errors.rb
36
+ - lib/bj/joblist.rb
37
+ - lib/bj/logger.rb
38
+ - lib/bj/runner.rb
39
+ - lib/bj/stdext.rb
40
+ - lib/bj/table.rb
41
+ - lib/bj/util.rb
26
42
  has_rdoc: true
27
43
  homepage: http://codeforpeople.com/lib/ruby/bj/
28
44
  post_install_message: