Skip to content

Commit

Permalink
implement signal watcher
Browse files Browse the repository at this point in the history
  • Loading branch information
solverat committed Jul 13, 2019
1 parent d34a6bf commit 0dda65f
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 11 deletions.
12 changes: 5 additions & 7 deletions src/DsTrinityDataBundle/Normalizer/DefaultResourceNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,12 @@ protected function normalizeDocument(ContextDataInterface $contextData, Resource
/** @var Document $document */
$document = $resourceContainer->getResource();

if (!method_exists($document, 'getProperty')) {
return [];
}

// @todo: Hardlink data detection!
// @todo: Related document detection! (some content parts could be inherited)
// @todo: How to handle Snippets?
$documentId = sprintf('%s_%d', 'document', $document->getId());
$resourceMeta = new ResourceMeta($documentId, $document->getId(), 'document', $document->getType());
$resourceMeta = new ResourceMeta($documentId, $document->getId(), 'document', $document->getType(), ['id' => $document->getId()]);
$returnResourceContainer = $contextData->getContextDispatchType() === ContextDataInterface::CONTEXT_DISPATCH_TYPE_DELETE ? null : $resourceContainer;

return [new NormalizedDataResource($returnResourceContainer, $resourceMeta)];
Expand All @@ -70,7 +68,7 @@ protected function normalizeAsset(ContextDataInterface $contextData, ResourceCon
$asset = $resourceContainer->getResource();

$documentId = sprintf('%s_%d', 'asset', $asset->getId());
$resourceMeta = new ResourceMeta($documentId, $asset->getId(), 'asset', $asset->getType());
$resourceMeta = new ResourceMeta($documentId, $asset->getId(), 'asset', $asset->getType(), ['id' => $asset->getId()]);
$returnResourceContainer = $contextData->getContextDispatchType() === ContextDataInterface::CONTEXT_DISPATCH_TYPE_DELETE ? null : $resourceContainer;

return [new NormalizedDataResource($returnResourceContainer, $resourceMeta)];
Expand All @@ -88,7 +86,7 @@ protected function normalizeDataObject(ContextDataInterface $contextData, Resour
$object = $resourceContainer->getResource();

$documentId = sprintf('%s_%d', 'object', $object->getId());
$resourceMeta = new ResourceMeta($documentId, $object->getId(), 'object', $object->getType());
$resourceMeta = new ResourceMeta($documentId, $object->getId(), 'object', $object->getType(), ['id' => $object->getId()]);
$returnResourceContainer = $contextData->getContextDispatchType() === ContextDataInterface::CONTEXT_DISPATCH_TYPE_DELETE ? null : $resourceContainer;

return [new NormalizedDataResource($returnResourceContainer, $resourceMeta)];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ protected function normalizeDocument(ContextDataInterface $contextData, Resource

// @todo: Hardlink data detection!
// @todo: Related document detection! (some content parts could be inherited)
// @todo: How to handle Snippets?
$documentLocale = $document->getProperty('language');
if (empty($documentLocale)) {
Expand All @@ -70,7 +71,7 @@ protected function normalizeDocument(ContextDataInterface $contextData, Resource
}

$documentId = sprintf('%s_%s_%d', 'document', $documentLocale, $document->getId());
$resourceMeta = new ResourceMeta($documentId, $document->getId(), 'document', $document->getType(), ['id' => $document->getId()]);
$resourceMeta = new ResourceMeta($documentId, $document->getId(), 'document', $document->getType(), ['id' => $document->getId()], ['locale' => $documentLocale]);
$returnResourceContainer = $contextData->getContextDispatchType() === ContextDataInterface::CONTEXT_DISPATCH_TYPE_DELETE ? null : $resourceContainer;

return [new NormalizedDataResource($returnResourceContainer, $resourceMeta)];
Expand All @@ -89,7 +90,7 @@ protected function normalizeAsset(ContextDataInterface $contextData, ResourceCon
$asset = $resourceContainer->getResource();

$documentId = sprintf('%s_%d', 'asset', $asset->getId());
$resourceMeta = new ResourceMeta($documentId, $asset->getId(), 'asset', $asset->getType(), ['id' => $asset->getId()]);
$resourceMeta = new ResourceMeta($documentId, $asset->getId(), 'asset', $asset->getType(), ['id' => $asset->getId()], ['locale' => null]);
$returnResourceContainer = $contextData->getContextDispatchType() === ContextDataInterface::CONTEXT_DISPATCH_TYPE_DELETE ? null : $resourceContainer;

return [new NormalizedDataResource($returnResourceContainer, $resourceMeta)];
Expand All @@ -109,7 +110,7 @@ protected function normalizeDataObject(ContextDataInterface $contextData, Resour
$normalizedResources = [];
foreach ($this->options['locales'] as $locale) {
$documentId = sprintf('%s_%s_%d', 'object', $locale, $object->getId());
$resourceMeta = new ResourceMeta($documentId, $object->getId(), 'object', $object->getType(), ['id' => $object->getId()]);
$resourceMeta = new ResourceMeta($documentId, $object->getId(), 'object', $object->getType(), ['id' => $object->getId()], ['locale' => $locale]);
$returnResourceContainer = $contextData->getContextDispatchType() === ContextDataInterface::CONTEXT_DISPATCH_TYPE_DELETE ? null : $resourceContainer;
$normalizedResources[] = new NormalizedDataResource($returnResourceContainer, $resourceMeta);
}
Expand Down
44 changes: 43 additions & 1 deletion src/DsTrinityDataBundle/Service/DataProviderService.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
use DsTrinityDataBundle\DsTrinityDataBundle;
use DsTrinityDataBundle\Registry\DataBuilderRegistryInterface;
use DsTrinityDataBundle\Service\Builder\DataBuilderInterface;
use DsWebCrawlerBundle\DsWebCrawlerBundle;
use DynamicSearchBundle\DynamicSearchEvents;
use DynamicSearchBundle\Event\ErrorEvent;
use DynamicSearchBundle\Event\NewDataEvent;
use DynamicSearchBundle\Logger\LoggerInterface;
use DynamicSearchBundle\Normalizer\Resource\ResourceMetaInterface;
Expand Down Expand Up @@ -88,6 +90,8 @@ public function setIndexOptions(array $indexOptions)
*/
public function fetchListData()
{
$this->addSignalListener();

foreach (['asset', 'document', 'object'] as $type) {
$this->fetchByType($type, DataProviderInterface::PROVIDER_BEHAVIOUR_FULL_DISPATCH);
}
Expand All @@ -98,7 +102,7 @@ public function fetchListData()
*/
public function fetchSingleData(ResourceMetaInterface $resourceMeta)
{
$elementType = $resourceMeta->getResourceType();
$elementType = $resourceMeta->getResourceCollectionType();
$elementId = $resourceMeta->getResourceId();

if (!in_array($elementType, ['asset', 'document', 'object'])) {
Expand Down Expand Up @@ -180,9 +184,47 @@ protected function log($level, $message)
protected function dispatchData(array $elements, string $providerBehaviour, ?ResourceMetaInterface $resourceMeta = null)
{
foreach ($elements as $element) {

$newDataEvent = new NewDataEvent($this->contextDispatchType, $this->contextName, $element, $providerBehaviour, $resourceMeta);
$this->eventDispatcher->dispatch(DynamicSearchEvents::NEW_DATA_AVAILABLE, $newDataEvent);

$this->dispatchProcessControlSignal();

}
}

protected function addSignalListener()
{
if (php_sapi_name() !== 'cli') {
return;
}

if (!function_exists('pcntl_signal')) {
return;
}

declare(ticks=1);

pcntl_signal(SIGTERM, [$this, 'handleSignal']);
pcntl_signal(SIGINT, [$this, 'handleSignal']);
pcntl_signal(SIGHUP, [$this, 'handleSignal']);
pcntl_signal(SIGQUIT, [$this, 'handleSignal']);

}

protected function dispatchProcessControlSignal()
{
if (!function_exists('pcntl_signal')) {
return;
}

pcntl_signal_dispatch();
}

public function handleSignal($signal)
{
$newDataEvent = new ErrorEvent($this->contextName, sprintf('crawler has been stopped by user (signal: %s)', $signal), DsWebCrawlerBundle::PROVIDER_NAME);
$this->eventDispatcher->dispatch(DynamicSearchEvents::ERROR_DISPATCH_ABORT, $newDataEvent);
}

/**
Expand Down

0 comments on commit 0dda65f

Please sign in to comment.