From b103470caad6e7cd8013130e2651e637da94d4ef Mon Sep 17 00:00:00 2001 From: Chris Page Date: Thu, 22 Aug 2024 10:20:42 +0100 Subject: [PATCH] Added config variable to disable dispatching jobs after commit (#3698) * Only dispatch conversions after all database transactions have been committed * Added optional queue_conversions_after_database_commit configuration to dispatch jobs within a transaction --- config/media-library.php | 5 +++++ docs/converting-images/defining-conversions.md | 5 +++++ src/Conversions/FileManipulator.php | 8 ++++++-- 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/config/media-library.php b/config/media-library.php index 7f6abbdb7..22517c5a1 100644 --- a/config/media-library.php +++ b/config/media-library.php @@ -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. */ diff --git a/docs/converting-images/defining-conversions.md b/docs/converting-images/defining-conversions.md index 8c8a8a37b..886b3c06b 100644 --- a/docs/converting-images/defining-conversions.md +++ b/docs/converting-images/defining-conversions.md @@ -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 ` diff --git a/src/Conversions/FileManipulator.php b/src/Conversions/FileManipulator.php index b80d513c4..9ad9f4832 100644 --- a/src/Conversions/FileManipulator.php +++ b/src/Conversions/FileManipulator.php @@ -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; } @@ -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; }