Sha256: 7e01afea94b391f680b6dbfe5322b948490430eae6a71138bd1d38ba13025fca

Contents?: true

Size: 1.55 KB

Versions: 1

Compression:

Stored size: 1.55 KB

Contents

# encoding: UTF-8
# frozen_string_literal: true

require 'nrser/refinements/types'
using NRSER::Types


# Namespace
# =======================================================================

module  Locd
module  CLI
module  Command


# Definitions
# =======================================================================

class Agent < Base

  desc              'truncate-logs',
                    "Truncate agent log file(s)."
  
  include_shared    t[ groups: t.has_any( :pattern, :multi ) ]
  
  option            :restart,
                    desc: "Restart the agent after truncation",
                    type: :boolean,
                    default: true
  
  def truncate_logs
    find_multi!( pattern ).each do |agent|
      log_paths = [agent.out_path, agent.err_path].compact.uniq
      
      unless log_paths.empty?
        restart = options[:restart] && agent.running?
        
        agent.stop if restart
        
        log_paths.each do |log_path|
          begin
            log_path.open( 'w' ) { |f| f.truncate 0 }
          rescue Exception => error
            logger.error "Failed to truncate #{ log_path }", error
          else
            logger.info "Truncated",
              'file' => log_path.to_s,
              'agent.label' => agent.label
          end
        end # each log_path
        
        agent.start if restart
      end # unless log_paths.empty?
    end # each agent
  end # #truncate_logs

end


# /Namespace
# =======================================================================

end # module  Command
end # module  CLI
end # module  Locd

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
locd-0.1.13 lib/locd/cli/command/agent/truncate_logs.rb