|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Flowpack\DecoupledContentStore\Core; |
| 4 | + |
| 5 | +use Flowpack\DecoupledContentStore\Core\Domain\ValueObject\ContentReleaseIdentifier; |
| 6 | +use Flowpack\DecoupledContentStore\Core\Infrastructure\RedisClientManager; |
| 7 | +use Neos\Flow\Annotations as Flow; |
| 8 | + |
| 9 | +/** |
| 10 | + * We usually rely on prunner to ensure that only one build is running at any given time. |
| 11 | + * |
| 12 | + * However, when running in a cloud environment with no shared storage, the prunner data folder is not shared between |
| 13 | + * instances. In this case, during a deployment, two containers run concurrently, with two separate prunner instances |
| 14 | + * (the old and the new one), which do not see each other. |
| 15 | + * |
| 16 | + * We could fix this in prunner itself, but this would be a bigger undertaking (different storage backends for prunner), |
| 17 | + * or we can work around this in DecoupledContentStore. This is what this class does. |
| 18 | + * |
| 19 | + * ## Main Idea |
| 20 | + * |
| 21 | + * - We use a special redis key "contentStore:concurrentBuildLock" which is set to the current being-built release ID in |
| 22 | + * `./flow contentReleasePrepare:ensureAllOtherInProgressContentReleasesWillBeTerminated` |
| 23 | + * - In the "Enumerate" and "Render" phases, we periodically check whether the concurrentBuildLock is set to the currently |
| 24 | + * in-progress content release. If NO, we abort. |
| 25 | + * |
| 26 | + * @Flow\Scope("singleton") |
| 27 | + */ |
| 28 | +class ConcurrentBuildLockService |
| 29 | +{ |
| 30 | + |
| 31 | + /** |
| 32 | + * @Flow\Inject |
| 33 | + * @var RedisClientManager |
| 34 | + */ |
| 35 | + protected $redisClientManager; |
| 36 | + |
| 37 | + public function ensureAllOtherInProgressContentReleasesWillBeTerminated(ContentReleaseIdentifier $contentReleaseIdentifier) |
| 38 | + { |
| 39 | + $this->redisClientManager->getPrimaryRedis()->set('contentStore:concurrentBuildLock', $contentReleaseIdentifier->getIdentifier()); |
| 40 | + } |
| 41 | + |
| 42 | + public function assertNoOtherContentReleaseWasStarted(ContentReleaseIdentifier $contentReleaseIdentifier) |
| 43 | + { |
| 44 | + $concurrentBuildLockString = $this->redisClientManager->getPrimaryRedis()->get('contentStore:concurrentBuildLock'); |
| 45 | + |
| 46 | + if (empty($concurrentBuildLockString)) { |
| 47 | + echo '!!!!! Hard-aborting the current job ' . $contentReleaseIdentifier->getIdentifier() . ' because the concurrentBuildLock does not exist.' . "\n\n"; |
| 48 | + echo "This should never happen for correctly configured jobs (that run after prepare_finished).\n\n"; |
| 49 | + exit(1); |
| 50 | + } |
| 51 | + |
| 52 | + $concurrentBuildLock = ContentReleaseIdentifier::fromString($concurrentBuildLockString); |
| 53 | + |
| 54 | + if (!$contentReleaseIdentifier->equals($concurrentBuildLock)) { |
| 55 | + // the concurrent build lock is different (i.e. newer) than our currently-running content release. |
| 56 | + // Thus, we abort the in-progress content release as quickly as we can - by DYING. |
| 57 | + |
| 58 | + echo '!!!!! Hard-aborting the current job ' . $contentReleaseIdentifier->getIdentifier() . ' because the concurrentBuildLock contains ' . $concurrentBuildLock->getIdentifier() . "\n\n"; |
| 59 | + echo "This is no error during deployment, but should never happen outside a deployment.\n\n It can only happen if two prunner instances run concurrently.\n\n"; |
| 60 | + exit(1); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | +} |
0 commit comments