<commit_before>require 'simple_states'
require 'travis/event'
require 'travis/hub/model/build'
require 'travis/hub/model/log'
require 'travis/hub/model/repository'

class Job < ActiveRecord::Base
  include SimpleStates, Travis::Event

  FINISHED_STATES = [:passed, :failed, :errored, :canceled]

  Job.inheritance_column = :unused

  belongs_to :repository
  belongs_to :build, polymorphic: true, foreign_key: :source_id, foreign_type: :source_type
  belongs_to :commit
  has_one    :log

  event :receive
  event :start,   after: :propagate
  event :finish,  after: :propagate, to: FINISHED_STATES # TODO should not allow canceled?
  event :cancel,  after: :propagate, if: :cancel?
  event :restart, after: :propagate, if: :restart?
  event :all, after: :notify

  serialize :config

  def config
    super || {}
  end

  def duration
    finished_at - started_at if started_at && finished_at
  end

  def finished?
    FINISHED_STATES.include?(state.try(:to_sym))
  end

  def restart?(*)
    config_valid?
  end

  def restart(*)
    reset_state
    log.clear
  end

  def cancel?(*)
    !finished?
  end

  def cancel(*)
    self.finished_at = Time.now
  end

  private

    def propagate(event, *args)
      build.send(:"#{event}!", *args)
    end

    def config_valid?
      !config[:'.result'].to_s.include?('error')
    end
end<commit_msg>Dont propagate finish event to build<commit_after>require 'simple_states'
require 'travis/event'
require 'travis/hub/model/build'
require 'travis/hub/model/log'
require 'travis/hub/model/repository'

class Job < ActiveRecord::Base
  include SimpleStates, Travis::Event

  FINISHED_STATES = [:passed, :failed, :errored, :canceled]

  Job.inheritance_column = :unused

  belongs_to :repository
  belongs_to :build, polymorphic: true, foreign_key: :source_id, foreign_type: :source_type
  belongs_to :commit
  has_one    :log

  event :receive
  event :start,   after: :propagate
  event :finish,  after: :propagate, to: FINISHED_STATES # TODO should not allow canceled?
  event :cancel,  after: :propagate, if: :cancel?
  event :restart, after: :propagate, if: :restart?
  event :all, after: :notify

  serialize :config

  def config
    super || {}
  end

  def duration
    finished_at - started_at if started_at && finished_at
  end

  def finished?
    FINISHED_STATES.include?(state.try(:to_sym))
  end

  def restart?(*)
    config_valid?
  end

  def restart(*)
    reset_state
    log.clear
  end

  def cancel?(*)
    !finished?
  end

  def cancel(*)
    self.finished_at = Time.now
  end

  private

    def propagate(event, *args)
      build.send(:"#{event}!", *args) unless build_already_canceled?(event)
    end

    def config_valid?
      !config[:'.result'].to_s.include?('error')
    end

    def build_already_canceled?(event)
      build.canceled? && event == 'finish'
    end
end
{'fraction_non_alphanumeric': 0.11446629213483146, 'fraction_numerical': 0.0, 'mean_word_length': 3.6858552631578947, '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': '489178', 'n_tokens_mistral': 1013, 'n_tokens_neox': 969, 'n_words': 248}