-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathContentReleaseManager.php
More file actions
146 lines (122 loc) · 5.36 KB
/
ContentReleaseManager.php
File metadata and controls
146 lines (122 loc) · 5.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php
declare(strict_types=1);
namespace Flowpack\DecoupledContentStore;
use Flowpack\DecoupledContentStore\Core\Domain\ValueObject\ContentReleaseIdentifier;
use Flowpack\DecoupledContentStore\Core\Domain\ValueObject\RedisInstanceIdentifier;
use Flowpack\DecoupledContentStore\Core\Infrastructure\RedisClientManager;
use Neos\ContentRepository\Domain\Model\Workspace;
use Flowpack\Prunner\ValueObject\JobId;
use Neos\Flow\Annotations as Flow;
use Flowpack\Prunner\PrunnerApiService;
use Flowpack\Prunner\ValueObject\PipelineName;
use Neos\Flow\Security\Context;
/**
* @Flow\Scope("singleton")
*/
class ContentReleaseManager
{
/**
* @Flow\Inject
* @var PrunnerApiService
*/
protected $prunnerApiService;
/**
* @Flow\Inject
* @var RedisClientManager
*/
protected $redisClientManager;
/**
* @Flow\InjectConfiguration("configEpoch")
* @var array
*/
protected $configEpochSettings;
/**
* @FLow\Inject
* @var Context
*/
protected $securityContext;
const REDIS_CURRENT_RELEASE_KEY = 'contentStore:current';
const NO_PREVIOUS_RELEASE = 'NO_PREVIOUS_RELEASE';
public function startIncrementalContentRelease(string $currentContentReleaseId = null, Workspace $workspace = null, array $additionalVariables = []): ContentReleaseIdentifier
{
$contentReleaseId = ContentReleaseIdentifier::create();
// the currentContentReleaseId is not used in any pipeline step in this package, but is a common need in other
// use cases in extensions, e.g. calculating the differences between current and new release
$this->prunnerApiService->schedulePipeline(PipelineName::create('do_content_release'), array_merge($additionalVariables, [
'contentReleaseId' => $contentReleaseId,
'currentContentReleaseId' => $this->resolveCurrentContentReleaseId($currentContentReleaseId),
'validate' => true,
'flushContentCache' => false,
'workspaceName' => $workspace !== null ? $workspace->getName() : 'live',
'accountId' => $this->getAccountId(),
]));
return $contentReleaseId;
}
// the validate parameter can be used to intentionally skip the validation step for this release
public function startFullContentRelease(bool $validate = true, string $currentContentReleaseId = null, Workspace $workspace = null, array $additionalVariables = []): ContentReleaseIdentifier
{
$contentReleaseId = ContentReleaseIdentifier::create();
$this->prunnerApiService->schedulePipeline(PipelineName::create('do_content_release'), array_merge($additionalVariables, [
'contentReleaseId' => $contentReleaseId,
'currentContentReleaseId' => $this->resolveCurrentContentReleaseId($currentContentReleaseId),
'validate' => $validate,
'flushContentCache' => true,
'workspaceName' => $workspace !== null ? $workspace->getName() : 'live',
'accountId' => $this->getAccountId(),
]));
return $contentReleaseId;
}
public function cancelAllRunningContentReleases(): void
{
$result = $this->prunnerApiService->loadPipelinesAndJobs();
$runningJobs = $result->getJobs()->forPipeline(PipelineName::create('do_content_release'))->running();
foreach ($runningJobs as $job) {
$this->prunnerApiService->cancelJob($job);
}
}
/**
* Cancel a single running content release ignoring all others
*/
public function cancelRunningContentRelease(JobId $jobId): void
{
$result = $this->prunnerApiService->loadPipelinesAndJobs();
$runningJobs = $result->getJobs()->forPipeline(PipelineName::create('do_content_release'))->running();
foreach ($runningJobs as $job) {
if ($job->getId() === $jobId) {
$this->prunnerApiService->cancelJob($job);
break;
}
}
}
public function toggleConfigEpoch(RedisInstanceIdentifier $redisInstanceIdentifier): void
{
$currentConfigEpochConfig = $this->configEpochSettings['current'] ?? null;
$previousConfigEpochConfig = $this->configEpochSettings['previous'] ?? null;
$redis = $this->redisClientManager->getRedis($redisInstanceIdentifier);
$configEpochRedis = $redis->get('contentStore:configEpoch');
if ($configEpochRedis === $currentConfigEpochConfig) {
$redis->set('contentStore:configEpoch', $previousConfigEpochConfig);
} else {
$redis->set('contentStore:configEpoch', $currentConfigEpochConfig);
}
}
private function resolveCurrentContentReleaseId(?string $currentContentReleaseId): string
{
if ($currentContentReleaseId !== null) {
return $currentContentReleaseId;
}
$redis = $this->redisClientManager->getPrimaryRedis();
$currentContentReleaseIdFromRedis = $redis->get(self::REDIS_CURRENT_RELEASE_KEY);
if ($currentContentReleaseIdFromRedis !== false) {
return $currentContentReleaseIdFromRedis;
}
return self::NO_PREVIOUS_RELEASE;
}
private function getAccountId(): ?string
{
if ($this->securityContext->isInitialized() && !is_null($this->securityContext->getAccount())) {
return $this->securityContext->getAccount()->getAccountIdentifier();
}
return null;
}
}