Skip to content

Commit

Permalink
Merge pull request #565 from City-of-Helsinki/UHF-10581
Browse files Browse the repository at this point in the history
UHF-10581
  • Loading branch information
rpnykanen authored Oct 9, 2024
2 parents b536d76 + bc76cf9 commit 3a541b2
Show file tree
Hide file tree
Showing 6 changed files with 421 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ description: 'Google indexing api integration'
package: HELfi
core_version_requirement: ^10 || ^11
dependencies:
- 'helfi_rekry_content:helfi_rekry_content'
- 'publication_date:publication_date'
- redirect:redirect
- scheduler:scheduler
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ services:

Drupal\helfi_google_api\EventSubscriber\JobPublishStateSubscriber: ~

Drupal\helfi_google_api\EventSubscriber\JobMigrationSubscriber: ~

helfi_google_api.google_service:
public: false
class: \Google\Service\Indexing
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

declare(strict_types=1);

namespace Drupal\helfi_google_api\EventSubscriber;

use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Queue\QueueFactory;
use Drupal\helfi_rekry_content\Entity\JobListing;
use Drupal\migrate\Event\MigrateEvents;
use Drupal\migrate\Event\MigratePostRowSaveEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
* Send indexing request ASAP after migration is done if necessary.
*/
class JobMigrationSubscriber implements EventSubscriberInterface {

/**
* The constructor.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
* The entity type manager.
* @param \Drupal\Core\Queue\QueueFactory $queueFactory
* The queue factory.
*/
public function __construct(
private EntityTypeManagerInterface $entityTypeManager,
private QueueFactory $queueFactory,
) {
}

/**
* {@inheritdoc}
*/
public static function getSubscribedEvents(): array {
return [
MigrateEvents::POST_ROW_SAVE => 'postRowSave',
];
}

/**
* Send indexing request to google.
*
* @param \Drupal\migrate\Event\MigratePostRowSaveEvent $event
* The scheduler event.
*/
public function postRowSave(MigratePostRowSaveEvent $event): void {
$id = $event->getDestinationIdValues();
$id = reset($id);

$node = $this->entityTypeManager
->getStorage('node')
->load($id);

if (
!$node instanceof JobListing ||
!$this->shouldRequestIndexingImmediately($node)
) {
return;
}

$this->queueFactory->get('job_listing_indexing_request')
->createItem(['nid' => $node->id()]);
}

/**
* Do we need to send indexing request right away ?
*
* @param \Drupal\helfi_rekry_content\Entity\JobListing $node
* The job listing node.
*
* @return bool
* Node should be indexed right now.
*/
private function shouldRequestIndexingImmediately(JobListing $node): bool {
return $node->isPublished() || (!$node->get('publish_on')->isEmpty() && $node->isPublished());
}

}
2 changes: 1 addition & 1 deletion public/modules/custom/helfi_google_api/src/GoogleApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function __construct(
public function isDryRun(): bool {
$config = $this->configFactory->get('helfi_google_api.settings');
$key = $config->get('indexing_api_key') ?: '';
$dryRun = $config->get('dry_run') ?: TRUE;
$dryRun = $config->get('dry_run') === FALSE ? FALSE : TRUE;

return !$key || $dryRun;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

declare(strict_types=1);

namespace Drupal\helfi_google_api\Plugin\QueueWorker;

use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Queue\QueueWorkerBase;
use Drupal\Core\Queue\RequeueException;
use Drupal\helfi_google_api\JobIndexingService;
use Drupal\helfi_rekry_content\Entity\JobListing;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
* Index the migrated job listings which are published on migrate.
*
* @QueueWorker(
* id = "job_listing_indexing_request",
* title = @Translation("Job listing indexing request worker"),
* cron = {"time" = 60}
* )
*/
final class IndexingWorker extends QueueWorkerBase implements ContainerFactoryPluginInterface {

/**
* The 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 $entityTypeManager
* The entity type manager.
* @param \Drupal\helfi_google_api\JobIndexingService $jobIndexingService
* The job indexing service.
*/
public function __construct(
array $configuration,
$plugin_id,
$plugin_definition,
protected EntityTypeManagerInterface $entityTypeManager,
protected JobIndexingService $jobIndexingService,
) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
}

/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) : self {
return new self(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('entity_type.manager'),
$container->get('Drupal\helfi_google_api\JobIndexingService'),
);
}

/**
* {@inheritdoc}
*/
public function processItem($data): void {
if (!isset($data['nid'])) {
return;
}

$nodeStorage = $this->entityTypeManager->getStorage('node');
$node = $nodeStorage->load($data['nid']);

if (!$node instanceof JobListing) {
return;
}

try {
$this->jobIndexingService->indexEntity($node);
}
catch (\Exception $e) {
throw new RequeueException();
}
}

}
Loading

0 comments on commit 3a541b2

Please sign in to comment.