Skip to content
Snippets Groups Projects
Commit 0981d205 authored by Marcus Johansson's avatar Marcus Johansson Committed by Marcus Johansson
Browse files

Issue #3416665 by Marcus_Johansson: Status field is not updated on queueworker

parent cc744c7e
Branches
Tags
No related merge requests found
Pipeline #81783 passed
......@@ -3,9 +3,11 @@
namespace Drupal\ai_interpolator\Plugin\QueueWorker;
use Drupal\ai_interpolator\AiInterpolatorRuleRunner;
use Drupal\ai_interpolator\AiInterpolatorStatusField;
use Drupal\ai_interpolator\Exceptions\AiInterpolatorRequestErrorException;
use Drupal\ai_interpolator\Exceptions\AiInterpolatorResponseErrorException;
use Drupal\ai_interpolator\Exceptions\AiInterpolatorRuleNotFoundException;
use Drupal\Core\Database\Connection;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
......@@ -38,6 +40,11 @@ class InterpolatorFieldData extends QueueWorkerBase implements ContainerFactoryP
*/
protected LoggerChannelFactoryInterface $loggerFactory;
/**
* The Drupal database connection.
*/
protected Connection $db;
/**
* Constructor.
*
......@@ -53,12 +60,15 @@ class InterpolatorFieldData extends QueueWorkerBase implements ContainerFactoryP
* The entity type manager.
* @param \Drupal\Core\Logger\LoggerChannelFactoryInterface $loggerFactory
* The logger factory.
* @param \Drupal\Core\Database\Connection $db
* The database connection.
*/
final public function __construct(array $configuration, $plugin_id, $plugin_definition, AiInterpolatorRuleRunner $aiRunner, EntityTypeManagerInterface $entityTypeManager, LoggerChannelFactoryInterface $loggerFactory) {
final public function __construct(array $configuration, $plugin_id, $plugin_definition, AiInterpolatorRuleRunner $aiRunner, EntityTypeManagerInterface $entityTypeManager, LoggerChannelFactoryInterface $loggerFactory, Connection $db) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->aiRunner = $aiRunner;
$this->entityTypeManager = $entityTypeManager;
$this->loggerFactory = $loggerFactory;
$this->db = $db;
}
/**
......@@ -71,7 +81,8 @@ class InterpolatorFieldData extends QueueWorkerBase implements ContainerFactoryP
$plugin_definition,
$container->get('ai_interpolator.rule_runner'),
$container->get('entity_type.manager'),
$container->get('logger.factory')
$container->get('logger.factory'),
$container->get('database')
);
}
......@@ -91,6 +102,10 @@ class InterpolatorFieldData extends QueueWorkerBase implements ContainerFactoryP
$entity = $this->aiRunner->generateResponse($newEntity, $data['fieldDefinition'], $data['interpolatorConfig']);
// Turn off the hook.
_ai_interpolator_entity_can_save_toggle(FALSE);
// Check if its the last queue item for the entity and reset processing.
if ($this->lastInQueue($entity->getEntityTypeId(), $entity->id())) {
$entity->set('ai_interpolator_status', AiInterpolatorStatusField::STATUS_FINISHED);
}
// Resave.
$success = $entity->save();
// Turn on the hook.
......@@ -122,7 +137,39 @@ class InterpolatorFieldData extends QueueWorkerBase implements ContainerFactoryP
'%message' => $e->getMessage(),
]);
}
// Since it failed.
$entity = $this->entityTypeManager->getStorage($data['entity_type'])->load($data['entity_id']);
$entity->set('ai_interpolator_status', AiInterpolatorStatusField::STATUS_FAILED);
$entity->save();
}
/**
* Check if its the last processed item for that entity.
*
* @param string $entityType
* The entity type.
* @param int $entityId
* The entity id.
*
* @return bool
* If its the last item.
*/
protected function lastInQueue($entityType, $entityId) {
$query = $this->db->select('queue', 'q');
$query
->fields('q', ['data'])
->condition('name', 'ai_interpolator_field_modifier');
$result = $query->execute();
// Amount found.
$amount = 0;
foreach ($result as $record) {
$data = unserialize($record->data, ['allowed_classes' => FALSE]);
if ($data['entity_type'] === $entityType && $data['entity_id'] === $entityId) {
$amount++;
}
}
// Since itself still counts, 1 item is the last.
return $amount <= 1;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment