Skip to content

Commit

Permalink
Merge pull request #29 from ytake/feature-some-fixed
Browse files Browse the repository at this point in the history
added rollback
  • Loading branch information
ytake authored Aug 4, 2016
2 parents 0f591f7 + 83876ca commit 4ada12a
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 21 deletions.
2 changes: 2 additions & 0 deletions src/Annotation/Async.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,6 @@
*/
final class Async extends Annotation
{
/** @var int $process */
public $process = 1;
}
33 changes: 26 additions & 7 deletions src/Interceptor/AsyncInterceptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

use Ray\Aop\MethodInvocation;
use Ray\Aop\MethodInterceptor;
use Ytake\LaravelAspect\Annotation\Async;
use Ytake\LaravelAspect\Annotation\AnnotationReaderTrait;

/**
* Class AsyncInterceptor
Expand All @@ -27,6 +29,8 @@
*/
class AsyncInterceptor implements MethodInterceptor
{
use AnnotationReaderTrait;

/**
* @param MethodInvocation $invocation
*
Expand All @@ -35,13 +39,28 @@ class AsyncInterceptor implements MethodInterceptor
*/
public function invoke(MethodInvocation $invocation)
{
$pid = pcntl_fork();
if ($pid === -1) {
throw new \RuntimeException('pcntl_fork() returned -1');
} elseif ($pid) {
return null;
} else {
$invocation->proceed();
/** @var Async $annotation */
$annotation = $this->reader
->getMethodAnnotation($invocation->getMethod(), $this->annotation);
$stack = [];
for ($i = 1; $i <= $annotation->process; $i++) {
$pid = pcntl_fork();
if ($pid === -1) {
throw new \RuntimeException('pcntl_fork() returned -1');
} elseif ($pid) {
$stack[$pid] = true;
if (count($stack) >= $annotation->process) {
unset($stack[pcntl_waitpid(-1, $status, WUNTRACED)]);
}

return null;
} else {
$invocation->proceed();
exit;
}
}
while (count($stack) > 0) {
unset($stack[pcntl_waitpid(-1, $status, WUNTRACED)]);
}
}
}
1 change: 1 addition & 0 deletions src/Interceptor/TransactionalInterceptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public function invoke(MethodInvocation $invocation)
$database->rollBack();
throw $exception;
}
$database->rollBack();
throw $exception;
}
}
Expand Down
14 changes: 0 additions & 14 deletions src/PointCut/AsyncPointCut.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
*/
namespace Ytake\LaravelAspect\PointCut;

use Ray\Aop\Matcher;
use Ray\Aop\Pointcut;
use Illuminate\Contracts\Container\Container;
use Ytake\LaravelAspect\Annotation\Async;
use Ytake\LaravelAspect\Interceptor\AsyncInterceptor;
Expand All @@ -42,16 +40,4 @@ public function configure(Container $app)

return $this->withAnnotatedAnyInterceptor($app);
}

/**
* {@inheritdoc}
*/
protected function withAnnotatedAnyInterceptor(Container $app)
{
return new Pointcut(
(new Matcher)->any(),
(new Matcher)->annotatedWith($this->annotation),
[$this->interceptor]
);
}
}

0 comments on commit 4ada12a

Please sign in to comment.