Using Gearman with Catalyst to Create a Simple Image Thumbnailer

SYNOPSIS

Gearman is a distributed job queue system that excels in doing things quickly, and asynchronously run job processes for things that need batch processesing, or those which you don't want your web application having to deal with. It's quick, and good for things like thumbnailing, which we will be talking about today.

The Process

So, here's how this goes down:

The web browser uploads the image to your Catalyst app. Catalyst writes out the image to your given directory on the file system, maybe inserts a pointer to that in the database, and then at the same time, creates a job in the Gearman worker pool to create a thumbnail of said image. Gearman either queues it up because it's working on other images, or it takes care of it right away if it's just twiddling its thumbs. Then, presto, you have a thumbnail of your image(s)!

Again, a small ascii flowchart of this process should remove all questions you have of this process:

This tells our Gearman::Worker object that our server (a list of server IPs can be passed here) resides at 127.0.0.1, and registers the job "thumbinate" with the associated job defined in this class, with the proper arguments passed.

And that's that. You now have the ability to create thumbnails cleanly and smoothly with Gearman and Catalyst.

Final Notes

Note that some of you may have done this with TheSchwartz. Which is all good and fine. However, it's concerned more about reliability than speed. Gearman doesn't check to make sure jobs got done successfully, but it is much more suited for creating thumbnails because of speed. Thumbnails can also be trivially recreated if something fails.