<commit_before>module Bosh::Director
  module Jobs
    class DBJob
      attr_reader :job_class, :task_id

      def initialize(job_class, task_id, args)
        unless job_class.kind_of?(Class) &&
            job_class <= Jobs::BaseJob
          raise DirectorError, "Invalid director job class `#{job_class}'"
        end
        raise DirectorError, "Invalid director job class `#{job_class}'. It should have `perform' method."  unless job_class.instance_methods(false).include?(:perform)
        @job_class = job_class
        @task_id = task_id
        @args = args
        @logger = Config.logger
        raise DirectorError, "Invalid director job class `#{job_class}'. It should specify queue value." unless queue_name
      end

      def perform
        if Models::Task.first(id: @task_id, state: 'queued').nil?
          raise DirectorError, "Cannot perform job for task #{@task_id} (not in 'queued' state)"
        end

        process_status = ForkedProcess.run do
          @job_class.perform(@task_id, *decode(encode(@args)))
        end

        if process_status.signaled?
          @logger.debug("Task #{@task_id} was terminated, marking as failed")
          fail_task
        end
      end

      def queue_name
        @job_class.instance_variable_get(:@queue) ||
            (@job_class.respond_to?(:queue) and @job_class.queue)
      end

      private

      def fail_task
        Models::Task.first(id: @task_id).update(state: 'error')
      end

      def encode(object)
        if MultiJson.respond_to?(:dump) && MultiJson.respond_to?(:load)
          MultiJson.dump object
        else
          MultiJson.encode object
        end
      end

      # Given a string, returns a Ruby object.
      def decode(object)
        return unless object
<commit_msg>Do not use logger in DBJob, does not work properly in multithreaded setup<commit_after>module Bosh::Director
  module Jobs
    class DBJob
      attr_reader :job_class, :task_id

      def initialize(job_class, task_id, args)
        unless job_class.kind_of?(Class) &&
            job_class <= Jobs::BaseJob
          raise DirectorError, "Invalid director job class `#{job_class}'"
        end
        raise DirectorError, "Invalid director job class `#{job_class}'. It should have `perform' method."  unless job_class.instance_methods(false).include?(:perform)
        @job_class = job_class
        @task_id = task_id
        @args = args
        raise DirectorError, "Invalid director job class `#{job_class}'. It should specify queue value." unless queue_name
      end

      def perform
        if Models::Task.first(id: @task_id, state: 'queued').nil?
          raise DirectorError, "Cannot perform job for task #{@task_id} (not in 'queued' state)"
        end

        process_status = ForkedProcess.run do
          @job_class.perform(@task_id, *decode(encode(@args)))
        end

        if process_status.signaled?
          puts "Task #{@task_id} was terminated, marking as failed"
          fail_task
        end
      end

      def queue_name
        @job_class.instance_variable_get(:@queue) ||
            (@job_class.respond_to?(:queue) and @job_class.queue)
      end

      private

      def fail_task
        Models::Task.first(id: @task_id).update(state: 'error')
      end

      def encode(object)
        if MultiJson.respond_to?(:dump) && MultiJson.respond_to?(:load)
          MultiJson.dump object
        else
          MultiJson.encode object
        end
      end

      # Given a string, returns a Ruby object.
      def decode(object)
        return unless object

{'fraction_non_alphanumeric': 0.09430803571428571, 'fraction_numerical': 0.0, 'mean_word_length': 2.4045584045584047, 'pattern_counts': {'":': 0, '<': 5, '<?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': '1780947', 'n_tokens_mistral': 1041, 'n_tokens_neox': 1017, 'n_words': 289}