Class: S3log::Cron

Inherits:
Object
  • Object
show all
Defined in:
lib/s3log/cron.rb

Instance Method Summary collapse

Constructor Details

#initialize(configfile) ⇒ Cron

Returns a new instance of Cron.



4
5
6
7
8
9
10
11
12
13
# File 'lib/s3log/cron.rb', line 4

def initialize(configfile)
  @configfile = configfile
  @config = YAML::load_file(configfile)
  @path = File.dirname(File.expand_path(configfile))
  @jobname = @config['jobname']
  @schedule = @config['schedule']
  @logdir = @config['logdir']
  FileUtils.mkdir(@logdir) unless Dir.exists? @logdir
  S3log::Log.set_logger(File.join(@logdir, 's3log.log'), @config['loglevel'])
end

Instance Method Details

#existingObject



38
39
40
41
42
43
44
45
# File 'lib/s3log/cron.rb', line 38

def existing
  existing = %x(crontab -l 2> /dev/null)
  if $?.exitstatus.zero?
    existing
  else
    ""
  end
end

#updateObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/s3log/cron.rb', line 15

def update
  line = "#{@schedule} bash -l -c 'cd #{@path} && bundle exec s3log download -c #{@configfile} >> /dev/null 2>&1' # s3log_#{@jobname}\n"
  tmp_cron_file = Tempfile.open('tmp_cron')
  included = false
  existing.each_line do |l|
    if l =~ Regexp.new("# s3log_#{@jobname}")
      tmp_cron_file << line
      included = true
    else
      tmp_cron_file << l
    end
  end
  tmp_cron_file << "# S3log job #{@jobname}\n#{line}" unless included
  tmp_cron_file.fsync
  if system("crontab #{tmp_cron_file.path}")
    S3log::Log.info "#{@jobname} [update] crontab updated."
  else
    S3log::Log.warn "#{@jobname} [fail] Couldn't write crontab."
    tmp_cron_file.close!
    exit(1)
  end
end