58
59
60
61
62
63
64
65
66
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
|
# File 'lib/rubber/commands/cron.rb', line 58
def execute
cmd = command_list
self.rootdir ||= Rubber.root
ident = cmd[0].gsub(/\W+/, "_").gsub(/(^_+)|(_+$)/, '')[0..19]
self.logfile ||= "#{Rubber.root}/log/cron-sh-#{ident}.log"
log = logfile
if task?
log = "#{rootdir}/log/cron-task-#{ident}.log"
cmd = [$0] + cmd
elsif ruby?
ruby_code = cmd.join(' ')
ident = ruby_code.gsub(/\W+/, "_").gsub(/(^_+)|(_+$)/, '')[0..19]
log = "#{rootdir}/log/cron-ruby-#{ident}.log"
cmd = ["ruby", "-e", ruby_code]
elsif runner?
log = "#{rootdir}/log/cron-runner-#{ident}.log"
cmd = ["rails", "runner"] + cmd
elsif rake?
log = "#{rootdir}/log/cron-rake-#{ident}.log"
cmd = ["rake"] + cmd
end
if user
if user =~ /^[0-9]+$/
uid = user.to_i
else
uid = Etc.getpwnam(user).uid
end
Process::UID.change_privilege(uid) if uid != Process.euid
end
FileUtils.mkdir_p(File.dirname(log))
Dir.chdir(rootdir)
status = Open3.popen3(*cmd) do |stdin, stdout, stderr, wait_thread|
File.open(log, "a") do | fh |
fh.puts "\nrubber:cron running #{cmd.inspect} at #{Time.now}\n"
threads = []
threads << Thread.new(stdout) do |stdout|
stdout.each { |line| $stdout.puts line if echoout?; fh.print line; fh.flush }
end
threads << Thread.new(stderr) do |stderr|
stderr.each { |line| $stderr.puts line if echoerr?; fh.print line; fh.flush }
end
threads.each { |t| t.join }
end
wait_thread.value
end
result = status.exitstatus
if result != 0
puts ""
puts "*** Process exited with non-zero error code, full output follows"
puts "*** Command was: #{cmd.join(' ')}"
puts ""
puts IO.read(log)
end
exit(result)
end
|