-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathContentReleaseCommands.php
169 lines (151 loc) · 5.28 KB
/
ContentReleaseCommands.php
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<?php
namespace Drupal\va_gov_build_trigger\Commands;
use Drupal\Core\Logger\LoggerChannelTrait;
use Drupal\Core\State\StateInterface;
use Drupal\va_gov_build_trigger\Controller\ContentReleaseNotificationController;
use Drupal\va_gov_build_trigger\EventSubscriber\ContinuousReleaseSubscriber;
use Drupal\va_gov_build_trigger\Service\BuildSchedulerInterface;
use Drupal\va_gov_build_trigger\Service\ReleaseStateManager;
use Drupal\va_gov_build_trigger\Service\ReleaseStateManagerInterface;
use Drupal\va_gov_content_release\Request\RequestInterface;
use Drush\Commands\DrushCommands;
/**
* A Drush interface to the content release.
*/
class ContentReleaseCommands extends DrushCommands {
use LoggerChannelTrait;
/**
* The release state manager.
*
* @var \Drupal\va_gov_build_trigger\Service\ReleaseStateManagerInterface
*/
protected $releaseStateManager;
/**
* The build scheduler service.
*
* @var \Drupal\va_gov_build_trigger\Service\BuildSchedulerInterface
*/
protected $buildScheduler;
/**
* The content release request service.
*
* @var \Drupal\va_gov_content_release\Request\RequestInterface
*/
protected $requestService;
/**
* The state management service.
*
* @var \Drupal\Core\State\StateInterface
*/
protected $state;
/**
* Constructor.
*
* @param \Drupal\va_gov_build_trigger\Service\ReleaseStateManagerInterface $releaseStateManager
* The release state manager service.
* @param \Drupal\va_gov_build_trigger\Service\BuildSchedulerInterface $buildScheduler
* The build scheduler service.
* @param \Drupal\va_gov_content_release\Request\RequestInterface $requestService
* The request service.
* @param \Drupal\Core\State\StateInterface $state
* The state service.
*/
public function __construct(
ReleaseStateManagerInterface $releaseStateManager,
BuildSchedulerInterface $buildScheduler,
RequestInterface $requestService,
StateInterface $state,
) {
$this->releaseStateManager = $releaseStateManager;
$this->buildScheduler = $buildScheduler;
$this->requestService = $requestService;
$this->state = $state;
$this->logger = $this->getLogger('va_gov_build_trigger');
}
/**
* Reset the content release state.
*
* @command va-gov:content-release:reset-state
* @aliases va-gov-content-release-reset-state
*/
public function resetState() {
$this->releaseStateManager->resetState();
$this->getLogger('va_gov_build_trigger')->info('Content release state has been reset to \'ready\'.');
}
/**
* Advance the state like an external system would do through HTTP.
*
* @param string $state
* Required. Declare which state to advance to.
*
* @command va-gov:content-release:advance-state
* @aliases va-gov-content-release-advance-state
*/
public function advanceState($state) {
$is_allowed_notification = in_array($state, ContentReleaseNotificationController::allowedNotifications());
$can_transition = $this->releaseStateManager->canAdvanceStateTo($state);
$can_transition = ($can_transition === ReleaseStateManager::STATE_TRANSITION_OK);
if (!$is_allowed_notification || !$can_transition) {
$this->getLogger('va_gov_build_trigger')->error('State cannot be advanced to @state', [
'@state' => $state,
]);
return;
}
$this->releaseStateManager->advanceStateTo($state);
$this->getLogger('va_gov_build_trigger')->info('State has been advanced to @state', [
'@state' => $state,
]);
}
/**
* Get the current release state.
*
* @command va-gov:content-release:get-state
* @aliases va-gov-content-release-get-state
*/
public function getReleaseState() {
$state = $this->releaseStateManager->getState();
$this->io()->write($state);
}
/**
* Make sure builds are going out at least hourly during business hours.
*
* @command va-gov:content-release:check-scheduled
* @aliases va-gov-content-release-check-scheduled
*/
public function checkScheduledBuild() {
$this->buildScheduler->checkScheduledBuild();
}
/**
* If the state is stale, reset the state.
*
* @command va-gov:content-release:check-stale
* @aliases va-gov-content-release-check-stale
*/
public function checkStale() {
if ($this->releaseStateManager->releaseStateIsStale()) {
$this->resetState();
$this->requestService->submitRequest('Submitting new request due to staleness.');
}
}
/**
* Check continuous release state.
*
* @command va-gov:content-release:is-continuous-release-enabled
* @aliases va-gov-content-release-is-continuous-release-enabled
*/
public function checkContinuousReleaseState() {
$this->io()->writeln(print_r($this->state->get(ContinuousReleaseSubscriber::CONTINUOUS_RELEASE_ENABLED, FALSE)));
}
/**
* Toggle continuous release.
*
* @command va-gov:content-release:toggle-continuous
* @aliases va-gov-content-release-toggle-continuous
*/
public function toggleContinuousRelease() {
$current = $this->state->get(ContinuousReleaseSubscriber::CONTINUOUS_RELEASE_ENABLED, FALSE);
$this->state->set(ContinuousReleaseSubscriber::CONTINUOUS_RELEASE_ENABLED, !$current);
$status_text = (!$current === TRUE ? 'enabled' : 'disabled');
$this->io()->writeln('Continuous release is now ' . $status_text);
}
}