<commit_before>module Sprat
  class Job

    @queue = :test_jobs
    
    attr_accessor :settings, :id, :spreadsheet, :worksheet, :host, :local, :status, :reason, :results, :created

    def initialize(app_settings = SpratTestRunner.settings)
      @settings = app_settings
      @status = "Pending"
      @results = []
    end

    def self.var_names
      ['spreadsheet', 'worksheet', 'host', 'local', 'status', 'reason', 'results', 'created']
    end
    
    def self.load(id)
      job = new
      job.id = id
      Job.var_names.each do |name|
        job.instance_variable_set("@#{name}", job.settings.redis.hget("jobs:#{job.id}", name))
      end
      job
    end

    def get_id
      if @id == nil
        @id = @settings.redis.incr("jobs.next.id")
        @settings.redis.lpush("jobs", @id)
      end
      @id
    end

    def save
      @id = get_id
      Job.var_names.each do |name|
        value = instance_variable_get("@#{name}")
        @settings.redis.hset("jobs:#{@id}", name, value.to_s)
      end
    end

    def local?
      return @local.to_s != "0" 
    end

    def exec()

      source = Source.new(@spreadsheet, @worksheet, @settings.username, @settings.password)

      unless local?
        source.update_status("Running", "Status")
        source.update_status(Time.now.to_s, "Started At")    
        source.update_status("", "Finished At")    
        source.reset_spreadsheet()
      end

      @results = []
      @status = "Running"
      save

      tester = Tester.new(@settings)

      begin
        tester.run(source, host)
        @status = tester.status
        @reason = tester.reason
      rescue => e
        @status = "FAIL (" + e.message + ")"
        @reason = e.message + e.backtrace.inspect
      end

      @results = JSON.generate(tester.get_results)
      save

      unless local?<commit_msg>Moved final parse of results into exception handling.<commit_after>module Sprat
  class Job

    @queue = :test_jobs
    
    attr_accessor :settings, :id, :spreadsheet, :worksheet, :host, :local, :status, :reason, :results, :created

    def initialize(app_settings = SpratTestRunner.settings)
      @settings = app_settings
      @status = "Pending"
      @results = []
    end

    def self.var_names
      ['spreadsheet', 'worksheet', 'host', 'local', 'status', 'reason', 'results', 'created']
    end
    
    def self.load(id)
      job = new
      job.id = id
      Job.var_names.each do |name|
        job.instance_variable_set("@#{name}", job.settings.redis.hget("jobs:#{job.id}", name))
      end
      job
    end

    def get_id
      if @id == nil
        @id = @settings.redis.incr("jobs.next.id")
        @settings.redis.lpush("jobs", @id)
      end
      @id
    end

    def save
      @id = get_id
      Job.var_names.each do |name|
        value = instance_variable_get("@#{name}")
        @settings.redis.hset("jobs:#{@id}", name, value.to_s)
      end
    end

    def local?
      return @local.to_s != "0" 
    end

    def exec()

      source = Source.new(@spreadsheet, @worksheet, @settings.username, @settings.password)

      unless local?
        source.update_status("Running", "Status")
        source.update_status(Time.now.to_s, "Started At")    
        source.update_status("", "Finished At")    
        source.reset_spreadsheet()
      end

      @results = []
      @status = "Running"
      save

      tester = Tester.new(@settings)

      begin
        tester.run(source, host)
        @status = tester.status
        @reason = tester.reason
        @results = JSON.generate(tester.get_results)
      rescue => e
        @status = "FAIL (" + e.message + ")"
        @reason = e.message + e.backtrace.inspect
      end

      save

      unless local?
{'fraction_non_alphanumeric': 0.13449197860962567, 'fraction_numerical': 0.0005347593582887701, 'mean_word_length': 2.1892583120204603, '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': '5471402', 'n_tokens_mistral': 1241, 'n_tokens_neox': 1198, 'n_words': 266}