<commit_before>module Freyr
  
  class Command
    extend Forwardable
    
    ROOT_PIDS = '/var/run/freyr'
    FREYR_PIDS = ENV['USER'] == 'root' ? ROOT_PIDS : File.expand_path('.freyr', '~')
    
    if !File.exist?(FREYR_PIDS)
      Dir.mkdir(FREYR_PIDS)
    elsif !File.directory?(FREYR_PIDS)
      File.delete(FREYR_PIDS)
      Dir.mkdir(FREYR_PIDS)
    end
    
    attr_reader :command, :name, :service
    
    def initialize(name, command=nil, args = {})
      if name.is_a?(Service)
        @service = name
        @name = service.name
        @command = service.start_command
        @env = service.env
      else
        @name = name
        @command = command
        
        @env = args[:env]
      end
    end
    
    def pid_file
      File.join(file_dir,"#{@name}.pid")
    end
    
    def file_dir
      admin? ? ROOT_PIDS : FREYR_PIDS
    end
    
    def read_pid
      return unless File.exist?(pid_file)
      p = File.open(pid_file).read
      p ? p.to_i : nil
    end
    
    def pid(force = false)
      @pid = nil if force
      @pid ||= read_pid
    end
    
    def alive?
      return unless pid
      Process.getpgid(pid)
      true
    rescue Errno::ESRCH
      retried = false unless defined?(retried)
      
      if !retried && @pid = pid_from_procname
        save
        retried = true<commit_msg>look in both directories for pid when using sudo<commit_after>module Freyr
  
  class Command
    extend Forwardable
    
    ROOT_PIDS = '/var/run/freyr'
    USER_PIDS = File.expand_path('.freyr', '~')
    
    if !File.exist?(USER_PIDS)
      Dir.mkdir(USER_PIDS)
    elsif !File.directory?(USER_PIDS)
      File.delete(USER_PIDS)
      Dir.mkdir(USER_PIDS)
    end
    
    attr_reader :command, :name, :service
    
    def initialize(name, command=nil, args = {})
      if name.is_a?(Service)
        @service = name
        @name = service.name
        @command = service.start_command
        @env = service.env
      else
        @name = name
        @command = command
        
        @env = args[:env]
      end
    end
    
    def pid_file
      path = File.join(file_dir,"#{@name}.pid")
      if File.exist?(path)
        path
      else
        File.join(file_dir(true),"#{@name}.pid")
      end
    end
    
    def file_dir(force_user = false)
      return USER_PIDS if force_user
      admin? ? ROOT_PIDS : USER_PIDS
    end
    
    def read_pid
      return unless File.exist?(pid_file)
      p = File.open(pid_file).read
      p ? p.to_i : nil
    end
    
    def pid(force = false)
      @pid = nil if force
      @pid ||= read_pid
    end
    
    def alive?
      return unless pid
      Process.getpgid(pid)
      true
    rescue Errno::ESRCH
      retried = false unless defined?(retried)
      
      if !retried && @pid = pid_from_procname
        save
        retried = true
{'fraction_non_alphanumeric': 0.10028149190710767, 'fraction_numerical': 0.0, 'mean_word_length': 1.854417670682731, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3472936', 'n_tokens_mistral': 982, 'n_tokens_neox': 983, 'n_words': 214}