Skip to content

Commit

Permalink
Added config variable to disable dispatching jobs after commit (#3698)
Browse files Browse the repository at this point in the history
* Only dispatch conversions after all database transactions have been committed

* Added optional queue_conversions_after_database_commit configuration to dispatch jobs within a transaction
  • Loading branch information
chrispage1 authored Aug 22, 2024
1 parent 50e9a12 commit b103470
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
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

0 comments on commit b103470

Please sign in to comment.