Skip to content

Commit

Permalink
Allow force update a solr document.
Browse files Browse the repository at this point in the history
  • Loading branch information
TiSiE committed Sep 27, 2019
1 parent a48320b commit b84ce60
Showing 1 changed file with 45 additions and 21 deletions.
66 changes: 45 additions & 21 deletions src/Listener/JobEventSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Doctrine\ODM\MongoDB\Events;
use Interop\Container\ContainerInterface;
use Jobs\Entity\Job;
use Jobs\Entity\JobInterface;
use Jobs\Entity\StatusInterface;
use Solr\Bridge\Manager;
use Solr\Filter\EntityToDocument\JobEntityToSolrDocument as EntityToDocumentFilter;
Expand All @@ -25,6 +26,7 @@
* @author Anthonius Munthi <me@itstoni.com>
* @author Miroslav Fedeleš <miroslav.fedeles@gmail.com>
* @since 0.26
* @since 0.8.2 Allow forced updates on documents.
* @package Solr\Listener
*/
class JobEventSubscriber implements EventSubscriber
Expand All @@ -34,7 +36,7 @@ class JobEventSubscriber implements EventSubscriber
* @var Manager
*/
protected $solrManager;

/**
* @var EntityToDocumentFilter
*/
Expand All @@ -44,17 +46,22 @@ class JobEventSubscriber implements EventSubscriber
* @var SolrClient
*/
protected $solrClient;

/**
* @var Job[]
*/
protected $add = [];

/**
* @var Job[]
*/
protected $delete = [];


/**
* @var string[]
*/
protected $forceUpdates = [];

/**
* @param Manager $manager
* @param EntityToDocumentFilter $entityToDocumentFilter
Expand All @@ -64,7 +71,20 @@ public function __construct(Manager $manager, EntityToDocumentFilter $entityToDo
$this->solrManager = $manager;
$this->entityToDocumentFilter = $entityToDocumentFilter;
}


/**
* @param JobInterface|string $jobOrId
* @since 0.8.2
*/
public function forceUpdate($jobOrId): void
{
if ($jobOrId instanceof JobInterface) {
$jobOrId = $jobOrId->getId();
}

$this->forceUpdates[] = $jobOrId;
}

/**
* Define what event this subscriber listen to
*
Expand All @@ -78,52 +98,56 @@ public function getSubscribedEvents()
Events::postFlush
];
}

/**
* @param LifecycleEventArgs $eventArgs
* @since 0.30
*/
public function prePersist(LifecycleEventArgs $eventArgs)
{
$document = $eventArgs->getDocument();

// check for a job instance
if (!$document instanceof Job) {
return;
}

// check if the job is active
if ($document->isActive()) {
// mark it for commit
$this->add[] = $document;
}
}

/**
* @param PreUpdateEventArgs $eventArgs
* @since 0.27
* @since 0.8.2 Check for forced updates on documents
*/
public function preUpdate(PreUpdateEventArgs $eventArgs)
{
$document = $eventArgs->getDocument();

// check for a job instance
if (!$document instanceof Job) {
return;
}

// check if the status or isDeleted flag has been changed
if (!$eventArgs->hasChangedField('status') && !$eventArgs->hasChangedField('isDeleted')) {
if (!in_array($document->getId(), $this->forceUpdates)
&& !$eventArgs->hasChangedField('status')
&& !$eventArgs->hasChangedField('isDeleted')
) {
return;
}

// check if the job is active
if ($document->isActive() && !$document->isDeleted()) {
// mark it for commit
$this->add[] = $document;
} else {
$status = $document->getStatus();

// check if the status has been changed to inactive or expired
// or isDeleted Flag is set.
if ($document->isDeleted()
Expand All @@ -137,7 +161,7 @@ public function preUpdate(PreUpdateEventArgs $eventArgs)
}
}
}

/**
* @param PostFlushEventArgs $eventArgs
* @since 0.27
Expand All @@ -148,20 +172,20 @@ public function postFlush(PostFlushEventArgs $eventArgs)
if (!$this->add && !$this->delete) {
return;
}

$client = $this->getSolrClient();

// process jobs for commit
foreach ($this->add as $job) {
$document = $this->entityToDocumentFilter->filter($job);
$client->addDocument($document);
}

// process jobs for delete
foreach ($this->delete as $job) {
$client->deleteByIds($this->entityToDocumentFilter->getDocumentIds($job));
}

// commit to index & optimize it
$client = $this->getSolrClient();
$client->commit(true, false);
Expand All @@ -172,7 +196,7 @@ public function postFlush(PostFlushEventArgs $eventArgs)
$this->add = [];
$this->delete = [];
}

/**
* @return SolrClient
* @since 0.27
Expand All @@ -183,7 +207,7 @@ protected function getSolrClient()
$path = $this->solrManager->getOptions()->getJobsPath();
$this->solrClient = $this->solrManager->getClient($path);
}

return $this->solrClient;
}

Expand Down

0 comments on commit b84ce60

Please sign in to comment.