|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Aimeos\MW\MQueue\Queue; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
class Beanstalk implements Iface |
|
7
|
|
|
{ |
|
8
|
|
|
private $client; |
|
9
|
|
|
private $queue; |
|
10
|
|
|
private $timeout; |
|
11
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Initializes the queue object |
|
15
|
|
|
* |
|
16
|
|
|
* @param \Pheanstalk\PheanstalkInterface $client Client object |
|
17
|
|
|
* @param string $queue Message queue name |
|
18
|
|
|
* @throws \Aimeos\MW\MQueue\Exception |
|
19
|
|
|
*/ |
|
20
|
|
|
public function __construct( \Pheanstalk\PheanstalkInterface $client, $queue, $timeout = null ) |
|
21
|
|
|
{ |
|
22
|
|
|
try { |
|
23
|
|
|
$client->useTube( $queue )->watch( $queue ); |
|
24
|
|
|
} catch( \Exception $e ) { |
|
25
|
|
|
throw new \Aimeos\MW\MQueue\Exception( $e->getMessage() ); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
$this->client = $client; |
|
29
|
|
|
$this->queue = $queue; |
|
30
|
|
|
$this->timeout = $timeout; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Adds a new message to the message queue |
|
36
|
|
|
* |
|
37
|
|
|
* @param string $msg Message, e.g. JSON encoded data |
|
38
|
|
|
*/ |
|
39
|
|
|
public function add( $msg ) |
|
40
|
|
|
{ |
|
41
|
|
|
try { |
|
42
|
|
|
$this->client->put( $msg ); |
|
43
|
|
|
} catch( \Exception $e ) { |
|
44
|
|
|
throw new \Aimeos\MW\MQueue\Exception( $e->getMessage() ); |
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Removes the message from the queue |
|
51
|
|
|
* |
|
52
|
|
|
* @param \Aimeos\MW\MQueue\Message\Iface $msg Message object |
|
53
|
|
|
*/ |
|
54
|
|
|
public function del( \Aimeos\MW\MQueue\Message\Iface $msg ) |
|
55
|
|
|
{ |
|
56
|
|
|
try { |
|
57
|
|
|
$this->client->delete( $msg->getObject() ); |
|
58
|
|
|
} catch( \Exception $e ) { |
|
59
|
|
|
throw new \Aimeos\MW\MQueue\Exception( $e->getMessage() ); |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Returns the next message from the queue |
|
66
|
|
|
* |
|
67
|
|
|
* @return \Aimeos\MW\MQueue\Message\Iface|null Message object or null if none is available |
|
68
|
|
|
*/ |
|
69
|
|
|
public function get() |
|
70
|
|
|
{ |
|
71
|
|
|
if( ( $job = $this->client->reserve( $this->timeout ) ) !== false ) { |
|
72
|
|
|
return new \Aimeos\MW\MQueue\Message\Beanstalk( $job ); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|