class AutoCron

Public Class Methods

new( templates, application ) click to toggle source
# File lib/auto_cron.rb, line 4
def initialize( templates, application )
  require 'erb'
  @auto_cron_dir = ENV["CONFIG_AUTO_CRON"]
  @templates = templates.split(",")
  @application = application
end

Public Instance Methods

updated_crontab() click to toggle source
# File lib/auto_cron.rb, line 11
def updated_crontab
  # Check for unopened or unclosed identifier blocks
  if read_crontab.index(comment_open) && !read_crontab.index(comment_close)
    warn "[fail] Unclosed indentifier; Your crontab file contains '#{comment_open}', but no '#{comment_close}'"
    exit(1)
  elsif !read_crontab.index(comment_open) && read_crontab.index(comment_close)
    warn "[fail] Unopened indentifier; Your crontab file contains '#{comment_close}', but no '#{comment_open}'"
    exit(1)
  end

  # If an existing identier block is found, replace it with the new cron entries
  if read_crontab.index(comment_open) && read_crontab.index(comment_close)
    read_crontab.gsub(Regexp.new("#{comment_open}.+#{comment_close}", Regexp::MULTILINE), auto_cron_wrapped.chomp)
  else 
    # Otherwise, append the new cron entries after any existing ones
    [read_crontab, auto_cron_wrapped].join("\n\n")
  end
end

Protected Instance Methods

auto_cron_inner() click to toggle source
# File lib/auto_cron.rb, line 36
def auto_cron_inner
  header_template = File.join( @auto_cron_dir, 'header.erb' ) 
  header = if File.exist?( header_template )
    ERB.new( File.read( header_template ) ).result
  else
    ''
  end
  @templates.each do |template|
    full_template_path = File.join( @auto_cron_dir, "#{ template }.erb" )
    header += "\n\n" + ERB.new( File.read( full_template_path ) ).result
  end
  
  header
end
auto_cron_wrapped() click to toggle source
# File lib/auto_cron.rb, line 32
def auto_cron_wrapped
  @auto_cron_wrapped ||= [comment_open, auto_cron_inner, comment_close].join("\n") + "\n"
end
comment_base() click to toggle source
# File lib/auto_cron.rb, line 55
def comment_base
  "auto_cron generated tasks for: #{ @application }"
end
comment_close() click to toggle source
# File lib/auto_cron.rb, line 63
def comment_close
  "# -- #{ comment_base } End -- #"
end
comment_open() click to toggle source
# File lib/auto_cron.rb, line 59
def comment_open
  "# -- #{ comment_base } Begin -- #"
end
read_crontab() click to toggle source
# File lib/auto_cron.rb, line 51
def read_crontab
  @current_crontab ||= %x[crontab -l 2> /dev/null]
end