Skip to content
This repository was archived by the owner on Jan 15, 2022. It is now read-only.

Commit e8a6b55

Browse files
authored
Merge pull request #19 from spiral/2.0-new
2.0 new
2 parents 2e6fe10 + fd0f6c5 commit e8a6b55

35 files changed

+363
-310
lines changed

.travis.yml

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ script:
2727
- go test -v -race -cover ./broker/beanstalk -coverprofile=beanstalk.txt -covermode=atomic
2828
- go test -v -race -cover ./broker/sqs -coverprofile=sqs.txt -covermode=atomic
2929
- vendor/bin/phpunit --coverage-clover=coverage.xml
30+
- vendor/bin/phpcs -n --standard=PSR2 --colors src tests
3031

3132
after_success:
3233
- bash <(curl -s https://codecov.io/bash) -f jobs.txt

broker/beanstalk/sock.bean

Whitespace-only changes.

composer.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@
1515
"spiral/roadrunner": "^1.1"
1616
},
1717
"require-dev": {
18+
"phpunit/phpunit": "~7.0",
19+
"squizlabs/php_codesniffer": "^3.4",
1820
"spiral/core": "^1.0",
19-
"doctrine/inflector": "^1.3",
20-
"phpunit/phpunit": "~7.0"
21+
"doctrine/inflector": "^1.3"
2122
},
2223
"autoload": {
2324
"psr-4": {

src/AbstractJob.php

-77
This file was deleted.

src/Consumer.php

+17-18
Original file line numberDiff line numberDiff line change
@@ -16,45 +16,44 @@
1616
*/
1717
final class Consumer
1818
{
19-
/*** @var Worker */
20-
private $worker;
21-
22-
/** @var FactoryInterface */
23-
private $factory;
19+
/** @var HandlerRegistryInterface */
20+
private $registry;
2421

2522
/**
2623
* @codeCoverageIgnore
27-
*
28-
* @param Worker $worker
29-
* @param FactoryInterface $factory
24+
* @param HandlerRegistryInterface $registry
3025
*/
31-
public function __construct(Worker $worker, FactoryInterface $factory)
26+
public function __construct(HandlerRegistryInterface $registry)
3227
{
33-
$this->worker = $worker;
34-
$this->factory = $factory;
28+
$this->registry = $registry;
3529
}
3630

3731
/**
3832
* @codeCoverageIgnore
33+
* @param Worker $worker
3934
* @param callable|null $finalize
4035
*/
41-
public function serve(callable $finalize = null)
36+
public function serve(Worker $worker, callable $finalize = null)
4237
{
43-
while ($body = $this->worker->receive($context)) {
38+
while ($body = $worker->receive($context)) {
4439
try {
4540
$context = json_decode($context, true);
41+
$handler = $this->registry->getHandler($context['job']);
4642

47-
$job = $this->factory->make($context['job'], $body);
48-
$job->execute($context['id']);
43+
$handler->handle(
44+
$context['job'],
45+
$context['id'],
46+
$handler->unserialize($context['job'], $body)
47+
);
4948

50-
$this->worker->send("ok");
49+
$worker->send("ok");
5150
} catch (\Throwable $e) {
52-
$this->worker->error((string)$e);
51+
$worker->error((string)$e);
5352
} finally {
5453
if ($finalize !== null) {
5554
call_user_func($finalize, $e ?? null);
5655
}
5756
}
5857
}
5958
}
60-
}
59+
}

src/Exception/JobException.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@
1111

1212
class JobException extends \RuntimeException
1313
{
14-
}
14+
}

src/Factory/SpiralFactory.php

-57
This file was deleted.

src/FactoryInterface.php

-26
This file was deleted.

src/HandlerInterface.php

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
/**
3+
* Spiral Framework.
4+
*
5+
* @license MIT
6+
* @author Anton Titov (Wolfy-J)
7+
*/
8+
declare(strict_types=1);
9+
10+
namespace Spiral\Jobs;
11+
12+
/**
13+
* Handles incoming jobs.
14+
*/
15+
interface HandlerInterface
16+
{
17+
/**
18+
* Handle incoming job.
19+
*
20+
* @param string $jobType
21+
* @param string $jobID
22+
* @param array $payload
23+
*/
24+
public function handle(string $jobType, string $jobID, array $payload): void;
25+
26+
/**
27+
* Serialize payload.
28+
*
29+
* @param string $jobType
30+
* @param array $payload
31+
* @return string
32+
*/
33+
public function serialize(string $jobType, array $payload): string;
34+
35+
/**
36+
* Unserialize payload.
37+
*
38+
* @param string $jobType
39+
* @param string $payload
40+
* @return array
41+
*/
42+
public function unserialize(string $jobType, string $payload): array;
43+
}

src/HandlerRegistryInterface.php

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
/**
3+
* Spiral Framework.
4+
*
5+
* @license MIT
6+
* @author Anton Titov (Wolfy-J)
7+
*/
8+
declare(strict_types=1);
9+
10+
namespace Spiral\Jobs;
11+
12+
use Spiral\Jobs\Exception\JobException;
13+
14+
/**
15+
* Resolves handler for a given job type.
16+
*/
17+
interface HandlerRegistryInterface
18+
{
19+
/**
20+
* Get handler for the given job type.
21+
*
22+
* @param string $jobType
23+
* @return HandlerInterface
24+
*
25+
* @throws JobException
26+
*/
27+
public function getHandler(string $jobType): HandlerInterface;
28+
}

src/JobHandler.php

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
/**
3+
* Spiral Framework.
4+
*
5+
* @license MIT
6+
* @author Anton Titov (Wolfy-J)
7+
*/
8+
declare(strict_types=1);
9+
10+
namespace Spiral\Jobs;
11+
12+
use Spiral\Core\ResolverInterface;
13+
use Spiral\Jobs\Exception\JobException;
14+
15+
/**
16+
* Handler which can invoke itself.
17+
*/
18+
abstract class JobHandler implements HandlerInterface
19+
{
20+
// default function with method injection
21+
protected const HANDLE_FUNCTION = 'invoke';
22+
23+
/** @var ResolverInterface */
24+
protected $resolver;
25+
26+
/**
27+
* @param ResolverInterface $resolver
28+
*/
29+
public function __construct(ResolverInterface $resolver)
30+
{
31+
$this->resolver = $resolver;
32+
}
33+
34+
/**
35+
* @inheritdoc
36+
*/
37+
public function handle(string $jobType, string $jobID, array $payload): void
38+
{
39+
$method = new \ReflectionMethod($this, static::HANDLE_FUNCTION);
40+
$method->setAccessible(true);
41+
42+
try {
43+
$parameters = ['payload' => $payload, 'id' => $jobID,] + $payload;
44+
$method->invokeArgs($this, $this->resolver->resolveArguments($method, $parameters));
45+
} catch (\Throwable $e) {
46+
throw new JobException(sprintf("[%s] %s", get_class($this), $e->getMessage()), $e->getCode(), $e);
47+
}
48+
}
49+
50+
/**
51+
* @inheritdoc
52+
*/
53+
public function serialize(string $jobType, array $payload): string
54+
{
55+
return json_encode($payload);
56+
}
57+
58+
/**
59+
* @inheritdoc
60+
*/
61+
public function unserialize(string $jobType, string $payload): array
62+
{
63+
return json_decode($payload, true);
64+
}
65+
}

0 commit comments

Comments
 (0)