<commit_before>namespace :submission do
  desc 'Generate PDF files for submissions'

  #
  # Returns the file that indicates if this rake process is already executing...
  #
  def rake_executing_marker_file
    File.join(Doubtfire::Application.config.student_work_dir, 'rake.running')
  end

  def is_executing?
    tmp_file = rake_executing_marker_file
    File.exist?(tmp_file)
  end

  def start_executing
    FileUtils.touch(rake_executing_marker_file)
  end

  def end_executing
    FileUtils.rm(rake_executing_marker_file)
  end

  task generate_pdfs: :environment do
    if is_executing?
      logger.error 'Skip generate pdf -- already executing'
      puts 'Skip generate pdf -- already executing'
    else
      start_executing

      begin
        logger.info 'Starting generate pdf'
<commit_msg>FIX: Have generate pdfs check pid of process<commit_after>namespace :submission do
  desc 'Generate PDF files for submissions'

  #
  # Returns the file that indicates if this rake process is already executing...
  #
  def rake_executing_marker_file
    File.join(Doubtfire::Application.config.student_work_dir, 'rake.running')
  end

  def is_executing?
    pid_file = rake_executing_marker_file
    tmp_file = pid_file
    return false unless File.exist?(tmp_file)

    # Check that the pid matches something running...
    begin
      pid = File.read(pid_file).to_i
      raise Errno::ESRCH if pid == 0
      Process.getpgid( pid )
      true
    rescue Errno::ESRCH
      # clean up old running file
      end_executing
      false
    end
  end

  def start_executing
    pid_file = rake_executing_marker_file
    FileUtils.touch(pid_file)
    File.open pid_file, "w" do |f|
      f.write Process.pid
    end
  end

  def end_executing
    FileUtils.rm(rake_executing_marker_file)
  end

  task generate_pdfs: :environment do
    if is_executing?
      logger.error 'Skip generate pdf -- already executing'
      puts 'Skip generate pdf -- already executing'
    else
      start_executing

      begin
        logger.info 'Starting generate pdf'

{'fraction_non_alphanumeric': 0.06313744536182612, 'fraction_numerical': 0.00048567265662943174, 'mean_word_length': 3.0711462450592886, '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': '5974451', 'n_tokens_mistral': 647, 'n_tokens_neox': 627, 'n_words': 180}