<commit_before>module EncodingJob::Statuses
  extend ActiveSupport::Concern
  
  included do
    enum status: [ :pending, :transcoding, :post_processing, :conformance_checking, :success, :failed ]
    
    scope :eligible_jobs, -> { where(status: [
      statuses[:pending],
      statuses[:transcoding],
      statuses[:post_processing],
      statuses[:conformance_checking]
    ]) }
  end
  
  module ClassMethods
    def transition
      eligible_jobs.each { |j| j.transition }
    end
  end
  
  def transition
    self.send("transition_from_#{status}")
  end
  
  private
  
  def transition_from_pending
    puts "from pending"
  end
  
  def transition_from_transcoding
    puts "from transcoding"
  end
  
  def transition_from_post_processing
    puts "from post processing"
  end
  
  def transition_from_conformance_checking
    puts "from conformance checking"
  end
end<commit_msg>Basic state machine logic for EncodingJob<commit_after>module EncodingJob::Statuses
  extend ActiveSupport::Concern
  
  included do
    enum status: [ :pending, :transcoding, :post_processing, :conformance_checking, :success, :failed ]
    
    scope :eligible_jobs, -> { where(status: [
      statuses[:pending],
      statuses[:transcoding],
      statuses[:post_processing],
      statuses[:conformance_checking]
    ]) }
  end
  
  module ClassMethods
    def transition
      eligible_jobs.each { |j| j.transition }
    end
  end
  
  def transition
    self.send("transition_from_#{status}")
  end
  
  private
  
  # State machine definition, this describes all possible transitions
  def transition_from_pending
    puts "from pending"
    if requires_transcoding?
      enter_transcoding
    elsif requires_post_processing?
      enter_post_processing
    else
      enter_conformance_checking
    end
  end
  
  def transition_from_transcoding
    puts "from transcoding"
    if did_complete_transcoding?
      enter_pending
    elsif did_fail_transcoding?
      enter_failed
    end
  end
  
  def transition_from_post_processing
    puts "from post processing"
    if did_complete_post_processing?
      enter_pending
    elsif did_fail_post_processing?
      enter_failed
    end
  end
  
  def transition_from_conformance_checking
    puts "from conformance checking"
    if did_finish_conformance_checking?
      enter_success
    end
  end
  
  # Status checking methods, used to determine preconditions for transitions
  def requires_transcoding?
  end

  def requires_post_processing?
  end

  def did_complete_transcoding?
  end
  
  def did_fail_transcoding?
  end
  
  def did_complete_post_processing?
  end
  
  def did_fail_post_processing?
  end
  
  def did_finish_conformance_checking?
  end
  
  # Entering a state triggers these actions
  def enter_pending
  end

  def enter_transcoding
  end
  
  def enter_post_processing
  end
  
  def enter_conformance_checking
  end
  
  def enter_failed
  end
  
  def enter_success
  end
end
{'fraction_non_alphanumeric': 0.053076402974983096, 'fraction_numerical': 0.0, 'mean_word_length': 3.2453371592539453, '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': '4882284', 'n_tokens_mistral': 923, 'n_tokens_neox': 898, 'n_words': 219}