1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NokitaKaze\Queue; |
4
|
|
|
|
5
|
|
|
abstract class AbstractQueueTransport implements QueueTransport { |
6
|
|
|
/** |
7
|
|
|
* @var iMessage[]|object[] |
8
|
|
|
*/ |
9
|
|
|
protected $_pushed_for_save = []; |
10
|
|
|
|
11
|
|
|
protected $_same_time_consumer_and_producer = 0; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @var callable|null |
15
|
|
|
*/ |
16
|
|
|
protected $_callback_closure = null; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var integer[] В keys список индексов полученных сообщений |
20
|
|
|
*/ |
21
|
|
|
protected $_consumed_keys = []; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Удалить сообщение и сразу же записать это в БД |
25
|
|
|
* |
26
|
|
|
* @param iMessage|object $message |
27
|
|
|
* |
28
|
|
|
* @return boolean |
29
|
|
|
*/ |
30
|
300 |
|
function delete_message($message) { |
|
|
|
|
31
|
300 |
|
return !empty($this->delete_messages([$message])); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Загоняем сообщение в очередь и явно сохраняем |
36
|
|
|
* |
37
|
|
|
* @param mixed $data |
38
|
|
|
* @param string|null $name Название сообщения. Если null, то можно дублировать |
39
|
|
|
* @param integer $sort |
40
|
|
|
* |
41
|
|
|
* @throws QueueException |
42
|
|
|
*/ |
43
|
194 |
|
function produce_message($data, $name = null, $sort = 5) { |
|
|
|
|
44
|
194 |
|
$this->push(Queue::build_message($data, $name, $sort)); |
45
|
194 |
|
$this->save(); |
46
|
194 |
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Загоняем сообщение в очередь необходимых для сохранения сообщений |
50
|
|
|
* |
51
|
|
|
* @param iMessage[]|iMessage|object[]|object $stream |
52
|
|
|
* |
53
|
|
|
* @throws QueueException |
54
|
|
|
*/ |
55
|
3699 |
|
function push($stream) { |
|
|
|
|
56
|
3699 |
|
$this->set_same_time_flag(1); |
57
|
3699 |
|
if (is_array($stream)) { |
58
|
1428 |
|
foreach ($stream as &$message) { |
59
|
1428 |
|
$this->_pushed_for_save[] = Queue::sanify_event_object($message); |
60
|
476 |
|
} |
61
|
476 |
|
} else { |
62
|
3384 |
|
$this->_pushed_for_save[] = Queue::sanify_event_object($stream); |
63
|
|
|
} |
64
|
3699 |
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param integer $flag |
68
|
|
|
* |
69
|
|
|
* @throws QueueException |
70
|
|
|
*/ |
71
|
3708 |
|
protected function set_same_time_flag($flag) { |
72
|
3708 |
|
$this->_same_time_consumer_and_producer |= $flag; |
73
|
3708 |
|
if ($this->_same_time_consumer_and_producer === 3) { |
74
|
9 |
|
throw new QueueException('Consumer and producer at the same time', 18); |
75
|
|
|
} |
76
|
3708 |
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param callable $closure |
80
|
|
|
*/ |
81
|
3 |
|
function set_callback_closure($closure) { |
|
|
|
|
82
|
3 |
|
$this->_callback_closure = $closure; |
83
|
3 |
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param double|integer $wait_time |
87
|
|
|
* |
88
|
|
|
* @throws QueueException |
89
|
|
|
*/ |
90
|
6 |
|
function listen($wait_time = -1) { |
|
|
|
|
91
|
6 |
|
$start = microtime(true); |
92
|
6 |
|
if (is_null($this->_callback_closure)) { |
93
|
3 |
|
throw new QueueException('Event listener has not been set', 3); |
94
|
|
|
} |
95
|
3 |
|
$closure = $this->_callback_closure; |
96
|
|
|
do { |
97
|
3 |
|
$message = $this->consume_next_message($wait_time); |
98
|
3 |
|
if (!is_null($message)) { |
99
|
3 |
|
call_user_func($closure, $message); |
100
|
1 |
|
} else { |
101
|
3 |
|
usleep(10000); |
102
|
|
|
} |
103
|
3 |
|
} while (($wait_time === -1) or ($start + $wait_time >= microtime(true))); |
104
|
3 |
|
} |
105
|
|
|
|
106
|
3423 |
|
function __clone() { |
|
|
|
|
107
|
3423 |
|
foreach ($this->_pushed_for_save as &$message) { |
108
|
9 |
|
$message = clone $message; |
109
|
1141 |
|
} |
110
|
3423 |
|
} |
111
|
|
|
|
112
|
9 |
|
function clear_consumed_keys() { |
|
|
|
|
113
|
9 |
|
$this->_consumed_keys = []; |
114
|
9 |
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @return boolean |
118
|
|
|
*/ |
119
|
209 |
|
function is_producer() { |
|
|
|
|
120
|
209 |
|
return (($this->_same_time_consumer_and_producer & 1) == 1); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @return boolean |
125
|
|
|
*/ |
126
|
360 |
|
function is_consumer() { |
|
|
|
|
127
|
360 |
|
return (($this->_same_time_consumer_and_producer & 2) == 2); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param iMessage|object $message |
132
|
|
|
* |
133
|
|
|
* @return string |
134
|
|
|
*/ |
135
|
3816 |
|
static function get_real_key_for_message($message) { |
|
|
|
|
136
|
3816 |
|
return Queue::get_real_key_for_message($message); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @param iMessage[] $messages |
141
|
|
|
* @param iMessage $message |
142
|
|
|
* @param string $message_key |
143
|
|
|
* |
144
|
|
|
* @return boolean |
145
|
|
|
*/ |
146
|
349 |
|
protected function change_message_in_array(array &$messages, $message, $message_key) { |
147
|
349 |
|
$exists = false; |
148
|
349 |
|
$message->is_read = true; |
|
|
|
|
149
|
349 |
|
foreach ($messages as $inner_id => &$inner_message) { |
150
|
349 |
|
$inner_message_key = self::get_real_key_for_message($inner_message); |
151
|
349 |
|
if ($inner_message_key == $message_key) { |
152
|
349 |
|
$message->time_last_update = microtime(true); |
|
|
|
|
153
|
349 |
|
$messages[$inner_id] = $message; |
154
|
349 |
|
$exists = true; |
155
|
106 |
|
} else { |
156
|
349 |
|
$inner_message->is_read = true; |
|
|
|
|
157
|
|
|
} |
158
|
106 |
|
} |
159
|
|
|
|
160
|
349 |
|
return $exists; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @param QueueTransport $queue |
165
|
|
|
* |
166
|
|
|
* @return boolean |
167
|
|
|
*/ |
168
|
1263 |
|
function is_equal_to($queue) { |
|
|
|
|
169
|
1263 |
|
return (get_class($this) == get_class($queue)); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @return string |
174
|
|
|
*/ |
175
|
39 |
|
static function generate_rnd_postfix() { |
|
|
|
|
176
|
39 |
|
return Queue::generate_rnd_postfix(); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
// @todo Удаление конкретных ключей из consumed. Причем туда передаётся кложур, в который передаётся название ключа |
180
|
|
|
// @todo Удаление конкретных ключей из индекса и очереди, с указанием max_create_timestamp, |
181
|
|
|
// чтобы не хранить в очереди те же сообщения, пришедшие ещё раз |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
?> |
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.