shrinerb / shrine

File Attachment toolkit for Ruby applications

Home Page:https://shrinerb.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

NoMethodError: undefined method `promote' for Shrine::Attacher:Class

mohnstrudel opened this issue · comments

commented

My setup is almost identical with this one: #200
Still getting the error when using Shrine together with Sidekiq


Error

2019-09-18T15:41:14.735Z pid=22765 tid=ovvg25t29 WARN: NoMethodError: undefined method `promote' for Shrine::Attacher:Class
2019-09-18T15:41:14.735Z pid=22765 tid=ovvg25t29 WARN: /Users/anton.kostin/projects/rails_sandbox/freelance/transmogram/app/jobs/shrine_backgrounding/promote_job.rb:6:in `perform'

Uploader

require "image_processing/mini_magick"

class ImageUploader < Shrine
  #plugins and uploading logic

  plugin :processing # allows hooking into promoting
  plugin :versions   # enable Shrine to handle a hash of files
  plugin :delete_raw # delete processed files after uploading

  plugin :validation_helpers
  plugin :determine_mime_type

  Attacher.validate do
    validate_min_size 1, message: "must not be empty"
    validate_max_size 10.megabytes, message: "is too large (max is 10 MB)"
    validate_mime_type_inclusion %w[image/jpeg image/png image/tiff]
    validate_extension_inclusion %w[jpg jpeg png tiff tif]
  end

  process(:store) do |io, context|
    versions = { original: io } # retain original

    # download the uploaded file from the temporary storage
    io.download do |original|
      pipeline = ImageProcessing::MiniMagick.source(original)

      versions[:supersmall] = pipeline.resize_to_fill!(50, 50)
      versions[:small] = pipeline.resize_to_fill!(100, 100)
      versions[:medium] = pipeline.resize_to_fill!(269, 202)
      versions[:large] = pipeline.resize_to_fill!(870, 569)
    end

    versions
  end

  Attacher.promote do |data|
    ShrineBackgrounding::PromoteJob.perform_async(data)
  end
  Attacher.delete do |data|
    ShrineBackgrounding::DeleteJob.perform_async(data)
  end
end

Job

module ShrineBackgrounding
  class PromoteJob
    include Sidekiq::Worker

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

Initializer

require "shrine"
require "shrine/storage/file_system"

Shrine.storages = {
  cache: Shrine::Storage::FileSystem.new("public", prefix: "uploads/cache"), # temporary
  store: Shrine::Storage::FileSystem.new("public", prefix: "uploads"),       # permanent
}

Shrine.plugin :activerecord # or :activerecord
Shrine.plugin :cached_attachment_data # for retaining the cached file across form redisplays
Shrine.plugin :restore_cached_data # re-extract metadata when attaching a cached file
# Shrine.plugin :rack_file # for no n-Rails apps

Shrine.plugin :backgrounding

System configuration

Ruby version: ruby-2.6.3

Shrine version: shrine (2.19.3, 2.18.1)
Sidekiq version: sidekiq (6.0.0, 5.2.7)

Could you create a minimal self-contained example that reproduces the error? I’m not seeing how this error could happen 🤔

commented

Thank you for such a quick reply. I will try to spin up a docker container on the weekend.

Note that it doesn't need to be a Docker container, I was more thinking about a self-contained script which has only the necessary amount of plugins needed to reproduce the error. The point is just that I can run it on my machine, with steps e.g:

  1. run this sidekiq command
  2. run this ruby script
commented

Utterly strange, no matter what I did, at some point the error was gone and I could not reproduce it.
So, some few steps:

  • I put a byebug inside of my worker method (perform(data))
  • I moved worker class up the hierarchy (i.g. removing ShrineBackgrounding::) and then down again
  • I had two different versions of both sidekiq and shire, deleted and reinstalled, so just only one is listed now
  • inbetween a lot of restarts both, the rails server and the sidekiq server

I think the issue can be closed now.