- Clone repo
docker-compose build
docker-compose up
- Create and migrate the database in the docker container:
docker exec active-job-demo_web_1 rails db:create db:migrate
Check out the Rails ActiveJob docs here https://edgeguides.rubyonrails.org/active_job_basics.html For image manipulation, check out the Mini Magick docs here https://github.com/minimagick/minimagick For info on ActiveStorage's image manipulation wrapper around MiniMagick look here https://edgeapi.rubyonrails.org/classes/ActiveStorage/Variant.html
Create an app that allows the user to upload images that will be manupulated asyncronouly from a job queue.
- Add a job called image_converter
- Write a job that will change the name of the photo to anything you like.
- Call the job in the create action.
- The new photo name should show up on the index page.
- Use minimagick to resize the photo to 100x100 pixels in the image_converter job.
- The new resized photo should show up on the index page.
HINT: You will have to make sure that the index page shows the changed photo and not the original.
Use something like
photo.images.first.variant(resize: "100x100")
in the job. You will also have to use.variant(resize: "100x100")
when you display the image. Read the docs on.variant
to understnad why. Basically, rails keeps a variant in ActiveStorage under a hash key that is based on the options you pass to.variant
.
- Set the queue for your job as "urgent".
- Call the job after a 10 second delay. This will appear to happen instantly, because the index page actually converts the image. You can confirm that the job was delayed by waiting for it to show up in the log after the index page renders.
- Before the photo is enqueued, check that the file extention is
.jpeg
,.jpg
, or.png
. Raise an exception if it is not to prevent it from being enqueued.
- Destroy the photo if an
ActiveStorage::InvariableError
error is received. You can test this by uploading a pdf, or other document that you rename to .jpg.