-
Notifications
You must be signed in to change notification settings - Fork 1
/
EnqueueRemoteTransportProcessor.php
73 lines (64 loc) · 1.88 KB
/
EnqueueRemoteTransportProcessor.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?php
namespace Quartz\Bridge\Enqueue;
use Enqueue\Client\CommandSubscriberInterface;
use Enqueue\Consumption\QueueSubscriberInterface;
use Enqueue\Consumption\Result;
use Enqueue\Util\JSON;
use Interop\Queue\PsrContext;
use Interop\Queue\PsrMessage;
use Interop\Queue\PsrProcessor;
use Quartz\Bridge\Scheduler\RpcProtocol;
use Quartz\Core\Scheduler;
class EnqueueRemoteTransportProcessor implements PsrProcessor, CommandSubscriberInterface, QueueSubscriberInterface
{
/**
* @var Scheduler
*/
private $scheduler;
/**
* @var RpcProtocol
*/
private $rpcProtocol;
/**
* @param Scheduler $scheduler
* @param RpcProtocol $rpcProtocol
*/
public function __construct(Scheduler $scheduler, RpcProtocol $rpcProtocol)
{
$this->scheduler = $scheduler;
$this->rpcProtocol = $rpcProtocol;
}
/**
* {@inheritdoc}
*/
public function process(PsrMessage $message, PsrContext $context)
{
try {
$request = $this->rpcProtocol->decodeRequest(JSON::decode($message->getBody()));
$result = call_user_func_array([$this->scheduler, $request['method']], $request['args']);
$result = $this->rpcProtocol->encodeValue($result);
} catch (\Exception $e) {
$result = $this->rpcProtocol->encodeValue($e);
}
return Result::reply($context->createMessage(JSON::encode($result)));
}
/**
* {@inheritdoc}
*/
public static function getSubscribedCommand()
{
return [
'processorName' => EnqueueRemoteTransport::COMMAND,
'queueName' => EnqueueRemoteTransport::COMMAND,
'queueNameHardcoded' => true,
'exclusive' => true,
];
}
/**
* {@inheritdoc}
*/
public static function getSubscribedQueues()
{
return [EnqueueRemoteTransport::COMMAND];
}
}