<commit_before>#!/usr/bin/env ruby
# Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
begin
  load File.expand_path('../bin/spring', __dir__)
rescue LoadError => e
  raise unless e.message.include?('spring')
end

dir = File.expand_path(File.join(File.dirname(__FILE__), '..'))
Dir.chdir dir

require 'bundler'
require 'daemons'

def before_fork
  # remember open file handles
  @files_to_reopen = []
  ObjectSpace.each_object(File) do |file|
    @files_to_reopen << file if !file.closed?
  end
end

def after_fork(dir)
  Dir.chdir dir

  # Re-open file handles
  @files_to_reopen.each do |file|
    file.reopen file.path, 'a+'
    file.sync = true
  end

  # Spring redirects STDOUT and STDERR to /dev/null
  # before we get here. This causes the `reopen` lines
  # below to fail because the handles are already
  # opened for write
  if defined?(Spring)
    $stdout.close
    $stderr.close
  end

  $stdout.reopen("#{dir}/log/scheduler_out.log", 'w')
  $stderr.reopen("#{dir}/log/scheduler_err.log", 'w')
end

before_fork

daemon_options = {
  multiple:  false,
  dir_mode:  :normal,
  dir:       File.join(dir, 'tmp', 'pids'),
  backtrace: true
}

Daemons.run_proc('scheduler', daemon_options) do

  after_fork(dir)

  require File.join(dir, 'config', 'environment')

  Rails.logger.info 'Scheduler started.'
  at_exit do
    Rails.logger.info 'Scheduler stopped.'
  end

  Scheduler.threads
end
<commit_msg>Refactoring: Stopping of scheduler worker process (via SIGTERM) isn't handled gracefully and prints Error (with Backtrace).<commit_after>#!/usr/bin/env ruby
# Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
begin
  load File.expand_path('../bin/spring', __dir__)
rescue LoadError => e
  raise unless e.message.include?('spring')
end

dir = File.expand_path(File.join(File.dirname(__FILE__), '..'))
Dir.chdir dir

require 'bundler'
require 'daemons'

def before_fork
  # remember open file handles
  @files_to_reopen = []
  ObjectSpace.each_object(File) do |file|
    @files_to_reopen << file if !file.closed?
  end
end

def after_fork(dir)
  Dir.chdir dir

  # Re-open file handles
  @files_to_reopen.each do |file|
    file.reopen file.path, 'a+'
    file.sync = true
  end

  # Spring redirects STDOUT and STDERR to /dev/null
  # before we get here. This causes the `reopen` lines
  # below to fail because the handles are already
  # opened for write
  if defined?(Spring)
    $stdout.close
    $stderr.close
  end

  $stdout.reopen("#{dir}/log/scheduler_out.log", 'w')
  $stderr.reopen("#{dir}/log/scheduler_err.log", 'w')
end

before_fork

daemon_options = {
  multiple:  false,
  dir_mode:  :normal,
  dir:       File.join(dir, 'tmp', 'pids'),
  backtrace: true
}

Daemons.run_proc('scheduler', daemon_options) do

  after_fork(dir)

  require File.join(dir, 'config', 'environment')

  Rails.logger.info 'Scheduler started.'
  at_exit do
    Rails.logger.info 'Scheduler stopped.'
  end

  begin
    Scheduler.threads
  rescue Interrupt
    nil
  end
end

{'fraction_non_alphanumeric': 0.12479311486262827, 'fraction_numerical': 0.005296259516716319, 'mean_word_length': 3.7816455696202533, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, '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': '6095281', 'n_tokens_mistral': 1109, 'n_tokens_neox': 1067, 'n_words': 274}