Skip to content

Commit

Permalink
Changed IdentityTrait to IdentityIntTrait, fixed locking async proces…
Browse files Browse the repository at this point in the history
…sing.
  • Loading branch information
TomasHermanek committed Oct 1, 2024
1 parent 11be693 commit 92e5165
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 22 deletions.
31 changes: 16 additions & 15 deletions src/Domain/AssetFile/AbstractAssetFileStatusFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
use AnzuSystems\CoreDamBundle\Exception\AssetFileProcessFailed;
use AnzuSystems\CoreDamBundle\Exception\DuplicateAssetFileException;
use AnzuSystems\CoreDamBundle\Exception\ForbiddenOperationException;
use AnzuSystems\CoreDamBundle\Exception\RuntimeException;
use AnzuSystems\CoreDamBundle\Logger\DamLogger;
use AnzuSystems\CoreDamBundle\Messenger\Message\AssetRefreshPropertiesMessage;
use AnzuSystems\CoreDamBundle\Model\Dto\Asset\AssetAdmFinishDto;
Expand All @@ -37,6 +36,7 @@
use Doctrine\ORM\NonUniqueResultException;
use League\Flysystem\FilesystemException;
use Psr\Cache\InvalidArgumentException;
use RuntimeException;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
use Symfony\Contracts\Service\Attribute\Required;
use Throwable;
Expand Down Expand Up @@ -196,18 +196,27 @@ public function finishUpload(AssetAdmFinishDto $assetFinishDto, AssetFile $asset
*/
public function storeAndProcess(AssetFile $assetFile, ?AdapterFile $file = null, bool $dispatchPropertyRefresh = true): AssetFile
{
$lockName = $assetFile->getAssetType()->value . '_' . $assetFile->getLicence()->getId();

try {
if ($assetFile->getAssetAttributes()->getStatus()->is(AssetFileProcessStatus::Uploaded)) {
$file = $this->store($assetFile, $file);
$file = $file ?: $this->createFile($assetFile);
$this->fileAttributesPostProcessor->processAttributes($assetFile, $file);
$this->fileAttributesPostProcessor->processChecksum($assetFile, $file);
// we need to lock process due to duplicity checks
$this->resourceLocker->lock($lockName);
$this->store($assetFile, $file);
}
if (null === $file) {
throw new RuntimeException(sprintf('AssetFile (%s) cant be processed without file', $assetFile->getId()));
}
if ($assetFile->getAssetAttributes()->getStatus()->is(AssetFileProcessStatus::Stored)) {
$this->chunkFileManager->clearChunks($assetFile);
$this->process($assetFile, $file, $dispatchPropertyRefresh);
$this->resourceLocker->unLock($lockName);
}
} catch (DuplicateAssetFileException $duplicateAssetFileException) {
$this->resourceLocker->unLock($lockName);
$assetFile->getAssetAttributes()->setOriginAssetId(
(string) $duplicateAssetFileException->getOldAsset()->getId()
);
Expand All @@ -218,19 +227,23 @@ public function storeAndProcess(AssetFile $assetFile, ?AdapterFile $file = null,
$this->assetStatusManager->toDuplicate($assetFile);
$this->assetFileEventDispatcher->dispatchAssetFileChanged($assetFile);
} catch (AssetFileProcessFailed $assetFileProcessFailed) {
$this->resourceLocker->unLock($lockName);
$this->assetStatusManager->toFailed(
$assetFile,
$assetFileProcessFailed->getAssetFileFailedType(),
$assetFileProcessFailed
);
$this->assetFileEventDispatcher->dispatchAssetFileChanged($assetFile);
} catch (Throwable $exception) {
$this->resourceLocker->unLock($lockName);
$this->assetStatusManager->toFailed(
$assetFile,
AssetFileFailedType::Unknown,
$exception
);
$this->assetFileEventDispatcher->dispatchAssetFileChanged($assetFile);
} finally {
$this->resourceLocker->unLock($lockName);
}

return $assetFile;
Expand All @@ -244,20 +257,10 @@ public function storeAndProcess(AssetFile $assetFile, ?AdapterFile $file = null,
* @throws TransportExceptionInterface
* @throws Throwable
*/
public function store(AssetFile $assetFile, ?AdapterFile $file = null): AdapterFile
public function store(AssetFile $assetFile, AdapterFile $file): AdapterFile
{
$file = $file ?: $this->createFile($assetFile);

$this->fileAttributesPostProcessor->processAttributes($assetFile, $file);
$this->fileAttributesPostProcessor->processChecksum($assetFile, $file);

$lockName = $assetFile->getAssetType()->value . '_' . $assetFile->getLicence()->getId();
$this->resourceLocker->lock($lockName);

$originAssetFile = $this->checkDuplicate($assetFile);
if ($originAssetFile) {
$this->resourceLocker->unLock($lockName);

throw new DuplicateAssetFileException(
oldAsset: $originAssetFile,
newAsset: $assetFile
Expand All @@ -269,9 +272,7 @@ public function store(AssetFile $assetFile, ?AdapterFile $file = null): AdapterF
$this->assetFileStorageOperator->save($assetFile, $file);
$this->assetStatusManager->toStored($assetFile);
$this->assetManager->commit();
$this->resourceLocker->unLock($lockName);
} catch (Throwable $exception) {
$this->resourceLocker->unLock($lockName);
$this->assetManager->rollback();

throw $exception;
Expand Down
2 changes: 1 addition & 1 deletion src/Domain/AssetFile/AssetFileStatusInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#[AutoconfigureTag]
interface AssetFileStatusInterface
{
public function store(AssetFile $assetFile, ?AdapterFile $file): AdapterFile;
public function store(AssetFile $assetFile, AdapterFile $file): AdapterFile;

public function process(AssetFile $assetFile, AdapterFile $file, bool $dispatchPropertyRefresh): AssetFile;

Expand Down
4 changes: 2 additions & 2 deletions src/Entity/AssetLicence.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use AnzuSystems\Contracts\Entity\Interfaces\IdentifiableInterface;
use AnzuSystems\Contracts\Entity\Interfaces\TimeTrackingInterface;
use AnzuSystems\Contracts\Entity\Interfaces\UserTrackingInterface;
use AnzuSystems\Contracts\Entity\Traits\IdentityTrait;
use AnzuSystems\Contracts\Entity\Traits\IdentityIntTrait;
use AnzuSystems\Contracts\Entity\Traits\TimeTrackingTrait;
use AnzuSystems\Contracts\Entity\Traits\UserTrackingTrait;
use AnzuSystems\CoreDamBundle\App;
Expand All @@ -31,7 +31,7 @@
#[BaseAppAssert\UniqueEntity(fields: ['extSystem', 'extId'], errorAtPath: ['extId'])]
class AssetLicence implements IdentifiableInterface, UserTrackingInterface, TimeTrackingInterface, AssetLicenceInterface, ExtSystemInterface
{
use IdentityTrait;
use IdentityIntTrait;
use TimeTrackingTrait;
use UserTrackingTrait;

Expand Down
4 changes: 2 additions & 2 deletions src/Entity/AssetLicenceGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use AnzuSystems\Contracts\Entity\Interfaces\IdentifiableInterface;
use AnzuSystems\Contracts\Entity\Interfaces\TimeTrackingInterface;
use AnzuSystems\Contracts\Entity\Interfaces\UserTrackingInterface;
use AnzuSystems\Contracts\Entity\Traits\IdentityTrait;
use AnzuSystems\Contracts\Entity\Traits\IdentityIntTrait;
use AnzuSystems\Contracts\Entity\Traits\TimeTrackingTrait;
use AnzuSystems\Contracts\Entity\Traits\UserTrackingTrait;
use AnzuSystems\CoreDamBundle\App;
Expand All @@ -33,7 +33,7 @@ class AssetLicenceGroup implements
UserTrackingInterface,
TimeTrackingInterface
{
use IdentityTrait;
use IdentityIntTrait;
use TimeTrackingTrait;
use UserTrackingTrait;

Expand Down
4 changes: 2 additions & 2 deletions src/Entity/ExtSystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use AnzuSystems\Contracts\Entity\Interfaces\IdentifiableInterface;
use AnzuSystems\Contracts\Entity\Interfaces\TimeTrackingInterface;
use AnzuSystems\Contracts\Entity\Interfaces\UserTrackingInterface;
use AnzuSystems\Contracts\Entity\Traits\IdentityTrait;
use AnzuSystems\Contracts\Entity\Traits\IdentityIntTrait;
use AnzuSystems\Contracts\Entity\Traits\TimeTrackingTrait;
use AnzuSystems\Contracts\Entity\Traits\UserTrackingTrait;
use AnzuSystems\CoreDamBundle\App;
Expand All @@ -30,7 +30,7 @@
#[ORM\Cache(usage: App::CACHE_STRATEGY)]
class ExtSystem implements IdentifiableInterface, UserTrackingInterface, TimeTrackingInterface, ExtSystemInterface
{
use IdentityTrait;
use IdentityIntTrait;
use UserTrackingTrait;
use TimeTrackingTrait;

Expand Down

0 comments on commit 92e5165

Please sign in to comment.