Sha256: c7f1e50ebdaf73fbfb9b9e2765f3173d103f310595a19212d810f4e1d020d488

Contents?: true

Size: 1.81 KB

Versions: 21

Compression:

Stored size: 1.81 KB

Contents

module Extface
  class Job < ActiveRecord::Base
    attr_accessor :thread
    
    belongs_to :device, inverse_of: :jobs
    
    scope :active, ->{ where(arel_table[:completed_at].eq(nil).and(arel_table[:failed_at].eq(nil)).and(arel_table[:started_at].not_eq(nil))) }
    scope :completed, ->{ where(arel_table[:completed_at].not_eq(nil)) }
    scope :failed, ->{ where(arel_table[:failed_at].not_eq(nil)) }
    
    def runtime(description = nil)
      update!(description: description, started_at: Time.now)
      begin
        raise 'No device assigned' unless device.present?
        raise 'No driver configured' unless device.driver.present?
        if device.driver.set_job(self)
          yield device.driver
          complete!
        else
          raise device.driver.errors.full_messages.join(', ')
        end
      rescue => e
        STDERR.puts e.message
        e.backtrace.each do |line|
          p line
        end
        failed! e.message
      end
    end
    
    def complete!
      self.completed_at = Time.now
      save!
      notify("Job #{self.id} completed!")
    end
    
    def completed?
      !!completed_at
    end
    
    def connected!
      unless connected?
        self.connected_at = Time.now
        save! unless Rails.env.test?
        notify("Job #{self.id} device connected!")
      end
    end
    
    def connected?
      !!connected_at?
    end
    
    def failed!(message)
      self.error = message
      self.failed_at = Time.now
      save!
      notify(message)
      notify("Job #{self.id} failed!")
    end
    
    def failed?
      !!failed_at
    end
    
    def notify(message)
      Extface.redis_block do |r|
        r.publish(self.id, message)
      end
    end
    
    def rpush(buffer)
      Extface.redis_block do |r|
        r.rpush self.id, buffer
      end
    end
    
  end
end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
extface-0.7.3 app/models/extface/job.rb
extface-0.7.2 app/models/extface/job.rb
extface-0.7.1 app/models/extface/job.rb
extface-0.7.0 app/models/extface/job.rb
extface-0.6.9 app/models/extface/job.rb
extface-0.6.8 app/models/extface/job.rb
extface-0.6.7 app/models/extface/job.rb
extface-0.6.6 app/models/extface/job.rb
extface-0.6.5 app/models/extface/job.rb
extface-0.6.4 app/models/extface/job.rb
extface-0.6.3 app/models/extface/job.rb
extface-0.6.2 app/models/extface/job.rb
extface-0.6.1 app/models/extface/job.rb
extface-0.6.0 app/models/extface/job.rb
extface-0.5.9 app/models/extface/job.rb
extface-0.5.8 app/models/extface/job.rb
extface-0.5.7 app/models/extface/job.rb
extface-0.5.6 app/models/extface/job.rb
extface-0.5.5 app/models/extface/job.rb
extface-0.5.4b app/models/extface/job.rb