Skip to content

Commit

Permalink
more bifurcating goodness
Browse files Browse the repository at this point in the history
  • Loading branch information
alexfinnarn committed Feb 6, 2024
1 parent 07d1e93 commit fb68187
Show file tree
Hide file tree
Showing 6 changed files with 278 additions and 5 deletions.
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);
}
}

}
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);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function checkScheduledBuild() : void {

$this->runDuringBusinessHours(function () use ($currentTime, $time_since_last_build) {
if ($time_since_last_build >= 3600) {
$this->requestService->submitRequest('Scheduled hourly build');
$this->requestService->submitRequest('Scheduled hourly build', ['frontend' => 'content_build']);
$this->state->set(self::VA_GOV_LAST_SCHEDULED_BUILD_REQUEST, $currentTime);
}
}, $this->requestService, $this->state, $time_since_last_build);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,15 @@ public function __construct(EntityTypeManagerInterface $entityTypeManager) {
* {@inheritDoc}
*/
public function submitRequest(string $reason, array $options) : void {
$frontend = $options['frontend'] ?? 'content_build';
$job_type = $frontend === 'next_build'
? 'va_gov_content_release_next_request'
: 'va_gov_content_release_request';

try {
$job = Job::create(static::JOB_TYPE, [
$job = Job::create($job_type, [
'reason' => $reason,
'frontend' => $options['frontend'] ?? 'content_build',
'frontend' => $frontend,
]);
$this->queue->enqueueJob($job);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
interface RequestInterface {

const QUEUE_NAME = 'content_release';
const JOB_TYPE = 'va_gov_content_release_request';

/**
* Trigger the content release.
Expand Down
2 changes: 1 addition & 1 deletion scripts/queue_runner/queue_runner.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ cd "${TUGBOAT_ROOT}"
./bin/drush advancedqueue:queue:process content_release 2>&1
#[ -f "./docroot/sites/default/files/.buildrequest" ] && ( ./scripts/build-frontend.sh > /dev/null 2>&1 && rm ./docroot/sites/default/files/.buildrequest ) &
#[ -f "./docroot/sites/default/files/.next-buildrequest" ] && ( ./scripts/next-build-frontend.sh && rm ./docroot/sites/default/files/.next-buildrequest ) &
sleep 60s
sleep 6s

0 comments on commit fb68187

Please sign in to comment.