Skip to content

Generate the variations of a media on creation, optionally in the background - #184

Open
rvanlaak wants to merge 3 commits into
jolicode:mainfrom
rvanlaak:feature/store-on-create
Open

rvanlaak wants to merge 3 commits into
jolicode:mainfrom
rvanlaak:feature/store-on-create

Conversation

@rvanlaak

@rvanlaak rvanlaak commented Oct 5, 2026

Copy link
Copy Markdown
Contributor

The bundle comes from a local filesystem perspective: checking whether a variation exists is cheap, and the application serves the files, so a missing variation can be generated when requested. On a bucket, neither holds. Listing media shouldn't cost a storage request per variation, and URLs served by the bucket itself (pre-signed, a CDN) never reach the application, so a missing variation is never generated.

This adds a cache.store_on_create setting, which generates all the variations of a media when it is created or moved. Setting store_on_create_message_bus hands that generation to Symfony Messenger, so it can run in a worker. By default, generation is synchronous and Messenger isn't required.

The documentation is in doc/misc-features/url-generation.rst.

… creation

Generating variations lazily suits a local filesystem, where checking that a
file exists is cheap. On a bucket each check is a request, and variations served
without the MediaController (through a CDN or pre-signed URLs) are never
generated when missing.

With cache.store_on_create enabled, a listener converts all the applicable
variations when a media is created or moved, forcing the conversion as no
variation can be stored at that path yet.
…us is configured

The store_on_create_message_bus setting names the bus to dispatch a
StoreVariations message to, instead of generating the variations
synchronously; routing the message to an asynchronous transport moves the
generation to a worker. A setting that names no Messenger bus fails the
container compilation.
@rvanlaak

Copy link
Copy Markdown
Contributor Author

@xavierlacot any spare time to review this improvement to the generation of variations and feature to dispatch that to the background?

@rvanlaak

Copy link
Copy Markdown
Contributor Author

@xavierlacot any feedback on decoupling generation from hoth path?

@xavierlacot

xavierlacot commented Nov 22, 2026 •

Copy link
Copy Markdown
Member

Hi @rvanlaak, sorry for the long silence, and thanks for this. I understand the need covered buy this PR, thanks a lot for trying to give an asnwer on this topic :)

My main feedback is about scope. The PR does two things:

1/ it adds a setting that generates variations when a media is created (store_on_create),
2/ it adds asynchronous generation through Messenger

I would like to split those 2 things, as I bvelieve that generating variations in a worker is a good idea, but it is not specific to "on create" It would be just as useful for folder moves, for regenerating after a variation is added to the configuration, or for joli:media:convert. Tying it to a store_on_create_message_bus setting closes those possibilities, which is something I want to avoid.

It is not a "no", it is rather "I would prefer that this PR is only centered on the 1st point (generating a varation when a media is created)"

For this PR (store on create only), here is what I would like to change

The naming of the setting is problematic. It should rather be named must_store_on_create or must_store_when_creating_media (on the same model like must_store_when_generating_url), in the same cache node.

The setting should handle multiple scopes: per library (as you did), plus an optional per-variation override following the existing pattern (null inherits from the library, clones inherit from their variation). Not every variation is served by the CDN, and generating all of them on every upload is expensive, so I think that, in many cases, users will want to trigger variation generation for only one or 2 variations, not all of them.

The way failures are handled is another point that should be addressed. A conversion failure should be logged and must not make createMedia() or move() fail, for 3 reasons:

  • it is what happens today without the setting. At the moment, a failing conversion only shows when the variation is requested, and it is what must_store_when_generating_url already does;
  • on create, the original is already written when the event is dispatched, so throwing an exception does not cancel anything: the admin reports an error, the file is there, and retrying the upload would answer "already exists";
  • on move, the Doctrine listener has already committed its update when our listener runs, so the rollback of the file would leave the entities pointing at a path that no longer exists. It would also mean that a media that cannot be converted could never be moved or renamed.

In practice, this means that the implementation must follow these guidelines:

  • each variation is handled on its own (today the loop stops at the first failure)
  • when there's an error while gfgenarting one variation at cfreate time, an error log with the library, path and variation, must be logged.
  • a low listener priority would be nice too, so that it runs after the Doctrine and application listeners.

A few smaller points:

  • the description says listing media should not cost a storage request per variation, but nothing changes in isStored(), which still asks the storage the first time. Either CacheStorage::store() primes the "stored" cache entry, or we drop this point from the documentation;
  • moving a folder still deletes the variations without regenerating them. Doing it synchronously for a whole folder is not reasonable, so I am fine with documenting it as a known limitation here and solving it with the asynchronous part (2);
  • the sentence saying an SVG is stored without variations is outdated since the SVG rasterization was merged, and there is a small conflict to resolve in JoliMediaBundle.php;
  • tests for the failure paths (a failing variation on create and on move) and for the configuration wiring would be welcome.

I know this is a fair amount of changes after a long wait on my side. If you are short on time, I can push them to your branch myself, just tell me which you prefer.

For the asynchronous part, here is the shape I have in mind - it includes quite a lot of differences compared to the current implementation

  • the "bus" must be a bundle-level setting: it is infrastructure (the "transport" is already chosen in the Messenger configuration)
  • whether a variation is generated asynchronously should be a separate boolean, per library with an optional per-variation override, same pattern as above. That way, a library on a local filesystem can stay synchronous while one on a bucket goes through a worker, and a variation needed right after the upload (such as the admin thumbnail) can stay synchronous while the heavy ones are deferred.

This has two consequences on the design :
1/ the message has to target a variation, or a list of them, and not only "the whole media", so that the handler does not redo what was already generated synchronously.
2/ a single entry point should decide, for each variation, whether to convert right away or to dispatch, so that any trigger can use it: store on create, folder moves, joli:media:convert, etc.

There is one problem I do not want to solve in the bundle yet. Between the upload and the moment the worker has run, the variation file does not exist. This is harmless when the application serves the file, because the MediaController generates it on the fly, and getTemporaryUrl() does the same. But a URL that points straight at the bucket or at a CDN returns a 404 during that time, and many CDNs keep that 404 in their cache for a while.

For a first version, I would only do three things:

  • explain this delay in the documentation;
  • document two ways to avoid it: configure the CDN to ask the application when the bucket has no file, or enable must_store_when_generating_url so that a missing variation is generated when its URL is built;
  • dispatch an event when the variations of a media have been stored, so that an application can wait for it before publishing the content, refreshing a preview or purging its CDN.

There are some other points to examinate and decisions to take:

  • a media replaced or moved while it is being converted (ie. while the conbversion message is beong handled), where a worker could write a stale variation or leave orphans at the previous path;
  • unrecoverable failures, which should not be retried;
  • failures of a single media now show up in two places, the logs of the request for the synchronous variations and the Messenger retries for the others;
  • a line in the documentation saying the workers must use the same application cache as the web processes, otherwise a replaced media can be handled with outdated properties.

If you agree with it, I would like to start a PR on asynchronous management myself, with all the above constraints in mind. In this cas, I would like to have you review it (as you have the real use case), so you could tell if it matches your needs. Does this split work for you?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants