diff --git a/docker/openshift/crons/migrate-changed-job-listings.sh b/docker/openshift/crons/migrate-changed-job-listings.sh index 655d7dbb..12f1b580 100644 --- a/docker/openshift/crons/migrate-changed-job-listings.sh +++ b/docker/openshift/crons/migrate-changed-job-listings.sh @@ -1,11 +1,18 @@ #!/bin/bash +migrations=( + "helfi_rekry_jobs:all" +) + echo "Starting job listing:changed migrations: $(date)" while true do echo "Running job listing:changed migrations: $(date)" - # Allow migrations to be run every 10 minutes, reset stuck migrations every 30 minutes. - drush migrate:import helfi_rekry_jobs:changed --update --reset-threshold 1800 --interval 600 --no-progress + + for migration in "${migrations[@]}"; do + # Allow migrations to be run every 10 minutes, reset stuck migrations every 30 minutes. + drush migrate:import "$migration" --reset-threshold 1800 --interval 600 --no-progress + done # Sleep for 15 minutes. sleep 900 diff --git a/docker/openshift/crons/migrate-job-listings.sh b/docker/openshift/crons/migrate-job-listings.sh index f7fd0d21..152b36fa 100644 --- a/docker/openshift/crons/migrate-job-listings.sh +++ b/docker/openshift/crons/migrate-job-listings.sh @@ -11,7 +11,6 @@ migrations=( "helfi_rekry_organizations:en" "helfi_rekry_employments:all" "helfi_rekry_employment_types:all" - "helfi_rekry_jobs:all" ) echo "Starting job listing migrations: $(date)" diff --git a/public/modules/custom/helfi_rekry_content/src/EventSubscriber/JobListingHideMissingSubscriber.php b/public/modules/custom/helfi_rekry_content/src/EventSubscriber/JobListingHideMissingSubscriber.php index e2ae1593..d97efc7b 100644 --- a/public/modules/custom/helfi_rekry_content/src/EventSubscriber/JobListingHideMissingSubscriber.php +++ b/public/modules/custom/helfi_rekry_content/src/EventSubscriber/JobListingHideMissingSubscriber.php @@ -7,6 +7,7 @@ use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Queue\QueueFactory; use Drupal\Core\StringTranslation\StringTranslationTrait; +use Drupal\node\NodeInterface; use Drush\Drupal\Migrate\MigrateMissingSourceRowsEvent; use Psr\Log\LoggerInterface; use Symfony\Component\DependencyInjection\Attribute\Autowire; @@ -83,14 +84,21 @@ function ($values, $destinationValue) { return; } - // Query missing nodes that are still published. - $query = $this->entityTypeManager->getStorage('node') - ->getQuery() + $query = $this->entityTypeManager + ->getStorage('node') + ->getQuery(); + + // Query missing nodes that are still published + // or still scheduled to be published. + $orCondition = $query->orConditionGroup(); + $orCondition->condition('status', NodeInterface::PUBLISHED); + $orCondition->exists('publish_on'); + + $nids = $query ->accessCheck(FALSE) ->condition('nid', $destinationIds, 'IN') - ->condition('status', 1) - ->notExists('unpublish_on'); - $nids = $query->execute(); + ->condition($orCondition) + ->execute(); foreach ($nids as $nid) { $job = ['nid' => $nid]; diff --git a/public/modules/custom/helfi_rekry_content/src/Plugin/QueueWorker/UnpublishWorker.php b/public/modules/custom/helfi_rekry_content/src/Plugin/QueueWorker/UnpublishWorker.php index e810e311..427b8b4b 100644 --- a/public/modules/custom/helfi_rekry_content/src/Plugin/QueueWorker/UnpublishWorker.php +++ b/public/modules/custom/helfi_rekry_content/src/Plugin/QueueWorker/UnpublishWorker.php @@ -6,20 +6,21 @@ use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; +use Drupal\Core\Queue\Attribute\QueueWorker; use Drupal\Core\Queue\QueueWorkerBase; +use Drupal\Core\StringTranslation\TranslatableMarkup; use Drupal\node\NodeInterface; use Psr\Log\LoggerInterface; use Symfony\Component\DependencyInjection\ContainerInterface; /** * Queue Worker for removing job listings not present in source. - * - * @QueueWorker( - * id = "job_listing_unpublish_worker", - * title = @Translation("Job listing unpublish worker"), - * cron = {"time" = 60} - * ) */ +#[QueueWorker( + id: 'job_listing_unpublish_worker', + title: new TranslatableMarkup('Job listing unpublish worker'), + cron: ['time' => 60], +)] final class UnpublishWorker extends QueueWorkerBase implements ContainerFactoryPluginInterface { /** @@ -74,7 +75,9 @@ public function processItem($data): void { // Unpublish all translations. if ($node instanceof NodeInterface && $node->getType() == 'job_listing') { - foreach (['fi', 'sv', 'en'] as $langcode) { + foreach ($node->getTranslationLanguages() as $language) { + $langcode = $language->getId(); + // Unpublish the job listing node as it's still published, but it's // no longer available at the source. if (!$node->hasTranslation($langcode)) { @@ -82,15 +85,15 @@ public function processItem($data): void { } $translation = $node->getTranslation($langcode); - if ($translation->isPublished()) { - $translation->setUnpublished(); - if ($translation->hasField('publish_on') && !empty($translation->get('publish_on')->getValue())) { - // Also clear the publish on date to make sure the translation is - // not going to be re-published. - $translation->set('publish_on', NULL); - } - $translation->save(); + $translation->setUnpublished(); + + // Also clear the published on date so the translation + // is not going to be re-published. + if ($translation->hasField('publish_on') && !empty($translation->get('publish_on')->getValue())) { + $translation->set('publish_on', NULL); } + + $translation->save(); } } diff --git a/public/modules/custom/helfi_rekry_content/tests/src/Kernel/UnpublishWorkerTest.php b/public/modules/custom/helfi_rekry_content/tests/src/Kernel/UnpublishWorkerTest.php new file mode 100644 index 00000000..329cb233 --- /dev/null +++ b/public/modules/custom/helfi_rekry_content/tests/src/Kernel/UnpublishWorkerTest.php @@ -0,0 +1,105 @@ +installEntitySchema('node'); + $this->installEntitySchema('user'); + $this->installSchema('node', 'node_access'); + $this->installConfig(['node', 'filter']); + $this->createContentType(['type' => 'job_listing']); + } + + /** + * Tests unpublish worker. + */ + public function testUnpublishWorker(): void { + $manager = $this->container->get('plugin.manager.queue_worker'); + $sut = $manager->createInstance('job_listing_unpublish_worker'); + + $published_node = $this->createNode([ + 'type' => 'job_listing', + 'status' => NodeInterface::PUBLISHED, + ]); + + $scheduled_node = $this->createNode([ + 'type' => 'job_listing', + 'status' => NodeInterface::NOT_PUBLISHED, + ]); + + ConfigurableLanguage::createFromLangcode('sv')->save(); + $translated_node = $this->createNode([ + 'type' => 'job_listing', + 'langcode' => 'en', + 'status' => NodeInterface::NOT_PUBLISHED, + ]); + $translated_node->addTranslation('sv', [ + 'title' => $this->randomMachineName(), + 'status' => NodeInterface::PUBLISHED, + ])->save(); + + $sut->processItem(['nid' => $published_node->id()]); + $this->assertNodeIsUnpublished($published_node->id()); + + $sut->processItem(['nid' => $scheduled_node->id()]); + $this->assertNodeIsUnpublished($scheduled_node->id()); + + $sut->processItem(['nid' => $translated_node->id()]); + $this->assertNodeIsUnpublished($translated_node->id()); + } + + /** + * Asserts that node is no longer published. + */ + private function assertNodeIsUnpublished(string $nid): void { + /** @var \Drupal\node\NodeInterface $node */ + $node = $this->container->get(EntityTypeManagerInterface::class) + ->getStorage('node') + ->load($nid); + + foreach ($node->getTranslationLanguages() as $language) { + $translation = $node->getTranslation($language->getId()); + $this->assertFalse($translation->isPublished()); + $this->assertEmpty($translation->get('publish_on')->getValue()); + } + } + +}