<commit_before>require "cognizant/process/actions/start"
require "cognizant/process/actions/stop"
require "cognizant/process/actions/restart"
require "cognizant/system"

module Cognizant
  class Process
    module Actions
      include Cognizant::Process::Actions::Start
      include Cognizant::Process::Actions::Stop
      include Cognizant::Process::Actions::Restart

      private

      def execute_action(result_handler, options)
        before_command = options[:before]
        command        = options[:command]
        after_command  = options[:after]
        signals        = options[:signals]
        timeout        = options[:timeout] || 10

        # TODO: Works well but can some refactoring make it more declarative?
        @action_thread = Thread.new do
          result = false
          queue = Queue.new
          thread = Thread.new do
            if before_command and not success = run(before_command).succeeded?
              queue.push(success)
              Thread.exit
            end

            if (command and success = run(command, options) and success.succeeded?)
              run(after_command) if after_command
              queue.push(success)
              Thread.exit
            end

            # If the caller has attempted to set signals, then it can handle it's result.
            if success = send_signals(signals: signals, timeout: timeout)
              run(after_command) if after_command
              queue.push(success)
              Thread.exit
            end

            queue.push(false)
            Thread.exit
          end

          timeout_left = timeout + 1
          while (timeout_left -= 1) > 0 do
            # If there is something in the queue, we have the required result.
            if result = queue.pop
              # Rollback the pending skips, since we finished before timeout.
              skip_ticks_for(-timeout_left)
              break
            end
            sleep 1
          end

          # Kill the nested thread.
          thread.kill
<commit_msg>Simply execute action method for lesser comlpexity.<commit_after>require "cognizant/process/actions/start"
require "cognizant/process/actions/stop"
require "cognizant/process/actions/restart"
require "cognizant/system"

module Cognizant
  class Process
    module Actions
      include Cognizant::Process::Actions::Start
      include Cognizant::Process::Actions::Stop
      include Cognizant::Process::Actions::Restart

      private

      def execute_action(result_handler, options)
        before_command = options[:before]
        command        = options[:command]
        after_command  = options[:after]
        signals        = options[:signals]
        timeout        = options[:timeout] || 10

        # TODO: Works well but can some refactoring make it more declarative?
        @action_thread = Thread.new do
          result = false
          queue = Queue.new
          thread = Thread.new do
            # If before_command succeeds, we move to the next command.
            (before_command and not success = run(before_command).succeeded?) or
            # If the command is available and it succeeds, we stop here.
            (command and success = run(command, options) and success.succeeded?) or
            # As a last try, check for signals. If the action has set signals, then it can handle it's result.
            (success = send_signals(signals: signals, timeout: timeout))

            run(after_command) if success and after_command
            queue.push(success)
            Thread.exit
          end

          timeout_left = timeout + 1
          while (timeout_left -= 1) > 0 do
            # If there is something in the queue, we have the required result.
            if result = queue.pop
              # Rollback the pending skips, since we finished before timeout.
              skip_ticks_for(-timeout_left)
              break
            end
            sleep 1
          end

          # Kill the nested thread.
          thread.kill

{'fraction_non_alphanumeric': 0.06893977103036336, 'fraction_numerical': 0.0029865604778496766, 'mean_word_length': 1.9486426999266324, 'pattern_counts': {'":': 0, '<': 3, '<?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': '950071', 'n_tokens_mistral': 1038, 'n_tokens_neox': 998, 'n_words': 322}