-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
07d1e93
commit fb68187
Showing
6 changed files
with
278 additions
and
5 deletions.
There are no files selected for viewing
132 changes: 132 additions & 0 deletions
132
...ules/custom/va_gov_build_trigger/src/Plugin/AdvancedQueue/JobType/NextReleaseDispatch.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
<?php | ||
|
||
namespace Drupal\va_gov_build_trigger\Plugin\AdvancedQueue\JobType; | ||
|
||
use Drupal\advancedqueue\Job; | ||
use Drupal\advancedqueue\JobResult; | ||
use Drupal\advancedqueue\Plugin\AdvancedQueue\JobType\JobTypeBase; | ||
use Drupal\Core\Logger\LoggerChannelFactoryInterface; | ||
use Drupal\Core\Logger\LoggerChannelTrait; | ||
use Drupal\Core\Plugin\ContainerFactoryPluginInterface; | ||
use Drupal\va_gov_build_trigger\Environment\EnvironmentDiscovery; | ||
use Drupal\va_gov_build_trigger\Service\ReleaseStateManager; | ||
use Drupal\va_gov_build_trigger\Service\ReleaseStateManagerInterface; | ||
use Symfony\Component\DependencyInjection\ContainerInterface; | ||
|
||
/** | ||
* AdvancedQueue queue processor plugin for content release dispatch. | ||
* | ||
* @AdvancedQueueJobType( | ||
* id = "va_gov_content_release_next_dispatch", | ||
* label = @Translation("VA.gov Content Release Next Dispatch"), | ||
* max_retries = 30, | ||
* retry_delay = 120 | ||
* ) | ||
*/ | ||
class NextReleaseDispatch extends JobTypeBase implements ContainerFactoryPluginInterface { | ||
use LoggerChannelTrait; | ||
|
||
/** | ||
* Logger Channel. | ||
* | ||
* @var \Psr\Log\LoggerInterface | ||
*/ | ||
protected $logger; | ||
|
||
/** | ||
* The release state manager. | ||
* | ||
* @var \Drupal\va_gov_build_trigger\Service\ReleaseStateManagerInterface | ||
*/ | ||
protected $releaseStateManager; | ||
|
||
/** | ||
* The environment discovery service. | ||
* | ||
* @var \Drupal\va_gov_build_trigger\Environment\EnvironmentDiscovery | ||
*/ | ||
protected $environmentDiscovery; | ||
|
||
/** | ||
* Constructs a \Drupal\Component\Plugin\PluginBase object. | ||
* | ||
* @param array $configuration | ||
* The plugin configuration. | ||
* @param string $pluginId | ||
* The plugin ID. | ||
* @param mixed $pluginDefinition | ||
* The plugin definition. | ||
* @param \Drupal\Core\Logger\LoggerChannelFactoryInterface $loggerFactory | ||
* A logger channel factory. | ||
* @param \Drupal\va_gov_build_trigger\Service\ReleaseStateManagerInterface $releaseStateManager | ||
* The release manager service. | ||
* @param \Drupal\va_gov_build_trigger\Environment\EnvironmentDiscovery $environmentDiscovery | ||
* The environment discovery service. | ||
*/ | ||
public function __construct( | ||
array $configuration, | ||
$pluginId, | ||
$pluginDefinition, | ||
LoggerChannelFactoryInterface $loggerFactory, | ||
ReleaseStateManagerInterface $releaseStateManager, | ||
EnvironmentDiscovery $environmentDiscovery | ||
) { | ||
parent::__construct($configuration, $pluginId, $pluginDefinition); | ||
|
||
$this->setLoggerFactory($loggerFactory); | ||
$this->logger = $this->getLogger('va_gov_build_trigger'); | ||
$this->releaseStateManager = $releaseStateManager; | ||
$this->environmentDiscovery = $environmentDiscovery; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { | ||
return new static( | ||
$configuration, | ||
$plugin_id, | ||
$plugin_definition, | ||
$container->get('logger.factory'), | ||
$container->get('va_gov_build_trigger.next_release_state_manager'), | ||
$container->get('va_gov.build_trigger.environment_discovery') | ||
); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function process(Job $job) { | ||
switch ($this->releaseStateManager->canAdvanceStateTo(ReleaseStateManager::STATE_DISPATCHED)) { | ||
case ReleaseStateManager::STATE_TRANSITION_OK: | ||
try { | ||
$payload = $job->getPayload(); | ||
$this->environmentDiscovery->triggerFrontendBuild($payload); | ||
} | ||
catch (\Exception $e) { | ||
return JobResult::failure('Release dispatch failed with error: ' . $e->getMessage()); | ||
} | ||
$this->releaseStateManager->advanceStateTo(ReleaseStateManager::STATE_DISPATCHED); | ||
$message = 'Content release has been dispatched.'; | ||
$this->logger->info($message); | ||
return JobResult::success($message); | ||
|
||
case ReleaseStateManager::STATE_TRANSITION_WAIT: | ||
$message = 'Release dispatch cannot be processed right now. Will try again in two minutes.'; | ||
$this->logger->info($message); | ||
// Since this job has a default retry behavior, this will be re-queued. | ||
return JobResult::failure($message); | ||
|
||
case ReleaseStateManager::STATE_TRANSITION_SKIP: | ||
$message = 'A release has already been dispatched but has not started yet. An additional release has <em>not</em> been dispatched.'; | ||
$this->logger->info($message); | ||
return JobResult::success($message); | ||
|
||
case ReleaseStateManager::STATE_TRANSITION_INVALID: | ||
$message = 'Dispatching a new release cannot happen right now. This request will be ignored.'; | ||
$this->logger->info($message); | ||
return JobResult::failure($message, 0); | ||
} | ||
} | ||
|
||
} |
137 changes: 137 additions & 0 deletions
137
...dules/custom/va_gov_build_trigger/src/Plugin/AdvancedQueue/JobType/NextReleaseRequest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
<?php | ||
|
||
namespace Drupal\va_gov_build_trigger\Plugin\AdvancedQueue\JobType; | ||
|
||
use Drupal\advancedqueue\Entity\QueueInterface; | ||
use Drupal\advancedqueue\Job; | ||
use Drupal\advancedqueue\JobResult; | ||
use Drupal\advancedqueue\Plugin\AdvancedQueue\JobType\JobTypeBase; | ||
use Drupal\Core\Logger\LoggerChannelFactoryInterface; | ||
use Drupal\Core\Logger\LoggerChannelTrait; | ||
use Drupal\Core\Plugin\ContainerFactoryPluginInterface; | ||
use Drupal\va_gov_build_trigger\Service\ReleaseStateManager; | ||
use Drupal\va_gov_build_trigger\Service\ReleaseStateManagerInterface; | ||
use Symfony\Component\DependencyInjection\ContainerInterface; | ||
|
||
/** | ||
* AdvancedQueue queue processor plugin for content release requests. | ||
* | ||
* @AdvancedQueueJobType( | ||
* id = "va_gov_content_release_next_request", | ||
* label = @Translation("VA.gov Content Release Next Request"), | ||
* max_retries = 30, | ||
* retry_delay = 120 | ||
* ) | ||
*/ | ||
class NextReleaseRequest extends JobTypeBase implements ContainerFactoryPluginInterface { | ||
use LoggerChannelTrait; | ||
|
||
/** | ||
* Logger Channel. | ||
* | ||
* @var \Psr\Log\LoggerInterface | ||
*/ | ||
protected $logger; | ||
|
||
/** | ||
* The release state manager. | ||
* | ||
* @var \Drupal\va_gov_build_trigger\Service\ReleaseStateManagerInterface | ||
*/ | ||
protected $releaseStateManager; | ||
|
||
/** | ||
* The content release dispatch queue. | ||
* | ||
* @var \Drupal\advancedqueue\Entity\QueueInterface | ||
*/ | ||
protected $dispatchQueue; | ||
|
||
/** | ||
* Constructs a ReleaseRequest object. | ||
* | ||
* @param array $configuration | ||
* The plugin configuration. | ||
* @param string $pluginId | ||
* The plugin ID. | ||
* @param mixed $pluginDefinition | ||
* The plugin definition. | ||
* @param \Drupal\Core\Logger\LoggerChannelFactoryInterface $loggerFactory | ||
* A logger channel factory. | ||
* @param \Drupal\va_gov_build_trigger\Service\ReleaseStateManagerInterface $releaseStateManager | ||
* The release state manager. | ||
* @param \Drupal\advancedqueue\Entity\QueueInterface $dispatchQueue | ||
* The release dispatch queue to add dispatch jobs to. | ||
*/ | ||
public function __construct( | ||
array $configuration, | ||
$pluginId, | ||
$pluginDefinition, | ||
LoggerChannelFactoryInterface $loggerFactory, | ||
ReleaseStateManagerInterface $releaseStateManager, | ||
QueueInterface $dispatchQueue | ||
) { | ||
parent::__construct($configuration, $pluginId, $pluginDefinition); | ||
|
||
$this->setLoggerFactory($loggerFactory); | ||
$this->logger = $this->getLogger('va_gov_build_trigger'); | ||
$this->releaseStateManager = $releaseStateManager; | ||
$this->dispatchQueue = $dispatchQueue; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { | ||
$dispatchQueue = $container->get('entity_type.manager') | ||
->getStorage('advancedqueue_queue') | ||
->load('content_release'); | ||
|
||
return new static( | ||
$configuration, | ||
$plugin_id, | ||
$plugin_definition, | ||
$container->get('logger.factory'), | ||
$container->get('va_gov_build_trigger.next_release_state_manager'), | ||
$dispatchQueue, | ||
); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function process(Job $job) { | ||
switch ($this->releaseStateManager->canAdvanceStateTo(ReleaseStateManager::STATE_REQUESTED)) { | ||
case ReleaseStateManager::STATE_TRANSITION_OK: | ||
$payload = $job->getPayload(); | ||
$dispatch_job = Job::create('va_gov_content_release_next_dispatch', [ | ||
'placeholder' => 'placeholder', | ||
'frontend' => $payload['frontend'], | ||
]); | ||
$this->dispatchQueue->enqueueJob($dispatch_job); | ||
$this->releaseStateManager->advanceStateTo(ReleaseStateManager::STATE_REQUESTED); | ||
$message = 'Content release dispatch has been queued. Reason: @reason'; | ||
$this->logger->info($message, [ | ||
'@reason' => $payload['reason'], | ||
]); | ||
return JobResult::success($message); | ||
|
||
case ReleaseStateManager::STATE_TRANSITION_WAIT: | ||
$message = 'Release request will be processed when the current release is completed. Current changes may not be included in the current release, but will go live during the next release. Will retry request in two minutes.'; | ||
$this->logger->info($message); | ||
// Since this job has a default retry behavior, this will be re-queued. | ||
return JobResult::failure($message); | ||
|
||
case ReleaseStateManager::STATE_TRANSITION_SKIP: | ||
$message = 'Release request will be included in the upcoming release dispatch. An additional release dispatch has <em>not<em> been queued.'; | ||
$this->logger->info($message); | ||
return JobResult::success($message); | ||
|
||
case ReleaseStateManager::STATE_TRANSITION_INVALID: | ||
$message = 'Somehow, ReleaseStateManager has determined that requesting a release right now is invalid.'; | ||
$this->logger->info($message); | ||
return JobResult::failure($message, 0); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters