|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace FondBot\Queue\Adapters; |
|
6
|
|
|
|
|
7
|
|
|
use FondBot\Queue\Job; |
|
8
|
|
|
use FondBot\Queue\Adapter; |
|
9
|
|
|
use Pheanstalk\Pheanstalk; |
|
10
|
|
|
use FondBot\Drivers\Driver; |
|
11
|
|
|
use FondBot\Drivers\Command; |
|
12
|
|
|
use FondBot\Channels\Channel; |
|
13
|
|
|
|
|
14
|
|
|
class BeanstalkdAdapter extends Adapter |
|
15
|
|
|
{ |
|
16
|
|
|
/** @var Pheanstalk */ |
|
17
|
|
|
private $connection; |
|
18
|
|
|
|
|
19
|
|
|
private $host; |
|
20
|
|
|
private $port; |
|
21
|
|
|
private $queue; |
|
22
|
|
|
private $timeout; |
|
23
|
|
|
private $persistent; |
|
24
|
|
|
|
|
25
|
|
|
public function __construct( |
|
26
|
|
|
string $host, |
|
27
|
|
|
int $port = 11300, |
|
28
|
|
|
string $queue = 'default', |
|
29
|
|
|
int $timeout = null, |
|
30
|
|
|
bool $persistent = false |
|
31
|
|
|
) { |
|
32
|
|
|
$this->host = $host; |
|
33
|
|
|
$this->port = $port; |
|
34
|
|
|
$this->queue = $queue; |
|
35
|
|
|
$this->timeout = $timeout; |
|
36
|
|
|
$this->persistent = $persistent; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Establish connection to the queue. |
|
41
|
|
|
*/ |
|
42
|
|
|
public function connect(): void |
|
43
|
|
|
{ |
|
44
|
|
|
$this->connection = new Pheanstalk($this->host, $this->port, $this->timeout, $this->persistent); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Push command onto the queue. |
|
49
|
|
|
* |
|
50
|
|
|
* @param Channel $channel |
|
51
|
|
|
* @param Driver $driver |
|
52
|
|
|
* @param Command $command |
|
53
|
|
|
*/ |
|
54
|
|
|
public function push(Channel $channel, Driver $driver, Command $command): void |
|
55
|
|
|
{ |
|
56
|
|
|
$job = new Job($channel, $driver, $command); |
|
57
|
|
|
$this->connection->putInTube($this->queue, $this->serialize($job)); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Push command onto the queue with a delay. |
|
62
|
|
|
* |
|
63
|
|
|
* @param Channel $channel |
|
64
|
|
|
* @param Driver $driver |
|
65
|
|
|
* @param Command $command |
|
66
|
|
|
* @param int $delay |
|
67
|
|
|
* |
|
68
|
|
|
* @return mixed|void |
|
69
|
|
|
*/ |
|
70
|
|
|
public function later(Channel $channel, Driver $driver, Command $command, int $delay): void |
|
71
|
|
|
{ |
|
72
|
|
|
$job = new Job($channel, $driver, $command); |
|
73
|
|
|
$this->connection->putInTube($this->queue, $this->serialize($job), Pheanstalk::DEFAULT_PRIORITY, $delay); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|