Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added config variable to disable dispatching jobs after commit #3698

Merged
merged 3 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions config/media-library.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@
*/
'queue_conversions_by_default' => env('QUEUE_CONVERSIONS_BY_DEFAULT', true),

/*
* Should database transactions be run after database commits?
*/
'queue_conversions_after_database_commit' => env('QUEUE_CONVERSIONS_AFTER_DB_COMMIT', true),

/*
* The fully qualified class name of the media model.
*/
Expand Down
5 changes: 5 additions & 0 deletions docs/converting-images/defining-conversions.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,11 @@ public function registerMediaConversions(?Media $media = null): void
}
```

The default behaviour is that queued conversions will run **after all database transactions have been committed**. \
This prevents unexpected behaviour where the model does not yet exist in the database and the conversion is disregarded.
If you need the conversions to run within your transaction, you can set the `queue_conversions_after_database_commit`
in the `media-library` config file to `false`.

## Using model properties in a conversion

When registering conversions inside the `registerMediaConversions` function you won't have access to your model properties by default. If you want to use a property of your model as input for defining a conversion you must set `registerMediaConversionsUsingModelInstance` to `
Expand Down
8 changes: 6 additions & 2 deletions src/Conversions/FileManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,9 @@ protected function dispatchQueuedConversions(
->onConnection(config('media-library.queue_connection_name'))
->onQueue(config('media-library.queue_name'));

dispatch($job)->afterCommit();
config('media-library.queue_conversions_after_database_commit')
? dispatch($job)->afterCommit()
: dispatch($job);

return $this;
}
Expand All @@ -120,7 +122,9 @@ protected function generateResponsiveImages(Media $media, bool $withResponsiveIm
->onConnection(config('media-library.queue_connection_name'))
->onQueue(config('media-library.queue_name'));

dispatch($job)->afterCommit();
config('media-library.queue_conversions_after_database_commit')
? dispatch($job)->afterCommit()
: dispatch($job);

return $this;
}
Expand Down