In MediaGoblin, processing refers to the act of transforming an original media file in various ways to make it suitable to serve. For example, with images, we prepare resized versions for thumbnail and gallery views. With video, we capture a thumbnail frame, and transcode a medium-sized version for embedded viewing.
Normally, we process media as soon as we can after it's been uploaded to the site. Sometimes, we want to reprocess some media. There are a couple of reasons why this might happen:
The original processing attempt failed. This could be for lots of reasons: maybe a transcoding process was killed by a crazed sysadmin, or the file is corrupted, or there might even be a bug in MediaGoblin (crazy, I know!).
Right now, when this happens, the unprocessed media lives in the database forever, a zombie. Instead of that, we should periodically retry processing the media, when it makes sense. Maybe we'll have better luck next time; if we do, it'll make the user happy.
- Something has changed on the site such that we ought to reprocess media that has already been processed. Maybe the administrator changed the size of thumbnail views, or in the future the MediaGoblin code will use a different audio codec. For an event like this, we need to reprocess all the affected existing media to make sure we can effectively serve them in the new way. These events should only take place when a site administrator requests it, and maybe when the site configuration changes to demand it.
Brett plans to work on this. If you want to help, get in touch! This is bug #420.
When should we try to reprocess?
If we're reprocessing media because previous attempts failed, we're likely to be more or less successful depending on why we failed. If we failed because the machine was low on memory or disk at the time, reprocessing stands a good chance of succeeding. If we failed because the media is corrupt, reprocessing will never work unless some code has changed in the meantime.
TODO: We should collect known cases of when processing failed, what it looked like. That will help us write code to determine why processing failed, and whether or not it's worthwhile to retry. (maybe this bug is a good start bug#438 ?)
When should we start reprocessing?
There are two forces pushing us in different directions on this. On the one hand, the more often we retry, the sooner the user's media will appear on the site, which makes them happy. On the other hand, if we retry so often that not much can change between different attempts, we're just wasting computing resources to little end. This could hurt our general site performance on deployments without resources to spare, like SheevaPlugs or Raspberry Pi systems.
Like scheduling code in GNU/Linux, there are a million different ways we could approach this, and no one system is going to be perfect for every site. We should instead strive to give hosts the tools they need to easily configure MediaGoblin according to their needs and their desires.
The first piece of this puzzle is to make full use of Celery's task routing capabilities. Each task should use an exchange that indicates at least:
- the media type
- whether this processing is for
- a new upload
- retry after failed processing
- reprocessing at administrator request
With this framework in place, a host has the capability to configure Celery with different worker pools for each of these exchanges depending on their needs and preferences.
However, Celery doesn't handle scheduling of tasks outside the constraints of worker pools. It's up to us to decide, and write in the code, issues like how long to wait between reprocessing attempts and when to give up completely. (For version 1, I'm planning an exponential backoff algorithm with a maximum wait of 1 day. TODO: Should there be different configuration knobs for each media type? That's a lot more complexity, but it's pretty hard to argue against the idea that expectations for processing ASCII art should be different from processing video.) Key values should be stored in and read from the global MediaGoblin configuration.
TODO: Discuss (at a meeting?) general priorities about how we want to balance "users see their media ASAP" vs. general site performance out of the box. Get feedback about the version 1 scheme, and maybe get alternative proposals.
cwebber's vague thoughts
09:09 < paroneayea> I've been thinking vaguely about a few things related to that like 09:10 < paroneayea> "what if you don't have the original anymore? Does it reprocess it into something more lossy?" 09:11 < paroneayea> "Should we set it up so that things can determine conditionally if they should be reprocessed? Ie, if resolutions have changed, but this one was smaller than the new lowest resolution anyway?" 09:11 < paroneayea> I'm not sure what the answer to those are but I've only thought vaguely about them.
Base Task class
I think we can write a common subclass of Celery's Task that will serve as the base for all of our processing tasks. It would provide an on_failure method to decide whether or not a retry is appropriate, and if so, handle rewriting the exchange (to mark this as a retry), calculate the exponential backoff time, and reschedule the task.
This class could also serve as a unifying place to collect utility functions that many processing tasks need. There's a lot of file handling code that's repeated with minor variation throughout the processing tasks right now; that could be abstracted into methods of this class to reduce redundancy in the code.
Splitting up tasks
Right now, our processing tasks are monolithic beasts: one single task performs all of the transformations necessary for the media to be considered "processed." We could improve code readability and maintainability, site reliability, and possibly even performance by splitting tasks up appropriately.
The basic idea here is that each processing task would, after successful completion, queue up a "check if finished" task, which would in turn do quick checks to see if all the necessary results of processing are in place. When it finds that they are, it marks the media entry as processed, and performs clean-up jobs like removing the original queued file, so that the media shows up in the gallery and so on.
(Alternatively, the "check if finished" task could be more stateful, keeping track of which tasks fire it off, and then performing its own work when the last task reports in. This approach seems more fragile and error-prone, so I prefer an approach that checks whether the subtasks actually did their jobs, but that might not always be possible, so I'm making a note of this.)
As an example: image processing includes four jobs: making a thumbnail, a medium image, stashing the original file (with slight renaming as appropriate), and saving EXIF and GPS data to the database. These four tasks could each be run individually. They all fire a "check if finished" task that examines if the media entry has files stashed from all these tasks (in other words, it peeks at media_files_dict). When all the files are in place, it marks the entry as processed, and performs necessary cleanup.
We can potentially save a lot of work with this approach. Consider a video where transcoding succeeds but generating a thumbnail fails. By splitting tasks up, the resource-intensive transcoding will only run once, while we retry thumbnail generation appropriately.
After we have reprocessing code, logged in users should be able to see information about where their entries stand in the queue: it's going to be processed, it's going to be reprocessed by such-and-such time, it failed completely. There are already some bugs about this (TODO: collect them here). The current panel would be a good starting point for publishing this information generally. There are also specific places where we could conditionally show useful information: for instance, mention around the media submission page that the media might be slow to appear if processing queues are unusually large.
This is a big enough job that it could probably justify its own feature page...