module MiGA::Daemon::Base

def bypass_maintenance?

Should the daemon ignore regular maintenance steps?
#
def bypass_maintenance?
  !!runopts(:bypass_maintenance)
end

def latency

Returns Integer indicating the number of seconds to sleep between checks
#
def latency
  runopts(:latency)
end

def logfh

Writing file handler (IO) to the log file
#
def logfh
  @logfh ||= nil
  return $stderr if show_log?
  return @logfh if @logfh && !@logfh.closed?
  @logfh = File.open(output_file, 'w')
end

def maxjobs

Returns Integer indicating the maximum number of concurrent jobs to run
#
def maxjobs
  runopts(:maxjobs)
end

def nodelist

Returns the path to the list of execution hostnames
#
def nodelist
  runopts(:nodelist)
end

def ppn(what = :dataset)

See also #runopts_for
Returns Integer indicating the number of CPUs per job, in jobs for +what+.
#
def ppn(what = :dataset)
  runopts_for(:ppn, what)
end

def runopts(k, v = nil, force = false)

value.
(or nil to use as getter). Skips consistency tests if +force+. Returns new
Set/get #options, where +k+ is the Symbol of the option and +v+ is the value
#
def runopts(k, v = nil, force = false)
  k = k.to_sym
  unless v.nil?
    case k
    when :latency, :maxjobs, :ppn, :ppn_project, :format_version, :verbosity,
         :skip_maintenance
      v = v.to_i
      if !force && v == 0 && !%i[verbosity skip_maintenance].include?(k)
        raise "Daemon's #{k} cannot be set to zero"
      end
    when :shutdown_when_done, :show_log, :bypass_maintenance
      v = !!v
    when :nodelist
      if v =~ /^\$/
        vv = ENV[v.sub('$', '')] or raise "Unset environment variable: #{v}"
        v = vv
      end
      say "Reading node list: #{v}"
      v = File.readlines(v).map(&:chomp)
    end
    @runopts[k] = v
  end
  @runopts[k]
end

def runopts_for(opt, what)

+:dataset+ or +:projects+
Returns the running option +opt+ in jobs for +what+. +what+ can be
#
def runopts_for(opt, what)
  runopts(:"#{opt}_#{what}") || runopts(opt)
end

def show_log!

Display log instead of the progress summary
#
def show_log!
  @runopts[:show_log] = true
end

def show_log?

Display log instead of the progress summary?
#
def show_log?
  @runopts[:show_log] ||= false
end

def show_summary!

Display progress summary instead of the log
#
def show_summary!
  @runopts[:show_log] = false
end

def shutdown_when_done?

complete
Returns Boolean indicating if the daemon should shutdown when processing is
#
def shutdown_when_done?
  !!runopts(:shutdown_when_done)
end

def skip_maintenance

Returns the number of times maintenance should be skipped before running
#
def skip_maintenance
  runopts(:skip_maintenance) || 0
end

def verbosity

3: Same, and indicate when each loop begins and ends
2: Same, and indicate when each task is performed (even if nothing happens)
1: General daemon and job information
0: No output
Verbosity levels are:
Returns the level of verbosity for the daemon as an Integer, or 1 if unset.
#
def verbosity
  runopts(:verbosity) || 1
end