<commit_before>require 'active_support/core_ext/hash/except'
require 'travis/support/instrumentation'
require 'travis/services/base'

module Travis
  module Services
    class UpdateJob < Base
      extend Travis::Instrumentation

      register :update_job

      EVENT = [:receive, :start, :finish, :reset]

      def run
        if job.canceled? && event != :reset
          # job is canceled, so we ignore events other than reset
          # and we send cancel event to the worker, it may not get
          # the first one
          cancel_job_in_worker
        else
          Metriks.timer("update_job.#{event}").time do
            ActiveRecord::Base.connection.begin_db_transaction
            ActiveRecord::Base.connection.execute('SET TRANSACTION ISOLATION LEVEL SERIALIZABLE')
            job.source.lock!
            job.send(:"#{event}!", data.except(:id))
            ActiveRecord::Base.connection.commit_db_transaction
          end
        end
      end
      instrument :run

      def job
        @job ||= Job::Test.find(data[:id])
      end

      def data
        @data ||= begin
          data = params[:data].symbolize_keys
          # TODO remove once workers send the state
          data[:state] = { 0 => :passed, 1 => :failed }[data.delete(:result)] if data.key?(:result)
          data
        end<commit_msg>rollback the transaction if something bad happens<commit_after>require 'active_support/core_ext/hash/except'
require 'travis/support/instrumentation'
require 'travis/services/base'

module Travis
  module Services
    class UpdateJob < Base
      extend Travis::Instrumentation

      register :update_job

      EVENT = [:receive, :start, :finish, :reset]

      def run
        if job.canceled? && event != :reset
          # job is canceled, so we ignore events other than reset
          # and we send cancel event to the worker, it may not get
          # the first one
          cancel_job_in_worker
        else
          Metriks.timer("update_job.#{event}").time do
            begin
              ActiveRecord::Base.connection.begin_db_transaction
              ActiveRecord::Base.connection.execute('SET TRANSACTION ISOLATION LEVEL SERIALIZABLE')
              job.source.lock!
              job.send(:"#{event}!", data.except(:id))
              ActiveRecord::Base.connection.commit_db_transaction
            rescue => e
              ActiveRecord::Base.connection.rollback_db_transaction
              raise
            end
          end
        end
      end
      instrument :run

      def job
        @job ||= Job::Test.find(data[:id])
      end

      def data
        @data ||= begin
          data = params[:data].symbolize_keys
          # TODO remove once workers send the state
          data[:state] = { 0 => :passed, 1 => :failed }[data.delete(:result)] if data.key?(:result)
          data
        end
{'fraction_non_alphanumeric': 0.09503843466107617, 'fraction_numerical': 0.001397624039133473, 'mean_word_length': 2.1255458515283845, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 8, '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': '4917916', 'n_tokens_mistral': 789, 'n_tokens_neox': 775, 'n_words': 211}