Class: Rubber::Commands::Cron

Inherits:
Clamp::Command
  • Object
show all
Defined in:
lib/rubber/commands/cron.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.descriptionObject



17
18
19
20
21
22
23
24
# File 'lib/rubber/commands/cron.rb', line 17

def self.description
  Rubber::Util.clean_indent( <<-EOS
    Runs the given command, sending all stdout/stderr to a logfile, but echoing
    the entire file if the command exits with an error.  Exits with the same
    error code the command exited with
  EOS
  )
end

.subcommand_descriptionObject



13
14
15
# File 'lib/rubber/commands/cron.rb', line 13

def self.subcommand_description
  "A cron-safe way for running commands"
end

.subcommand_nameObject



9
10
11
# File 'lib/rubber/commands/cron.rb', line 9

def self.subcommand_name
  "cron"
end

Instance Method Details

#executeObject



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
  
  # make sure dir containing logfile exists
  FileUtils.mkdir_p(File.dirname(log))
  
  # set current directory to rootdir
  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