Skip to content

Commit

Permalink
#110 fix exception handling
Browse files Browse the repository at this point in the history
  • Loading branch information
tbela99 committed Oct 23, 2022
1 parent a9d1f88 commit 387375b
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 36 deletions.
22 changes: 16 additions & 6 deletions src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use TBela\CSS\Parser\SyntaxError;
use TBela\CSS\Process\Exceptions\UnhandledException;
use TBela\CSS\Process\Pool as ProcessPool;
use TBela\CSS\Process\ProcessInterface;

/**
* Css Parser
Expand Down Expand Up @@ -100,6 +101,19 @@ public function __construct(string $css = '', array $options = [], string $media
}
}

/**
* @param mixed $file
* @param mixed $slice
* @return \Closure
*/
public function handleException(mixed $file, mixed $slice): \Closure
{
return function (\Throwable $e, ProcessInterface $process) use ($file, $slice) {

throw new \RuntimeException(sprintf('%s - error parsing %s:%s:%s', preg_replace('#.*\\\\([a-zA-Z][a-zA-Z0-9]*)$#', '$1', $process::class), $file, $slice[1]->line, $slice[1]->column), $e->getCode(), $e);
};
}

/**
* @param string $event parse event name in ['enter', 'exit']
* @param callable $callable
Expand Down Expand Up @@ -241,10 +255,7 @@ public function parallelize(string $content, object $root, string $file): void
then(function (array $result, int $index) use (&$data) {

$data[$index] = $result;
})->catch(function (\Throwable $e) use($file, $slice) {

throw new \RuntimeException(sprintf('error parsing %s:%s:%s', $file, $slice[1]->line, $slice[1]->column), $e->getCode(), $e);
});
})->catch($this->handleException($file, $slice));
}

$processPool->wait();
Expand Down Expand Up @@ -1280,7 +1291,6 @@ public function __toString(): string
}

$size = 0;

$i = count($this->queue);

while ($i--) {
Expand Down Expand Up @@ -1339,7 +1349,7 @@ public function __toString(): string
then(function (array $result, int $index) use (&$css) {

$css[$index] = $result;
});
})->catch($this->handleException($file, $slice));
}
}

Expand Down
11 changes: 11 additions & 0 deletions src/Process/AbstractProcess.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ public function getTimeout(): ?float
return $this->timeout;
}

abstract protected function updateStatus(): void;

/**
* @return void
* @throws TimeoutException
Expand Down Expand Up @@ -177,6 +179,15 @@ public function check(int $waitTimeout): Generator

if ($data !== true && $data !== "done") {

// $this->updateStatus();

// if (!$this->running) {
//
// var_dump(array_filter(get_object_vars($this), 'is_scalar'));
//
// break;
// }

$this->checkTimeout();

yield $data;
Expand Down
13 changes: 12 additions & 1 deletion src/Process/MultiProcessing/Process.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ public function __construct(Closure $closure)
$serialized = new SerializableClosure($closure);

$script = 'require "' . $autoload . '";';

$vars = $serialized->getReflector()->getUseVariables();

foreach ($vars as $key => $var) {
Expand Down Expand Up @@ -182,4 +181,16 @@ public function cleanup(): void
$this->pipes = [];
}
}

protected function updateStatus(): void
{

if ($this->process) {

$this->status = proc_get_status($this->process);

$this->running = $this->status['running'];
$this->stopped = $this->status['stopped'];
}
}
}
62 changes: 33 additions & 29 deletions src/Process/Pool.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class Pool implements PoolInterface
protected ?int $startTime = null;

protected int $count = 0;
protected int $concurrency = 25;
protected int $concurrency = 20;

/**
* @var int time in nanoseconds
Expand Down Expand Up @@ -189,14 +189,14 @@ protected function check($collect = true): bool
break;
}

$data = $this->storage[$thread]->data;
$data = $this->storage[$thread];

if ($collect && $thread->isTerminated()) {
try {

$this->collect($thread);
} else if ($collect && $thread->isRunning()) {
if ($collect && $thread->isTerminated()) {

try {
$this->collect($thread);
} else if ($collect && $thread->isRunning()) {

foreach ($thread->check(1) as $status) {

Expand All @@ -211,21 +211,22 @@ protected function check($collect = true): bool
$running = max(0, $running - 1);
}
}
} catch (Throwable $e) {

// if ($e instanceof TimeoutException) {

$this->storage->detach($thread);
// }
} else if (!$thread->isStarted()) {

$this->handleException($e, $data);
$thread->start();
$this->emit('start', $data->data->index, $thread);
$running++;
}
} catch (Throwable $e) {

} else if (!$thread->isStarted()) {
// if ($e instanceof TimeoutException) {

$this->storage->detach($thread);
// }

$thread->start();
$this->emit('start', $data->index, $thread);
$running++;
// var_dump($data);
$this->handleException($e, $data, $thread);
}
}

Expand Down Expand Up @@ -358,37 +359,40 @@ public function cancel(): static
/**
* @param Throwable $e
* @param $data
* @param ProcessInterface $thread
* @return void
* @throws UnhandledException
* @throws ReflectionException
*/
protected function handleException(Throwable $e, $data): void
protected function handleException(Throwable $e, $data, ProcessInterface $thread): void
{
$class = new ReflectionClass($e::class);

while ($class) {
$handler = null;
$original = new ReflectionClass($e);

foreach ($data->error as $name => $h) {

if (isset($data->error[$class->getName()])) {
if ($original->isSubclassOf($name)) {

$handler = $h;
break;
}

$class = $class->getParentClass();
}
foreach ($original->getInterfaces() as $interface) {

if ($class === false) {
if ($interface->isSubclassOf($name)) {

$class = null;
$handler = $h;
break 2;
}
}
}

$handler = $data->error[$class?->getName()] ?? $data->error['generic'] ?? null;

if (is_callable($handler)) {

call_user_func($handler, $e);
call_user_func($handler, $e, $thread);
} else {

throw new UnhandledException(sprintf("unhandled exception in task #%d", $data->index), $e->getCode(), $e);
throw new UnhandledException(sprintf("unhandled exception in task #%d", $data->data->index), $e->getCode(), $e);
}
}
}
4 changes: 4 additions & 0 deletions src/Process/Thread/PCNTL/Thread.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,8 @@ public function cleanup(): void
pcntl_waitpid($this->pid, $this->exitCode);
}
}

protected function updateStatus(): void
{
}
}

0 comments on commit 387375b

Please sign in to comment.