Adjusting thigns to go to queue.

This commit is contained in:
calcu1on 2025-03-30 22:59:13 -04:00
parent c49263a78d
commit 18cb2c1a8b
9 changed files with 204 additions and 22 deletions

View File

@ -1,6 +0,0 @@
<!Doctype html>
<html lang="en">
<body>
<p>Welcome DANNANNDANN to my site.</p>
</body>
</html>

7
scripts/curltest.sh Normal file
View File

@ -0,0 +1,7 @@
#!/usr/bin/env bash
filename=$PWD/web/sites/default/files/api-responses/characters.json
if [ ! -f "${filename}" ]; then
touch "${filename}"
fi
curl -s "https://dchadwick.ddev.site/api/character-list" > $PWD/web/sites/default/files/api-responses/characters.json

View File

@ -25,3 +25,12 @@ function dc_core_taxo_form_submit($form, &$form_state) {
} }
function dc_core_theme() {
return [
'lotr_characters' => [
'variables' => [
'characters' => [],
],
],
];
}

View File

@ -411,7 +411,23 @@ protected function getCharacterJson(): array {
'bio' => 'Gothmog was the Lieutenant of Morgul and a high-ranking commander of Saurons forces in the War of the Ring. As one of the more prominent Orc leaders, he was present during the Battle of the Pelennor Fields, leading the forces of Mordor. Gothmogs grotesque appearance and ruthlessness made him a terrifying figure. He met his end in the Battle of the Black Gate, where his forces were destroyed and he himself was slain by the armies of Gondor and Rohan.', 'bio' => 'Gothmog was the Lieutenant of Morgul and a high-ranking commander of Saurons forces in the War of the Ring. As one of the more prominent Orc leaders, he was present during the Battle of the Pelennor Fields, leading the forces of Mordor. Gothmogs grotesque appearance and ruthlessness made him a terrifying figure. He met his end in the Battle of the Black Gate, where his forces were destroyed and he himself was slain by the armies of Gondor and Rohan.',
'image_url' => 'https://vignette.wikia.nocookie.net/lotr/images/9/94/Gothmog_Lieutenant_of_Morgul.png/revision/latest/scale-to-width-down/350?cb=20190602142219', 'image_url' => 'https://vignette.wikia.nocookie.net/lotr/images/9/94/Gothmog_Lieutenant_of_Morgul.png/revision/latest/scale-to-width-down/350?cb=20190602142219',
'youtube_url' => 'https://www.youtube.com/watch?v=hegeHfK5Dh4' 'youtube_url' => 'https://www.youtube.com/watch?v=hegeHfK5Dh4'
] ],
[
'name' => 'Dan Chadwick',
'species' => 'Human',
'height' => 'Huge',
'hair' => 'Brown',
'eyes' => 'Blue',
'birth' => 'Unknown',
'death' => 'Unknown, last seen leaving Middle-earth',
'title' => 'Drupalist',
'occupation' => 'Strugglbus',
'affiliation' => 'Twilight new moon',
'bio' => 'I am not sure what to put here.',
'image_url' => 'https://platform.polygon.com/wp-content/uploads/sites/2/chorus/uploads/chorus_asset/file/22263166/lotr3_movie_screencaps.com_10384.jpg',
'youtube_url' => ''
],
]; ];
return $characters; return $characters;

View File

@ -0,0 +1,33 @@
<?php
declare(strict_types=1);
namespace Drupal\dc_core\Plugin\Block;
use Drupal\Core\Block\Attribute\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\StringTranslation\TranslatableMarkup;
#[Block(
id: "characters_block",
admin_label: new TranslatableMarkup("LOTR Characters"),
category: new TranslatableMarkup("DC Core")
)]
final class CharactersBlock extends BlockBase {
/**
* {@inheritdoc}
*/
public function build(): array {
$path = 'public://api-responses/characters.json';
$characters = json_decode(file_get_contents($path), TRUE);
$build = [
'#theme' => 'lotr_characters',
'#characters' => $characters,
];
return $build;
}
}

View File

@ -0,0 +1,5 @@
{% for character in characters %}
<h2>{{ character.name }}</h2>
<h4>{{ character.title }}</h4>
<p>{{ character.bio }}</p>
{% endfor %}

View File

@ -21,7 +21,6 @@ class UfcCommands extends DrushCommands {
public function importFighters(): void { public function importFighters(): void {
// Fighter importer service. // Fighter importer service.
$fighter_importer = \Drupal::service('ufc.import_fighters'); $fighter_importer = \Drupal::service('ufc.import_fighters');
// First check for the item in cache. // First check for the item in cache.
$fighter_list = \Drupal::cache()->get($this->cacheId); $fighter_list = \Drupal::cache()->get($this->cacheId);
if (!$fighter_list) { if (!$fighter_list) {
@ -32,22 +31,19 @@ public function importFighters(): void {
$fighter_list = $fighter_list->data; $fighter_list = $fighter_list->data;
} }
// Add each division to a batch process. $fighter_import_queue = \Drupal::queue('fighter_import');
$batch = new BatchBuilder();
$batch->setTitle("Starting Complete Fighter Import")
->setFinishCallback([UfcCommands::class, 'importFinished'])
->setInitMessage("Importing...");
foreach ($fighter_list as $division => $fighters) { foreach ($fighter_list as $division => $fighters) {
$args = [ foreach ($fighters as $fighter) {
$division, $queue_item = new \stdClass();
$fighters, $queue_item->first_name = $fighter['firstname'];
]; $queue_item->last_name = $fighter['lastname'];
$batch->addOperation([FighterImporter::class, 'processDivision'], $args); $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();
} }
/** /**

View File

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

View File

@ -136,7 +136,6 @@ public static function processDivision($div, $fighters): void {
$fighter->createPlayerNode(); $fighter->createPlayerNode();
} }
} }
$context['results']['processed']++;
} }
/** /**