Skip to content

Commit

Permalink
implement signal listener for tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
solverat committed Sep 12, 2018
1 parent 2c51c86 commit b095ebc
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 24 deletions.
6 changes: 5 additions & 1 deletion src/LuceneSearchBundle/LuceneSearchBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
class LuceneSearchBundle extends AbstractPimcoreBundle
{
use PackageVersionTrait;

const PACKAGE_NAME = 'dachcom-digital/lucene-search';

/**
Expand All @@ -32,7 +33,7 @@ public function getInstaller()
}

/**
* @return string[]
* {@inheritdoc}
*/
public function getJsPaths()
{
Expand All @@ -42,6 +43,9 @@ public function getJsPaths()
];
}

/**
* {@inheritdoc}
*/
public function getCssPaths()
{
return [
Expand Down
39 changes: 37 additions & 2 deletions src/LuceneSearchBundle/Task/AbstractTask.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare (ticks=1);

namespace LuceneSearchBundle\Task;

use LuceneSearchBundle\Configuration\Configuration;
Expand All @@ -8,6 +10,13 @@

abstract class AbstractTask implements TaskInterface
{
/**
* Used for logging identification
*
* @var string
*/
protected $prefix = 'task.abstract';

/**
* @var Configuration
*/
Expand All @@ -19,12 +28,12 @@ abstract class AbstractTask implements TaskInterface
protected $handlerDispatcher;

/**
* AbstractLogger
* @var AbstractLogger
*/
protected $logger;

/**
* array
* @var array
*/
protected $options;

Expand Down Expand Up @@ -95,6 +104,32 @@ public function log($message, $level = 'debug', $logToBackend = true, $logToSyst
$this->logger->log($message, $level, $logToBackend, $logToSystem);
}

/**
* Add Signal Listener to allow task cancellation and clean up
*/
public function addSignalListener()
{
if (php_sapi_name() === 'cli') {
if (function_exists('pcntl_signal')) {
pcntl_signal(SIGTERM, [$this, 'handleCliSignal']);
pcntl_signal(SIGINT, [$this, 'handleCliSignal']);
pcntl_signal(SIGHUP, [$this, 'handleCliSignal']);
pcntl_signal(SIGQUIT, [$this, 'handleCliSignal']);
}
}
}

/**
* Simple kill process if no callback has been defined.
*
* @param null $signo
*/
public function handleCliSignal($signo = null)
{
$this->log(sprintf('[task.%s] has been interrupted by signal (%s).', $this->prefix, $signo), 'debugHighlight');
exit;
}

/**
* @param bool $isLastCycle
*/
Expand Down
10 changes: 7 additions & 3 deletions src/LuceneSearchBundle/Task/Crawler/CrawlerTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,12 @@
class CrawlerTask extends AbstractTask
{
/**
* @var array
* @var string
*/
protected $prefix = 'task.crawler';

/**
* @var string
*/
protected $seed;

Expand Down Expand Up @@ -158,14 +163,13 @@ private function getInvalidLinks()
*/
public function process($previousData)
{
$this->logger->setPrefix('task.crawler');
$this->logger->setPrefix($this->prefix);

$start = microtime(true);

try {
$spider = new Spider($this->seed);
} catch (\Exception $e) {

$this->log('error: ' . $e->getMessage(), 'error');
return false;
}
Expand Down
29 changes: 14 additions & 15 deletions src/LuceneSearchBundle/Task/Parser/ParserTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,18 @@

class ParserTask extends AbstractTask
{
/**
* @var string
*/
protected $prefix = 'task.parser';

/**
* @var \Zend_Search_Lucene
*/
protected $index = null;
protected $index;

/**
* @var
* @var string
*/
protected $assetTmpDir;

Expand Down Expand Up @@ -112,16 +117,16 @@ public function isValid()
*/
public function process($crawlData)
{
$this->logger->setPrefix('task.parser');
$this->logger->setPrefix($this->prefix);

$this->addSignalListener();
$this->checkAndPrepareIndex();

foreach ($crawlData as $resource) {
if ($resource instanceof Resource) {
$this->parseResponse($resource);
} else {
$this->log('crawler resource not a instance of \VDB\Spider\Resource. Given type: ' . gettype($resource),
'notice');
$this->log('crawler resource not a instance of \VDB\Spider\Resource. Given type: ' . gettype($resource), 'notice');
}
}

Expand Down Expand Up @@ -157,8 +162,8 @@ public function parseResponse($resource)
}

/**
* @param Resource $resource
* @param $host
* @param Resource $resource
* @param string $host
*
* @return bool
*/
Expand Down Expand Up @@ -321,7 +326,7 @@ private function parseHtml($resource, $host)

/**
* @param Resource $resource
* @param $host
* @param string $host
*
* @return bool
*/
Expand Down Expand Up @@ -791,9 +796,6 @@ protected function extractImageAltText($html)
return $data;
}

/**
*
*/
protected function checkAndPrepareIndex()
{
if (!$this->index) {
Expand All @@ -817,10 +819,7 @@ protected function checkAndPrepareIndex()
}
}

/**
*
*/
public function optimizeIndex()
protected function optimizeIndex()
{
// optimize lucene index for better performance
$this->index->optimize();
Expand Down
17 changes: 15 additions & 2 deletions src/LuceneSearchBundle/Task/System/ShutDownTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,33 @@

class ShutDownTask extends AbstractTask
{
/**
* @var string
*/
protected $prefix = 'task.shutdown';

/**
* @return bool
*/
public function isValid()
{
return true;
}

/**
* @param mixed $crawlData
*
* @return bool|mixed
*/
public function process($crawlData)
{
$this->logger->setPrefix('task.shutdown');
$this->logger->setPrefix($this->prefix);

if ($this->isLastCycle() === false) {
return false;
}

$this->logger->log('Stopping Crawling...', 'debug', false, false);
$this->logger->log('Stopping crawling...', 'debug', false, false);

$this->handlerDispatcher->getStoreHandler()->resetPersistenceStore();
$this->handlerDispatcher->getStoreHandler()->resetUriFilterPersistenceStore();
Expand Down
10 changes: 9 additions & 1 deletion src/LuceneSearchBundle/Task/System/StartUpTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@

class StartUpTask extends AbstractTask
{
/**
* @var string
*/
protected $prefix = 'task.startup';

/**
* @return bool
*/
public function isValid()
{
//we're in a running cycle, don't interrupt.
Expand All @@ -32,7 +40,7 @@ public function isValid()
*/
public function process($crawlData)
{
$this->logger->setPrefix('task.startup');
$this->logger->setPrefix($this->prefix);

if ($this->isFirstCycle() === false) {
return false;
Expand Down

0 comments on commit b095ebc

Please sign in to comment.