Adjusting thigns to go to queue.
This commit is contained in:
@@ -21,7 +21,6 @@ class UfcCommands extends DrushCommands {
|
||||
public function importFighters(): void {
|
||||
// Fighter importer service.
|
||||
$fighter_importer = \Drupal::service('ufc.import_fighters');
|
||||
|
||||
// First check for the item in cache.
|
||||
$fighter_list = \Drupal::cache()->get($this->cacheId);
|
||||
if (!$fighter_list) {
|
||||
@@ -32,22 +31,19 @@ class UfcCommands extends DrushCommands {
|
||||
$fighter_list = $fighter_list->data;
|
||||
}
|
||||
|
||||
// Add each division to a batch process.
|
||||
$batch = new BatchBuilder();
|
||||
$batch->setTitle("Starting Complete Fighter Import")
|
||||
->setFinishCallback([UfcCommands::class, 'importFinished'])
|
||||
->setInitMessage("Importing...");
|
||||
$fighter_import_queue = \Drupal::queue('fighter_import');
|
||||
foreach ($fighter_list as $division => $fighters) {
|
||||
$args = [
|
||||
$division,
|
||||
$fighters,
|
||||
];
|
||||
$batch->addOperation([FighterImporter::class, 'processDivision'], $args);
|
||||
foreach ($fighters as $fighter) {
|
||||
$queue_item = new \stdClass();
|
||||
$queue_item->first_name = $fighter['firstname'];
|
||||
$queue_item->last_name = $fighter['lastname'];
|
||||
$queue_item->image = $fighter['image'];
|
||||
$queue_item->division = $division;
|
||||
$queue_item->profile = $fighter['profile'];
|
||||
$fighter_import_queue->createItem($queue_item);
|
||||
}
|
||||
}
|
||||
|
||||
// Set and run the batch.
|
||||
batch_set($batch->toArray());
|
||||
drush_backend_batch_process();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,123 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\ufc\Plugin\QueueWorker;
|
||||
|
||||
use Drupal\Core\Annotation\QueueWorker;
|
||||
use Drupal\Core\Entity\EntityTypeManagerInterface;
|
||||
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
|
||||
use Drupal\Core\Queue\QueueWorkerBase;
|
||||
use Drupal\ufc\Fighter;
|
||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
|
||||
/**
|
||||
* Custom Queue Worker.
|
||||
*
|
||||
* @QueueWorker(
|
||||
* id = "fighter_import",
|
||||
* title = @Translation("Fighter Import Queue"),
|
||||
* cron = {"time" = 2400}
|
||||
* )
|
||||
*/
|
||||
final class FighterQueueWorker extends QueueWorkerBase implements ContainerFactoryPluginInterface {
|
||||
|
||||
/**
|
||||
* The entity type manager.
|
||||
*
|
||||
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
|
||||
*/
|
||||
protected $entityTypeManager;
|
||||
|
||||
/**
|
||||
* Main constructor.
|
||||
*
|
||||
* @param array $configuration
|
||||
* Configuration array.
|
||||
* @param mixed $plugin_id
|
||||
* The plugin id.
|
||||
* @param mixed $plugin_definition
|
||||
* The plugin definition.
|
||||
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
|
||||
* The entity type manager.
|
||||
*/
|
||||
public function __construct(
|
||||
array $configuration,
|
||||
$plugin_id,
|
||||
$plugin_definition,
|
||||
EntityTypeManagerInterface $entityTypeManager
|
||||
) {
|
||||
parent::__construct($configuration, $plugin_id, $plugin_definition);
|
||||
$this->entityTypeManager = $entityTypeManager;
|
||||
}
|
||||
|
||||
/**
|
||||
* Used to grab functionality from the container.
|
||||
*
|
||||
* @param \Symfony\Component\DependencyInjection\ContainerInterface $container
|
||||
* The container.
|
||||
* @param array $configuration
|
||||
* Configuration array.
|
||||
* @param mixed $plugin_id
|
||||
* The plugin id.
|
||||
* @param mixed $plugin_definition
|
||||
* The plugin definition.
|
||||
* @return static
|
||||
*/
|
||||
public static function create(
|
||||
ContainerInterface $container,
|
||||
array $configuration,
|
||||
$plugin_id,
|
||||
$plugin_definition
|
||||
) {
|
||||
return new static(
|
||||
$configuration,
|
||||
$plugin_id,
|
||||
$plugin_definition,
|
||||
$container->get('entity_type.manager'),
|
||||
$container->get('database'),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Processes an item in the queue.
|
||||
*
|
||||
* @param mixed $data
|
||||
* The queue item data.
|
||||
*
|
||||
*/
|
||||
public function processItem($data): void {
|
||||
$this->doProcess($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Do the actual processing of an item.
|
||||
*
|
||||
* @param $item
|
||||
* The item to process.
|
||||
*/
|
||||
private function doProcess($item): bool {
|
||||
$fighter = new Fighter(\Drupal::httpClient());
|
||||
$fighter->first_name = $item->first_name;
|
||||
$fighter->last_name = $item->last_name;
|
||||
$fighter->image = $item->image;
|
||||
$fighter->class = $item->division;
|
||||
if (!$fighter->scrapeDataFromFighterPage($item->profile)) {
|
||||
\Drupal::logger('ufc')->alert("FAILED: $fighter->first_name $fighter->last_name to " . $item->profile);
|
||||
}
|
||||
// Check if node exists, by title.
|
||||
$fighter->createMediaEntityFromImage();
|
||||
$title = $fighter->first_name . " " . $fighter->last_name;
|
||||
$node_lookup = reset(\Drupal::entityTypeManager()->getStorage('node')->loadByProperties(['title' => $title]));
|
||||
|
||||
if (!empty($node_lookup)) {
|
||||
// Update instead of create.
|
||||
$fighter->updatePlayerNode($node_lookup->id());
|
||||
\Drupal::logger('ufc')->notice("$title updated successfully.");
|
||||
}
|
||||
else {
|
||||
\Drupal::logger('ufc')->warning("No existing player found for $title...creating");
|
||||
$fighter->createPlayerNode();
|
||||
}
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -136,7 +136,6 @@ class FighterImporter {
|
||||
$fighter->createPlayerNode();
|
||||
}
|
||||
}
|
||||
$context['results']['processed']++;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user