Skip to content
Snippets Groups Projects
Commit ced692e0 authored by Lidia Matei's avatar Lidia Matei Committed by Yas Naoi
Browse files

Issue #3231898 by XLD, yas, baldwinlouie: Add VMware cron job

parent aa1894b5
No related branches found
No related tags found
No related merge requests found
vmware_view_items_per_page: 50
vmware_view_expose_items_per_page: true
vmware_update_resources_queue_cron_time: 5
vmware_queue_limit: 50
......@@ -11,3 +11,7 @@ vmware.settings:
type: integer
vmware_view_expose_items_per_page:
type: boolean
vmware_update_resources_queue_cron_time:
type: integer
vmware_queue_limit:
type: integer
......@@ -124,6 +124,28 @@ class VmwareAdminSettings extends ConfigFormBase {
'#default_value' => $config->get('vmware_view_items_per_page'),
];
$form['cron'] = [
'#type' => 'details',
'#title' => $this->t('Cron'),
'#open' => TRUE,
];
$form['cron']['vmware_update_resources_queue_cron_time'] = [
'#type' => 'number',
'#title' => 'Update resources every',
'#description' => $this->t('Run cron to process queue to update resources.'),
'#default_value' => $config->get('vmware_update_resources_queue_cron_time'),
'#min' => 1,
'#max' => 9999,
'#field_suffix' => 'seconds',
];
$form['cron']['vmware_queue_limit'] = [
'#type' => 'number',
'#title' => 'Queue Limit',
'#description' => $this->t('Number of items allowed in the update queue. New items are added when the queue count falls below this threshold.'),
'#default_value' => $config->get('vmware_queue_limit'),
];
$form['icon'] = [
'#type' => 'details',
'#title' => $this->t('Icon'),
......@@ -167,6 +189,8 @@ class VmwareAdminSettings extends ConfigFormBase {
$config->set('vmware_view_expose_items_per_page', $form_state->getValue('vmware_view_expose_items_per_page'));
$config->set('vmware_view_items_per_page', $form_state->getValue('vmware_view_items_per_page'));
$config->set('vmware_update_resources_queue_cron_time', $form_state->getValue('vmware_update_resources_queue_cron_time'));
$config->set('vmware_queue_limit', $form_state->getValue('vmware_queue_limit'));
$config->save();
......
<?php
namespace Drupal\vmware\Plugin\QueueWorker;
use Drupal\cloud\Traits\CloudContentEntityTrait;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Queue\QueueWorkerBase;
use Drupal\vmware\Service\VmwareServiceException;
use Drupal\vmware\Service\VmwareServiceInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Processes for Vmware Update Resources Queue.
*
* @QueueWorker(
* id = "vmware_update_resources_queue",
* title = @Translation("Vmware Update Resources Queue"),
* )
*/
class VmwareUpdateResourcesQueueWorker extends QueueWorkerBase implements ContainerFactoryPluginInterface {
use CloudContentEntityTrait;
/**
* The vmware service.
*
* @var \Drupal\vmware\Service\VmwareServiceInterface
*/
private $vmwareService;
/**
* Constructs a new LocaleTranslation object.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param array $plugin_definition
* The plugin implementation definition.
* @param \Drupal\vmware\Service\VmwareServiceInterface $vmware_service
* The vmware service.
*/
public function __construct(array $configuration, $plugin_id, array $plugin_definition, VmwareServiceInterface $vmware_service) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->vmwareService = $vmware_service;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('vmware')
);
}
/**
* {@inheritdoc}
*/
public function processItem($data): void {
$cloud_context = $data['cloud_context'];
$vmware_method_name = $data['vmware_method_name'];
if (!method_exists($this->vmwareService, $vmware_method_name)) {
$this->logger('vmware_service')->error($this->t("The method @vmware_method_name does not exist in class VmwareService.",
[
'@vmware_method_name' => $vmware_method_name,
]));
return;
}
try {
$this->vmwareService->setCloudContext($cloud_context);
if (empty($this->vmwareService->getSessionId())) {
$this->vmwareService->login();
}
$updated = $this->vmwareService->$vmware_method_name();
}
catch (VmwareServiceException $e) {
$updated = FALSE;
}
if (empty($updated)) {
$this->logger('vmware_service')->info('Unable to update resource with error @error.',
[
'@error' => $e->getMessage(),
]);
return;
}
$this->logger('vmware_service')->info('Updated resource in @context.', ['@context' => $cloud_context]);
}
}
......@@ -75,7 +75,7 @@ class VmwareService extends CloudServiceBase implements VmwareServiceInterface {
/**
* The session ID.
*
* @var array
* @var string
*/
private $sessionId;
......@@ -146,6 +146,9 @@ class VmwareService extends CloudServiceBase implements VmwareServiceInterface {
* {@inheritdoc}
*/
public function updateVms(array $params = [], $clear = TRUE): bool {
$this->logger('vmware_service')->info($this->t('Updating VMs for @cloud_context.', [
'@cloud_context' => $this->cloudContext,
]));
return $this->updateEntities(
'vmware_vm',
'Vm',
......@@ -272,6 +275,9 @@ class VmwareService extends CloudServiceBase implements VmwareServiceInterface {
* {@inheritdoc}
*/
public function updateHosts(array $params = [], $clear = TRUE): bool {
$this->logger('vmware_service')->info($this->t('Updating Hosts for @cloud_context.', [
'@cloud_context' => $this->cloudContext,
]));
return $this->updateEntities(
'vmware_host',
'Host',
......@@ -561,7 +567,12 @@ class VmwareService extends CloudServiceBase implements VmwareServiceInterface {
// Reset the progressive so batch works with out a web head.
$batch = &batch_get();
$batch['progressive'] = FALSE;
batch_process();
if (PHP_SAPI === 'cli' && function_exists('drush_backend_batch_process')) {
drush_backend_batch_process();
}
else {
batch_process();
}
// Log the end time.
$end = time();
......@@ -591,4 +602,33 @@ class VmwareService extends CloudServiceBase implements VmwareServiceInterface {
\Drupal::service('plugin.cache_clearer')->clearCachedDefinitions();
}
/**
* Create queue items for update resources queue.
*/
public function createResourceQueueItems(): void {
$queue_limit = $this->configFactory->get('vmware.settings')->get('vmware_queue_limit');
$update_resources_queue = $this->queueFactory->get('vmware_update_resources_queue');
if ($update_resources_queue->numberOfItems() >= $queue_limit) {
return;
}
$method_names = [
'updateVms',
'updateHosts',
];
foreach ($method_names as $method_name) {
$update_resources_queue->createItem([
'cloud_context' => $this->cloudContext,
'vmware_method_name' => $method_name,
]);
}
}
/**
* {@inheritdoc}
*/
public function getSessionId(): ?string {
return $this->sessionId;
}
}
......@@ -176,4 +176,17 @@ interface VmwareServiceInterface {
*/
public function describeResourcePools(array $params = []): array;
/**
* Create queue items for update resources queue.
*/
public function createResourceQueueItems(): void;
/**
* Get the current session id.
*
* @return null|string
* Either NULL or the session string.
*/
public function getSessionId(): ?string;
}
......@@ -337,3 +337,14 @@ function vmware_update_8219(): void {
drupal_flush_all_caches();
}
/**
* Add configuration for cron/queues.
*/
function vmware_update_8220(): void {
$config_factory = \Drupal::configFactory();
$config = $config_factory->getEditable('vmware.settings');
$config->set('vmware_update_resources_queue_cron_time', 5);
$config->set('vmware_queue_limit', 50);
$config->save();
}
......@@ -501,3 +501,27 @@ function vmware_entity_view_alter(array &$build, EntityInterface $entity, Entity
\Drupal::service('cloud')->reorderForm($build, vmware_server_template_field_orders(FALSE));
}
}
/**
* Implements hook_cron().
*/
function vmware_cron(): void {
$vmware_service = \Drupal::service('vmware');
// Update resources.
$config_entities = \Drupal::service('plugin.manager.cloud_config_plugin')
->loadConfigEntities('vmware');
foreach ($config_entities ?: [] as $config_entity) {
$vmware_service->setCloudContext($config_entity->getCloudContext());
$vmware_service->createResourceQueueItems();
}
}
/**
* Implements hook_queue_info_alter().
*/
function vmware_queue_info_alter(&$queues): void {
$config = \Drupal::config('vmware.settings');
if (!empty($queues['vmware_update_resources_queue'])) {
$queues['vmware_update_resources_queue']['cron']['time'] = $config->get('vmware_update_resources_queue_cron_time');
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment