-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #565 from City-of-Helsinki/UHF-10581
UHF-10581
- Loading branch information
Showing
6 changed files
with
421 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
public/modules/custom/helfi_google_api/src/EventSubscriber/JobMigrationSubscriber.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
public/modules/custom/helfi_google_api/src/Plugin/QueueWorker/IndexingWorker.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.