class Ridoku::Cron

Attributes

cron[RW]

Public Instance Methods

run() click to toggle source
# File lib/ridoku/cron.rb, line 17
def run
  command = Base.config[:command]
  sub_command = (command.length > 0 && command[1]) || nil

  load_environment

  case sub_command
  when 'list', nil
    list
  when 'set', 'add', 'update'
    add
  when 'delete'
    delete
  when 'remove'
    remove
  else
    print_cron_help
  end
end

Protected Instance Methods

add() click to toggle source
# File lib/ridoku/cron.rb, line 131
def add

  croninfo = {}
  cronindex = nil

  ARGV.each do |cron|
    info = cron.split(':',2)
    croninfo[info[0].to_s] = info[1]
    cronindex = get_path_index(info[1]) if info[0] == 'path'
  end

  croninfo['instance'] = @default_instance unless croninfo.key?('instance')

  if cronindex.nil?
    self.cron << croninfo
  else
    self.cron[cronindex] = croninfo
  end

  list
  Base.save_stack
end
delete() click to toggle source
# File lib/ridoku/cron.rb, line 154
def delete
  return print_cron_help unless ARGV.length > 0
  cronindex = get_path_index(ARGV.first)

  if cronindex.nil?
    $stdout.puts $stdout.colorize(
      'Unable to find the specified script path in the cron list.', :red)
    return list
  end

  cr = self.cron[cronindex]
  cr['type'] = 'delete'
  cr['minute'] = '-'
  cr['hour'] = '-'
  cr['day_of_month'] = '-'
  cr['month'] = '-'
  cr['day_of_week'] = '-'
  
  list
  Base.save_stack
end
get_path_index(path) click to toggle source
# File lib/ridoku/cron.rb, line 194
def get_path_index(path)
  cron.each_with_index do |cr, idx|
    return idx if cr['path'] == path
  end

  return nil
end
line(offset, columns, keys = nil) click to toggle source
# File lib/ridoku/cron.rb, line 202
def line(offset, columns, keys = nil)
  keys ||= columns.keys
  output = ''

  keys.each do |key|
    skey = key.to_s
    content = columns[key] || columns[skey]
    if skey == 'type'
      case content
      when 'delete'
        output += $stdout.colorize(content, :red)
      when 'runner'
        output += $stdout.colorize(content, :green)
      else
        output += $stdout.colorize(content, :yellow)
      end
    else
      output += content
    end
    output += ' '*(offset[key] - content.length + 2)
  end

  output
end
list() click to toggle source
# File lib/ridoku/cron.rb, line 91
def list
  if cron.length == 0
    $stdout.puts 'No cron jobs specified!'
  else

    columns = {
      type: 'Type',
      path: 'Scripts',
      minute: 'M',
      hour: 'H',
      day_of_month: 'DM',
      month: 'MO',
      day_of_week: 'DW',
      instance: 'Instance'
    }

    offset = {}

    self.cron.each do |cr|
      columns.keys.each do |key|
        skey = key.to_s
        cr[skey] = '*' unless cr.key?(skey)
        val = cr[skey].length
        offset[key] = val if cr.key?(skey) && val > (offset[key] || 0)
      end
    end

    columns.keys.each do |key|
      offset[key] = columns[key].length if
        columns[key].length > (offset[key] || 0)
    end

    $stdout.puts $stdout.colorize(line(offset, columns), :bold)
    self.cron.each { |cr| $stdout.puts line(offset, cr, columns.keys) }
  end
rescue =>e
  puts e.backtrace
  puts e
end
load_environment() click to toggle source
# File lib/ridoku/cron.rb, line 39
def load_environment
  Base.fetch_stack
  Base.fetch_instance('workers')

  @default_instance = Base.instances.first[:hostname]
  self.cron = (Base.custom_json['deploy'][Base.config[:app].downcase]['cron'] ||= {})
end
print_cron_help() click to toggle source
remove() click to toggle source
# File lib/ridoku/cron.rb, line 176
def remove
  return print_cron_help unless ARGV.length > 0
  cronindex = get_path_index(ARGV.first)

  if cronindex.nil?
    $stdout.puts $stdout.colorize(
      'Unable to find the specified script path in the cron list.', :red)
    return list
  end

  self.cron.delete_at(cronindex)

  list
  Base.save_stack
end