Sha256: 0f3ee73275b89e2baa7e2942b9cb60516114f597bca92bf8e8772c74486e68a7

Contents?: true

Size: 1.21 KB

Versions: 20

Compression:

Stored size: 1.21 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))) }
    scope :completed, ->{ where(arel_table[:completed_at].not_eq(nil)) }
    scope :failed, ->{ where(arel_table[:failed_at].not_eq(nil)) }
    
    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

20 entries across 20 versions & 1 rubygems

Version Path
extface-0.5.4 app/models/extface/job.rb
extface-0.5.3 app/models/extface/job.rb
extface-0.5.2 app/models/extface/job.rb
extface-0.5.1 app/models/extface/job.rb
extface-0.5.0 app/models/extface/job.rb
extface-0.4.9 app/models/extface/job.rb
extface-0.4.8 app/models/extface/job.rb
extface-0.4.7 app/models/extface/job.rb
extface-0.4.6k app/models/extface/job.rb
extface-0.4.6i app/models/extface/job.rb
extface-0.4.6h app/models/extface/job.rb
extface-0.4.6g app/models/extface/job.rb
extface-0.4.6f app/models/extface/job.rb
extface-0.4.6e app/models/extface/job.rb
extface-0.4.6d app/models/extface/job.rb
extface-0.4.6c app/models/extface/job.rb
extface-0.4.6b app/models/extface/job.rb
extface-0.4.6a app/models/extface/job.rb
extface-0.4.6 app/models/extface/job.rb
extface-0.4.5 app/models/extface/job.rb