<commit_before>require_relative '../../../../config/environment'
require 'emerald/api'

require 'sidekiq'
require 'emerald/api/models/job'
require 'emerald/api/workers/container_factory'

class JobWorker
  include Sidekiq::Worker

  def perform(job_id)
    @job = Job.find(job_id)

    # this is async
    @job.log_stream do |delivery_info, properties, payload|
      payload = JSON.parse(payload)
      log_line = payload['payload']['log'].strip
      @job.logs.create(content: log_line) if !log_line.empty?
    end

    f = ContainerFactory.new(@job, ENV['FLUENTD_URL'])
    volume_container, git_container, test_runner_container = f.create_containers

    start
    status_code = run_container(git_container)
    if status_code != 0
      stop(:error)
      return
    end

    status_code = run_container(test_runner_container)
    job_state = { 0 => :passed }.fetch(status_code, :failed)
    stop(job_state)
  rescue => e
    puts e
    puts e.backtrace
    stop(:error)
  ensure
    remove_containers(volume_container, git_container, test_runner_container)
  end

  private

  def start
    @job.update!(
      state: :running,
      started_at: Time.now
    )<commit_msg>tell user through the log that the job has errored<commit_after>require_relative '../../../../config/environment'
require 'emerald/api'

require 'sidekiq'
require 'emerald/api/models/job'
require 'emerald/api/workers/container_factory'

class JobWorker
  include Sidekiq::Worker

  def perform(job_id)
    @job = Job.find(job_id)

    # this is async
    @job.log_stream do |delivery_info, properties, payload|
      payload = JSON.parse(payload)
      log_line = payload['payload']['log'].strip
      @job.logs.create(content: log_line) if !log_line.empty?
    end

    f = ContainerFactory.new(@job, ENV['FLUENTD_URL'])
    volume_container, git_container, test_runner_container = f.create_containers

    start
    status_code = run_container(git_container)
    if status_code != 0
      @job.logs.create(
        content: "Job errored because git checkout has been unsuccessful (error code: #{status_code})"
      )
      stop(:error)
      return
    end

    status_code = run_container(test_runner_container)
    job_state = { 0 => :passed }.fetch(status_code, :failed)
    stop(job_state)
  rescue => e
    puts e
    puts e.backtrace
    @job.logs.create(
      content: "Job errored unexpectedly."
    )
    stop(:error)
  ensure
    remove_containers(volume_container, git_container, test_runner_container)
  end

  private

  def start
    @job.update!(
      state: :running,
      started_at: Time.now
    )
{'fraction_non_alphanumeric': 0.1088880338591766, 'fraction_numerical': 0.0015390534821085034, 'mean_word_length': 3.3261231281198005, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '6185573', 'n_tokens_mistral': 854, 'n_tokens_neox': 827, 'n_words': 185}