Sha256: d523ad7cc590e067339a44756c8f8274f281926907bdd09f8a6d1a11e459bf0c

Contents?: true

Size: 780 Bytes

Versions: 6

Compression:

Stored size: 780 Bytes

Contents

class QueuedTask < ActiveRecord::Base
  validates :klass, presence: true

  serialize :data, Hash

  scope :locked, -> { where(locked: true) }
  scope :not_locked, -> { where('locked = 0 OR locked IS NULL') }

  def lock!
    update_attribute :locked, true
  end

  def unlock!
    update_attribute :locked, false
  end

  def run!
    klass.constantize.new(data).run!
  end

  def self.queue(klass, data)
    create!(klass: klass, data: data)
  end

  def self.queue_unless_already_queued(klass, data)
    self.queue(klass, data) if where(klass: klass).where('data = ?', data.to_yaml).empty?
  end

  def dequeue!
    destroy
  end

  def process!
    lock!

    begin
      run!
      dequeue!
    rescue
      raise
    ensure
      unlock! unless destroyed?
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
udongo-1.0.4 app/models/queued_task.rb
udongo-1.0.3 app/models/queued_task.rb
udongo-1.0.2 app/models/queued_task.rb
udongo-1.0.1 app/models/queued_task.rb
udongo-1.0.0 app/models/queued_task.rb
udongo-0.1.0 app/models/queued_task.rb