Skip to content

Backgrounding Libraries

Nick Giancola edited this page Feb 16, 2022 · 5 revisions

Shrine's backgrounding plugin 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_block do
  PromoteJob.perform_later(self.class.name, record, name, file_data)
end
Shrine::Attacher.destroy_block do
  DestroyJob.perform_later(self.class.name, data)
end
class PromoteJob < ActiveJob::Base
  def perform(attacher_class, record, name, file_data)
    attacher_class = Object.const_get(attacher_class)

    attacher = attacher_class.retrieve(model: record, name: name, file: file_data)
    attacher.atomic_promote
  end
end
class DestroyJob < ActiveJob::Base
  def perform(attacher_class, data)
    attacher_class = Object.const_get(attacher_class)

    attacher = attacher_class.from_data(data)
    attacher.destroy
  end
end

Sidekiq

Shrine::Attacher.promote_block do
  PromoteJob.perform_async(self.class.name, record.class.name, record.id, name.to_s, file_data)
end
Shrine::Attacher.destroy_block do
  DestroyJob.perform_async(self.class.name, data)
end
class PromoteJob
  include Sidekiq::Worker

  def perform(attacher_class, record_class, record_id, name, file_data)
    attacher_class = Object.const_get(attacher_class)
    record         = Object.const_get(record_class).find(record_id)

    attacher = attacher_class.retrieve(model: record, name: name, file: file_data)
    attacher.atomic_promote
  end
end
class DestroyJob
  include Sidekiq::Worker

  def perform(attacher_class, data)
    attacher_class = Object.const_get(attacher_class)

    attacher = attacher_class.from_data(data)
    attacher.destroy
  end
end

Resque

Shrine::Attacher.promote_block do
  Resque.enqueue(PromoteJob, self.class.name, record.class.name, record.id, name, file_data)
end
Shrine::Attacher.destroy_block do
  Resque.enqueue(DestroyJob, self.class.name, data)
end
class PromoteJob
  def self.perform(attacher_class, record_class, record_id, name, file_data)
    attacher_class = Object.const_get(attacher_class)
    record         = Object.const_get(record_class).find(record_id)

    attacher = attacher_class.retrieve(model: record, name: name, file: file_data)
    attacher.atomic_promote
  end
end
class DestroyJob
  def self.perform(attacher_class, data)
    attacher_class = Object.const_get(attacher_class)

    attacher = attacher_class.from_data(data)
    attacher.destroy
  end
end

SuckerPunch

Shrine::Attacher.promote_block do
  PromoteJob.perform_async(self.class.name, record.class.name, record.id, name, file_data)
end
Shrine::Attacher.destroy_block do
  DestroyJob.perform_async(self.class.name, data)
end
class PromoteJob
  include SuckerPunch::Job

  def perform(attacher_class, record_class, record_id, name, file_data)
    attacher_class = Object.const_get(attacher_class)
    record         = Object.const_get(record_class).find(record_id)

    attacher = attacher_class.retrieve(model: record, name: name, file: file_data)
    attacher.atomic_promote
  end
end
class DestroyJob
  include SuckerPunch::Job

  def perform(attacher_class, data)
    attacher_class = Object.const_get(attacher_class)

    attacher = attacher_class.from_data(data)
    attacher.destroy
  end
end

QueueClassic

Shrine::Attacher.promote_block do
  QC.enqueue("PromoteJob.perform", self.class.name, record.class.name, record.id, name, file_data)
end
Shrine::Attacher.destroy_block do
  QC.enqueue("DestroyJob.perform", self.class.name, data)
end
class PromoteJob
  def self.perform(attacher_class, record_class, record_id, name, file_data)
    attacher_class = Object.const_get(attacher_class)
    record         = Object.const_get(record_class).find(record_id)

    attacher = attacher_class.retrieve(model: record, name: name, file: file_data)
    attacher.atomic_promote
  end
end
class DestroyJob
  def self.perform(attacher_class, data)
    attacher_class = Object.const_get(attacher_class)

    attacher = attacher_class.from_data(data)
    attacher.destroy
  end
end

RocketJob

Shrine::Attacher.promote_block do
  PromoteJob.create!(
    attacher_class: self.class.name,
    record_class: record.class.name,
    record_id: record.id,
    name: name,
    file_data: file_data,
  )
end
Shrine::Attacher.destroy_block do
  DestroyJob.create!(
    attacher_class: self.class.name,
    data: data,
  )
end
class PromoteJob < RocketJob::Job
  field :attacher_class, type: String
  field :record_class,   type: String
  field :record_id,      type: String
  field :name,           type: String
  filed :file_data,      type: Hash

  def perform
    attacher_class = Object.const_get(attacher_class)
    record         = Object.const_get(record_class).find(record_id)

    attacher = attacher_class.retrieve(model: record, name: name, file: file_data)
    attacher.atomic_promote
  end
end
class DestroyJob < RocketJob::Job
  field :attacher_class, type: String
  field :data,           type: Hash

  def perform
    attacher_class = Object.const_get(attacher_class)

    attacher = attacher_class.from_data(data)
    attacher.destroy
  end
end

Que

Shrine::Attacher.promote_block do
  PromoteJob.enqueue(self.class.name record.class.name, record.id, name, file_data)
end
Shrine::Attacher.destroy_block do
  DestroyJob.enqueue(self.class.name, data)
end
class PromoteJob < Que::Job
  def run(attacher_class, record_class, record_id, name, file_data)
    attacher_class = Object.const_get(attacher_class)
    record         = Object.const_get(record_class).find(record_id)

    attacher = attacher_class.retrieve(model: record, name: name, file: file_data)
    attacher.atomic_promote
  end
end
class DestroyJob < Que::Job
  def run(attacher_class, data)
    attacher_class = Object.const_get(attacher_class)

    attacher = attacher_class.from_data(data)
    attacher.destroy
  end
end

Qu

Shrine::Attacher.promote_block do
  PromoteJob.create(self.class.name, record.class.name, record.id, name, file_data)
end
Shrine::Attacher.destroy_block do
  DestroyJob.create(self.class.name, data)
end
class PromoteJob < Qu::Job
  extend Dry::Initializer # dry-initializer gem

  param :attacher_class
  param :record_class
  param :record_id
  param :name
  param :file_data

  def perform
    attacher_class = Object.const_get(attacher_class)
    record         = Object.const_get(record_class).find(record_id)

    attacher = attacher_class.retrieve(model: record, name: name, file: file_data)
    attacher.atomic_promote
  end
end
class DestroyJob < Qu::Job
  extend Dry::Initializer # dry-initializer gem

  param :attacher_class
  param :data

  def perform
    attacher_class = Object.const_get(attacher_class)

    attacher = attacher_class.from_data(data)
    attacher.destroy
  end
end