Thumbnailer rails5/16/2023 Not every Rails application needs background image processing.īut there are situations when it is a really good idea. That’s 8 times slower than with background processing enabled! Conclusion I ran some tests without using Sidekiq to asynchronously process images requests and took around 120ms. Time_total: %sWe will send our image in the binary form using a -F flag. I will create a file curl-format.txt with the following content and use it later as an argument. Now, let’s see how we are doing on the performance side of things.įirst, a little trick to get the response time from curl. The goal here is to get a 200 response and afterward end up with four images in the public/uploads folder: the original image and 3 copies resized according to the rules from ImageUplaoder.Īlso, you will see that PromoteJob has been scheduled and successfully run in Sidekiq logs. In my case, it’s a file I called example.pngĬurl -X POST -i -F localhost:3000/uploads We have an endpoint where we can send images and they will be processed according to our specifications.įirst, we need an image to upload. Run Rails server on the other terminal tab:Īnd there you go! Our application is functional and running :) I will use an official docker image but you can run a Redis from your system if you prefer :)ĭocker run -name my-redis -d -publish 6379:6379 redis Now let’s run Redis server which Sidekiq uses to store and retrieve jobs. The last thing is to implement a “create action” in the UploadsController. This job will be put into Redis on the promotion event and later executed in Sidekiq. We need to implement a PromoteJob that we used in our initializer. Images now can be accessed through the image virtual field: Image processing is handled underneath by imagemagick: Next, let’s add a Shrine uploader with specifications on derivatives we want to generate. Promote event is run after our record “Upload” is persisted to the database. The last part registers a callback fired on the “promote event”. In this section, we will also declare plugins for integration with ActiveRecord, derivatives generation, and background processing. Rails g resource Upload image_data:jsonb & rails db:migrate We will create an uploads table with an image_data column which is essential for Shrine to work: Let’s use a resource generator from Rails CLI. Rails new shrine-uploader-api -api -database=postgresql & cd shrine-uploader-api & rails db:create You can see the full application on my Github. How does it work in practice? Step-by-step Guide I like it very much and recommend using it in your projects - it has truly great documentation that cannot be overstated. It’s a modern library with plugin design and is an alternative to gems like Carrierwave or Paperclip. We will use Sidekiq to schedule a job for image processing and immediately return a 200 status response.įor image upload, we will use Shrine. In this blog post, we will go through how to achieve fast response times while generating multiple versions of an image. So… What can be done about it? If you are like me, you want both to have a cake and eat it. What problem does it lead to? Image processing is a computationally intensive operation and the latency will inevitably drop. Often these images need to be resized and saved as multiple versions: for mobile, web, thumbnails, OG images, and other custom and non-standard formats needed by the Client. As Ruby on Rails developers, we often encounter the need to upload and store images provided by our users.
0 Comments
Leave a Reply. |