-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBeanstalkd.php
216 lines (195 loc) · 5.74 KB
/
Beanstalkd.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
<?php
declare (strict_types=1);
namespace core;
/**
Beanstalk is a simple, fast work queue.
This class is a small implementation of the protocol
to interact with this small daemon.
@see https://beanstalkd.github.io/
Installation should be as easy as apt-get install beanstalkd
*/
class Beanstalkd {
public static function conn() {
$errno = null;
$errstr = null;
$conn = fsockopen("127.0.0.1", 11300, $errno, $errstr, 2);
if ($conn === false) {
echo "CRITICAL - stream_socket_client fail: $errno $errstr\n";
exit(2);
}
return $conn;
}
public static function write($conn, string $cmd, int $timeout = 3): void {
self::assert_resource($conn);
if (stream_set_timeout($conn, $timeout) === false) {
echo "UNKNOWN - stream_set_timeout fail\n";
exit(3);
}
if (VERBOSE) echo ">> $cmd\n";
if (fwrite($conn, $cmd."\r\n", strlen($cmd."\r\n")) === false) {
echo "UNKNOWN - fwrite($cmd) fail\n";
exit(3);
}
}
public static function read($conn): ?string {
self::assert_resource($conn);
$res = stream_get_line($conn, 16384, "\r\n");
if (VERBOSE) echo "<< $res\n";
return $res ?: null;
}
public static function assert_resource($resource)
{
if (false === is_resource($resource)) {
throw new InvalidArgumentException(
sprintf(
'Argument must be a valid resource type. %s given.',
gettype($resource)
)
);
}
}
public static function res_kvp($conn, string $cmd): array {
self::write($conn, $cmd);
$bytes = 0;
{
$res = self::read($conn);
if ($res === false || substr($res, 0, 3) !== "OK ") {
echo "CRITICAL - reply($cmd) wrong: $res\n";
exit(2);
}
$bytes = substr($res, 3);
}
$lines = [];
{
$res = trim(fread($conn, $bytes+2));
if ($res === false) {
echo "CRITICAL - reply($cmd) wrong: $res\n";
exit(2);
}
if (VERBOSE) echo "<< $res\n";
foreach (explode("\n", $res) as $line) {
if (strlen($line) === 0) {
// Empty line
continue;
}
if ($line === "---") {
// Skip separation-thingy
continue;
}
$sep = strpos($line, ":");
if ($sep === false) {
echo "CRITICAL - expected separation char on line: $line\n";
exit(2);
}
$lines[ substr($line, 0, $sep) ] = substr($line, $sep+2);
}
}
return $lines;
}
public static function res_list($conn, string $cmd): array {
self::write($conn, $cmd);
$bytes = 0;
{
$res = self::read($conn);
if ($res === false || substr($res, 0, 3) !== "OK ") {
echo "CRITICAL - reply($cmd) wrong: $res\n";
exit(2);
}
$bytes = substr($res, 3);
}
$lines = [];
{
$res = trim(fread($conn, $bytes+2));
if ($res === false) {
echo "CRITICAL - reply($cmd) wrong: $res\n";
exit(2);
}
foreach (explode("\n", $res) as $line) {
if (VERBOSE) echo "<< $line\n";
if (strlen($line) === 0) {
// Empty line
continue;
}
if ($line === "---") {
// Skip separation-thingy
continue;
}
$sep = strpos($line, "- ");
if ($sep === false) {
echo "CRITICAL - expected separation char on line: $line\n";
exit(2);
}
$lines[] = substr($line, $sep+2);
}
}
return $lines;
}
public static function close($conn): void {
self::write($conn, "quit");
fclose($conn);
}
public static function tube($conn, string $tube): void {
self::write($conn, "use $tube");
$res = self::read($conn);
if ($res === false || trim($res) !== "USING $tube") {
echo "tube($tube) wrong: $res\n";
exit(2);
}
}
public static function kick($conn, int $n): void {
self::write($conn, "kick $n");
$res = self::read($conn);
if ($res === false || substr(trim($res), 0, 6) !== "KICKED") {
echo "CRITICAL - reply(KICK 50) wrong: $res\n";
exit(2);
}
}
public static function delete($conn, int $id): void {
self::write($conn, "delete $id");
$res = self::read($conn);
if ($res === false || substr(trim($res), 0, 7) !== "DELETED") {
echo "CRITICAL - reply(DELETE) wrong: $res\n";
exit(2);
}
}
public static function put($conn, string $data, int $delay = 5, int $timeToRun = 120, int $prio = 10): int {
self::write($conn, sprintf("put %d %d %d %d", $prio, $delay, $timeToRun, strlen($data)));
self::write($conn, $data);
$res = self::read($conn);
if ($res === false || substr(trim($res), 0, 8) !== "INSERTED") {
echo "CRITICAL - INSERTED wrong: $res\n";
exit(2);
}
return intval(explode(" ", $res)[1]);
}
public static function watch($conn, string $tube): void {
self::write($conn, "watch $tube");
$res = self::read($conn);
if ($res === false || substr(trim($res), 0, 8) !== "WATCHING") {
echo "CRITICAL - reply(WATCH) wrong: $res\n";
exit(2);
}
}
public static function reserve($conn, int $timeout = 60): ?array {
self::write($conn, "reserve-with-timeout $timeout", $timeout+1);
$res = self::read($conn);
/*$info = stream_get_meta_data($conn);
if ($info['timed_out']) {
var_dump($info);
user_error("stream timed out");
}*/
if ($res === "TIMED_OUT") return null;
/* RESERVED <id> <bytes>\r\n<data>\r\n */
$cmd = explode(" ", $res, 3);
if ($cmd[0] !== "RESERVED") {
echo "CMD invalid=$res\n";
exit(2);
}
$lines = fread($conn, intval($cmd[2])+2);
$json = json_decode($lines, true);
return [
"id" => intval($cmd[1]),
"data" => $json ?? $lines,
];
}
}