/
index.php
125 lines (98 loc) · 3.47 KB
/
index.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
<?php
require 'vendor/autoload.php';
require 'config.php';
use Respect\Validation\Validator as v;
use Respect\Validation\Exceptions\NestedValidationExceptionInterface;
$app = new \Slim\App([
'settings' => [
'displayErrorDetails' => true
]
]);
$container = $app->getContainer();
$container['view'] = function($c) {
$view = new \Slim\Views\Twig('templates/');
$basePath = rtrim(str_ireplace('index.php', '', $c['request']->getUri()->getBasePath()), '/');
$view->addExtension(new \Slim\Views\TwigExtension($c['router'], $basePath));
return $view;
};
$container['pheanstalk'] = new \Pheanstalk\Pheanstalk($config['beanstalk_server']);
function jobToDict($app, $tube, $kind) {
try {
if ($kind == 'ready') {
$job = $app->pheanstalk->peekReady($tube);
} elseif ($kind == 'delayed') {
$job = $app->pheanstalk->peekDelayed($tube);
} elseif ($kind == 'buried') {
$job = $app->pheanstalk->peekBuried($tube);
} else {
return null;
}
} catch (\Pheanstalk\Exception\ServerException $e) {
return null;
}
$statsJob = $app->pheanstalk->statsJob($job);
return ['data' => $job->getData(), 'stats' => $statsJob];
}
function writeServerState($app, $res) {
$service = [
'isListening' => $app->pheanstalk->getConnection()->isServiceListening(),
'stats' => $app->pheanstalk->stats()->getArrayCopy()
];
$tubes = [];
foreach($app->pheanstalk->listTubes() as $tube) {
$tubes[$tube]['stats'] = $app->pheanstalk->statsTube($tube);
$tubes[$tube]['ready'] = jobToDict($app, $tube, 'ready');
$tubes[$tube]['delayed'] = jobToDict($app, $tube, 'delayed');
$tubes[$tube]['buried'] = jobToDict($app, $tube, 'buried');
}
$r = $res->withHeader('Content-Type', 'application/json');
$r->write(json_encode([
'service' => $service,
'tubes' => $tubes
]));
return $r;
}
$app->get('/', function($req, $res) {
return $this->view->render($res, 'index.html');
});
$app->get('/api/info', function($req, $res) {
return writeServerState($this, $res);
});
$app->post('/cmd/delete', function($req, $res) {
$job_id = $req->getParam('job_id');
try {
v::numeric()->setName('job_id')->check($job_id);
} catch (ValidationExceptionInterface $e) {
return $res->withStatus(400)->write($e->getMainMessage());
}
try {
$job = new \Pheanstalk\Job($job_id, []);
$this->pheanstalk->delete($job);
} catch (\Pheanstalk\Exception\ServerException $e) {
return $res->withStatus(400)->write($e->getMessage());
}
return writeServerState($this, $res);
});
$app->post('/cmd/kick', function($req, $res) {
$job_id = $req->getParam('job_id');
try {
v::numeric()->setName('job_id')->check($job_id);
} catch (ValidationExceptionInterface $e) {
return $res->withStatus(400)->write($e->getMainMessage());
}
try {
$job = new \Pheanstalk\Job($job_id, []);
$this->pheanstalk->kickJob($job);
} catch (\Pheanstalk\Exception\ServerException $e) {
return $res->withStatus(400)->write($e->getMessage());
}
return writeServerState($this, $res);
});
$app->post('/cmd/pause', function($req, $res) {
$tube = $req->getParam('tube', 'default');
$duration = intval($req->getParam('duration', 60));
$this->pheanstalk->pauseTube($tube, $duration);
return writeServerState($this, $res);
});
$app->run();
?>