Sha256: 229e3da54b6fdbfaf9008c08e5df0bc9724dd0c6d6d9959fea4971572da6b136

Contents?: true

Size: 1.92 KB

Versions: 3

Compression:

Stored size: 1.92 KB

Contents

class Job < ActiveRecord::Base

  validates :name, :started_at, presence: true
  belongs_to :error

  default_scope -> { order(started_at: :desc) }



  def self.started_before(time)
    where arel_table[:started_at].lteq time
  end

  def self.record(job_name)
    job = Job.create!(name: job_name, started_at: Time.now)
    begin
      exception = nil

      Houston.reconnect do
        yield
      end

    rescue SocketError,
           Errno::ECONNREFUSED,
           Errno::ETIMEDOUT,
           Faraday::ConnectionFailed,
           Faraday::SSLError,
           Faraday::HTTP::ServerError,
           Faraday::HTTP::Unauthorized,
           Faraday::TimeoutError,
           Rugged::NetworkError,
           Unfuddle::ConnectionError,
           Octokit::BadGateway,
           Octokit::InternalServerError,
           Net::OpenTimeout,
           exceptions_wrapping(PG::ConnectionBad)

      # Note that the job failed, but do not report _these_ exceptions
      exception = $!

    rescue Exception # rescues StandardError by default; but we want to rescue and report all errors

      # Report all other exceptions
      exception = $!
      Houston.report_exception($!, parameters: {job_id: job.id, job_name: job_name})

    ensure
      begin
        Houston.reconnect do
          job.finish! exception
        end
      rescue Exception # rescues StandardError by default; but we want to rescue and report all errors
        Houston.report_exception($!, parameters: {job_id: job.id, job_name: job_name})
      end
    end
  end



  def exception=(exception)
    self.error = Error.find_or_create_for_exception(exception) if exception
  end

  def finish!(exception)
    update_attributes! finished_at: Time.now, succeeded: exception.nil?, exception: exception
  end

  def duration
    return nil unless finished?
    finished_at - started_at
  end

  def finished?
    finished_at.present?
  end

  def in_progress?
    finished_at.nil?
  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
houston-core-0.7.0.beta3 app/models/job.rb
houston-core-0.7.0.beta2 app/models/job.rb
houston-core-0.7.0.beta app/models/job.rb