Skip to content

Backgrounding Libraries

Janko Marohnić edited this page Feb 5, 2019 · 5 revisions

Shrine requires you to define and call background jobs directly, which means you can integrate Shrine with any backgrounding library:

First you need to load the plugin:

Shrine.plugin :backgrounding

ActiveJob

Shrine::Attacher.promote { |data| PromoteJob.perform_later(data) }
Shrine::Attacher.delete { |data| DeleteJob.perform_later(data) }
class PromoteJob < ActiveJob::Base
  def perform(data)
    Shrine::Attacher.promote(data)
  end
end
class DeleteJob < ActiveJob::Base
  def perform(data)
    Shrine::Attacher.delete(data)
  end
end

Sidekiq

Shrine::Attacher.promote { |data| PromoteJob.perform_async(data) }
Shrine::Attacher.delete { |data| DeleteJob.perform_async(data) }
class PromoteJob
  include Sidekiq::Worker
  def perform(data)
    Shrine::Attacher.promote(data)
  end
end
class DeleteJob
  include Sidekiq::Worker
  def perform(data)
    Shrine::Attacher.delete(data)
  end
end

DelayedJob

Shrine::Attacher.promote { |data| Delayed::Job.enqueue PromoteJob.new(data) }
Shrine::Attacher.delete { |data| Delayed::Job.enqueue DeleteJob.new(data) }
PromoteJob = Struct.new(:data) do
  def perform
    Shrine::Attacher.promote(data)
  end
end
DeleteJob = Struct.new(:data) do
  def perform
    Shrine::Attacher.delete(data)
  end
end

Resque

Shrine::Attacher.promote { |data| Resque.enqueue(PromoteJob, data) }
Shrine::Attacher.delete { |data| Resque.enqueue(DeleteJob, data) }
class PromoteJob
  def self.perform(data)
    Shrine::Attacher.promote(data)
  end
end
class DeleteJob
  def self.perform(data)
    Shrine::Attacher.delete(data)
  end
end

SuckerPunch

Shrine::Attacher.promote { |data| PromoteJob.perform_async(data) }
Shrine::Attacher.delete { |data| DeleteJob.perform_async(data) }
class PromoteJob
  include SuckerPunch::Job
  def perform(data)
    Shrine::Attacher.promote(data)
  end
end
class DeleteJob
  include SuckerPunch::Job
  def perform(data)
    Shrine::Attacher.delete(data)
  end
end

QueueClassic

Shrine::Attacher.promote { |data| QC.enqueue("PromoteJob.perform", data) }
Shrine::Attacher.delete { |data| QC.enqueue("DeleteJob.perform", data) }
class PromoteJob
  def self.perform(data)
    Shrine::Attacher.promote(data)
  end
end
class DeleteJob
  def self.perform(data)
    Shrine::Attacher.delete(data)
  end
end

RocketJob

Shrine::Attacher.promote { |data| PromoteJob.create!(data: data) }
Shrine::Attacher.delete { |data| DeleteJob.create!(data: data) }
class PromoteJob < RocketJob::Job
  key :data, Hash

  def perform
    Shrine::Attacher.promote(data)
  end
end
class DeleteJob < RocketJob::Job
  key :data, Hash

  def perform
    Shrine::Attacher.delete(data)
  end
end

Que

Shrine::Attacher.promote { |data| PromoteJob.enqueue(data) }
Shrine::Attacher.delete { |data| DeleteJob.enqueue(data) }
class PromoteJob < Que::Job
  def run(data)
    Shrine::Attacher.promote(data)
  end
end
class DeleteJob < Que::Job
  def run(data)
    Shrine::Attacher.delete(data)
  end
end

Qu

Shrine::Attacher.promote { |data| PromoteJob.create(data) }
Shrine::Attacher.delete { |data| DeleteJob.create(data) }
class PromoteJob < Qu::Job
  def initialize(data)
    @data = data
  end

  def perform
    Shrine::Attacher.promote(@data)
  end
end
class DeleteJob < Qu::Job
  def initialize(data)
    @data = data
  end

  def perform
    Shrine::Attacher.delete(@data)
  end
end