Sha256: e3a4e852ff3492e4ecdfa770fb10178971f5429682185b14969889ecc54d7845

Contents?: true

Size: 1.98 KB

Versions: 33

Compression:

Stored size: 1.98 KB

Contents

# encoding: utf-8

module OneApm
  module Agent
    class RestartMonitor

      def initialize
        @lock = Mutex.new
        create_restart_file
      end

      def create_restart_file
        @lock.synchronize do
          begin
            file_path = OneApm::Agent::RestartMonitor.file_path
            return if file_path.nil?
            if !File.exist?(file_path) && system("touch #{file_path}")
              OneApm::Manager.logger.info "create #{file_path} successful."
            else
              OneApm::Manager.logger.debug "#{file_path} has exist."  
            end
             OneApm::Agent::RestartMonitor.timestamp = File.mtime(file_path).to_i
          rescue => e
            OneApm::Manager.logger.warn("create #{file_path} fail.", e)
          end
        end
      end
      
      def self.need_restart?
        @result = timestamp == restart_file_timestamp
        self.timestamp = restart_file_timestamp unless @result
        !@result
      end

      def self.touch
        !file_path.nil? && system("touch #{file_path}")
      end

      private

      def self.timestamp
        @timestamp || 0
      end

      def self.timestamp= timestamp
        @timestamp = timestamp
      end

      def self.restart_file_timestamp
        return 0 unless File.exist?(file_path)
        File.mtime(file_path).to_i
      end

      def self.file_path
        return nil if path.nil?
        @file_path ||= "#{path}/#{OneApm::Manager.config[:agent_restart_file_name]}"
      end

      def self.path 
        @path ||= find_or_create_path(OneApm::Manager.config[:agent_restart_file_path], OneApm::Probe.instance.root)
      end

      def self.find_or_create_path(path_setting, root)
        for abs_path in [ File.expand_path(path_setting),
                          File.expand_path(File.join(root, path_setting)) ] do
          if File.directory?(abs_path) || (Dir.mkdir(abs_path) rescue nil)
            return abs_path[%r{^(.*?)/?$}]
          end
        end
        nil
      end

    end
  end
end

Version data entries

33 entries across 33 versions & 1 rubygems

Version Path
oneapm_rpm-1.4.2 lib/one_apm/agent/agent/restart_monitor.rb
oneapm_rpm-1.4.1 lib/one_apm/agent/agent/restart_monitor.rb
oneapm_rpm-1.4.0 lib/one_apm/agent/agent/restart_monitor.rb
oneapm_rpm-1.3.7 lib/one_apm/agent/agent/restart_monitor.rb
oneapm_rpm-1.3.7.rc1 lib/one_apm/agent/agent/restart_monitor.rb
oneapm_rpm-1.3.6 lib/one_apm/agent/agent/restart_monitor.rb
oneapm_rpm-1.3.6.rc1 lib/one_apm/agent/agent/restart_monitor.rb
oneapm_rpm-1.3.5 lib/one_apm/agent/agent/restart_monitor.rb
oneapm_rpm-1.3.5.rc1 lib/one_apm/agent/agent/restart_monitor.rb
oneapm_rpm-1.3.4 lib/one_apm/agent/agent/restart_monitor.rb
oneapm_rpm-1.3.4.rc1 lib/one_apm/agent/agent/restart_monitor.rb
oneapm_rpm-1.3.3 lib/one_apm/agent/agent/restart_monitor.rb
oneapm_rpm-1.3.3.rc1 lib/one_apm/agent/agent/restart_monitor.rb
oneapm_rpm-1.3.2 lib/one_apm/agent/agent/restart_monitor.rb
oneapm_rpm-1.3.2.rc1 lib/one_apm/agent/agent/restart_monitor.rb
oneapm_rpm-1.3.1 lib/one_apm/agent/agent/restart_monitor.rb
oneapm_rpm-1.3.1.rc1 lib/one_apm/agent/agent/restart_monitor.rb
oneapm_rpm-1.3.0 lib/one_apm/agent/agent/restart_monitor.rb
oneapm_rpm-1.3.0.rc1 lib/one_apm/agent/agent/restart_monitor.rb
oneapm_rpm-1.2.9 lib/one_apm/agent/agent/restart_monitor.rb