class TabKeeper::LogRedirection

Constants

MULTIPLE_CHARACTERS

Attributes

job_name[R]

Public Class Methods

new(previous, job: nil, job_name_proc: ->(x) { x } click to toggle source
# File lib/tab_keeper/log_redirection.rb, line 3
def initialize(previous, job: nil,
                         job_name_proc: ->(x) { x },
                         timing: nil,
                         log_directory: nil,
                         error_suffix: nil,
                         include_date_in_log_name: false,
                         **_options)
  if previous.nil?
    raise ArgumentError, "#{self.class.name} must not be first in the cron pipeline!"
  end

  if log_directory.nil?
    raise ArgumentError, "log_directory must be set for #{self.class.name}"
  end

  @previous = previous
  @job = job
  # Normalise the job name: if it's an array take the first thing, then get the last
  # thing after a "::" (if it's a class name), or a "/" (if it's a script, and removes
  # the need to escape yay)
  job_name = Array(@job).first.to_s.split(/((::)|\/)/).last.split(/[^\w\d]/).first
  @job_name = job_name_proc.call(job_name)
  @timing = timing
  @log_directory = log_directory
  @error_suffix = error_suffix
  @include_date_in_log_name = include_date_in_log_name
end

Public Instance Methods

to_s() click to toggle source
# File lib/tab_keeper/log_redirection.rb, line 31
def to_s
  "#{@previous} " \
    ">> #{@log_directory}/#{job_name}#{date_part}.log " \
    "2>#{error_part}"
end

Private Instance Methods

date_format() click to toggle source
# File lib/tab_keeper/log_redirection.rb, line 55
def date_format
  [
    '%Y',
    '%m',
    day_component,
    hour_component,
    min_component,
    second_component
  ].compact.join('-')
end
date_part() click to toggle source
# File lib/tab_keeper/log_redirection.rb, line 46
def date_part
  return unless include_date?
  "_`date +#{date_format}`"
end
day_component() click to toggle source
# File lib/tab_keeper/log_redirection.rb, line 66
def day_component
  '%d' if more_than_once_a_month?
end
error_part() click to toggle source
# File lib/tab_keeper/log_redirection.rb, line 41
def error_part
  return "&1" unless @error_suffix
  "> #{@log_directory}/#{job_name}#{date_part}.#{@error_suffix}.log"
end
hour_component() click to toggle source
# File lib/tab_keeper/log_redirection.rb, line 70
def hour_component
  '%H' if more_than_once_a_day?
end
include_date?() click to toggle source
# File lib/tab_keeper/log_redirection.rb, line 51
def include_date?
  @include_date_in_log_name
end
min_component() click to toggle source
# File lib/tab_keeper/log_redirection.rb, line 74
def min_component
  '%M' if more_than_once_a_day?
end
more_than_once_a_day?() click to toggle source
# File lib/tab_keeper/log_redirection.rb, line 88
def more_than_once_a_day?
  MULTIPLE_CHARACTERS.any? { |char| @timing.split(' ').first(2).join.include?(char) }
end
more_than_once_a_month?() click to toggle source
# File lib/tab_keeper/log_redirection.rb, line 84
def more_than_once_a_month?
  MULTIPLE_CHARACTERS.any? { |char| @timing.split(' ')[2].include?(char) }
end
more_than_once_an_hour?() click to toggle source
# File lib/tab_keeper/log_redirection.rb, line 92
def more_than_once_an_hour?
  MULTIPLE_CHARACTERS.any? { |char| @timing.split(' ').first.include?(char) }
end
second_component() click to toggle source
# File lib/tab_keeper/log_redirection.rb, line 78
def second_component
  '%s' if more_than_once_an_hour?
end