Conditions | 6 |
Paths | 5 |
Total Lines | 35 |
Code Lines | 20 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | <?php |
||
17 | public function dispatch($job, $params = []) |
||
18 | { |
||
19 | $this->etcd->setRoot('jobs'); |
||
20 | |||
21 | $service = $this->etcd->get($job); |
||
22 | if(!$service) { |
||
23 | throw new Exception("No service for job $job"); |
||
24 | } |
||
25 | |||
26 | $host = getenv(strtoupper($service).'_SERVICE_HOST') ?: $service; |
||
27 | |||
28 | $content = http_build_query([ |
||
29 | 'rpc' => json_encode([ |
||
30 | 'job' => $job, |
||
31 | 'params' => $params, |
||
32 | ]) |
||
33 | ]); |
||
34 | |||
35 | $context = stream_context_create([ |
||
36 | 'http' => [ |
||
37 | 'method' => 'POST', |
||
38 | 'header' => 'Content-Type: application/x-www-form-urlencoded', |
||
39 | 'content' => $content, |
||
40 | ], |
||
41 | ]); |
||
42 | |||
43 | $contents = file_get_contents("http://$host/api", false, $context); |
||
44 | |||
45 | $result = json_decode($contents); |
||
46 | if(!$result || !$result->success) { |
||
47 | throw new Exception($result->message ?: $contents); |
||
48 | } |
||
49 | |||
50 | return $result->data; |
||
51 | } |
||
52 | } |
||
53 |