Registry::servicing()   A
last analyzed

Complexity

Conditions 4
Paths 1

Size

Total Lines 52
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 28
dl 0
loc 52
rs 9.472
c 0
b 0
f 0
cc 4
nc 1
nop 4

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Service register
4
 * User: moyo
5
 * Date: 24/08/2017
6
 * Time: 3:55 PM
7
 */
8
9
namespace Carno\Consul;
10
11
use Carno\Consul\APIs\AgentServiceRegister;
12
use Carno\Consul\Chips\AgentRequired;
13
use Carno\Consul\Contracts\Defaults;
14
use Carno\Consul\Types\Agent;
15
use Carno\Consul\Types\Result;
16
use Carno\Consul\Types\Service;
17
use function Carno\Coroutine\async;
18
use function Carno\Coroutine\msleep;
19
use Carno\Net\Address;
20
use Carno\Net\Endpoint;
21
use Throwable;
22
23
class Registry
24
{
25
    use AgentRequired;
26
27
    /**
28
     * @param Address $advertise
29
     * @param string $service
30
     * @param array $tags
31
     * @param int $heartbeat
32
     * @return Service
33
     */
34
    public function servicing(
35
        Address $advertise,
36
        string $service,
37
        array $tags = [],
38
        int $heartbeat = Defaults::HEARTBEAT
39
    ) : Service {
40
        $serviced = (new Service($service))
41
            ->setEndpoints(
42
                (new Endpoint($advertise))
43
                    ->relatedService($service)
44
                    ->setTags(...$tags)
45
                    ->resetID()
46
            )->setKeepalive($heartbeat)
47
        ;
48
49
        async(static function (Agent $agent, Service $service) {
50
            for (;;) {
51
                /**
52
                 * @var Result $registered
53
                 */
54
55
                try {
56
                    $registered = yield (new AgentServiceRegister($agent))->service($service)->result();
57
                } catch (Throwable $e) {
58
                    logger('consul')->warning('Service registering error', [
59
                        'svc' => $service->id(),
60
                        'error' => sprintf('%s::%s', get_class($e), $e->getMessage()),
61
                    ]);
62
                    goto RETRYING;
63
                }
64
65
                if ($registered->success()) {
66
                    return;
67
                }
68
69
                logger('consul')->info('Service registering failed', [
70
                    'svc' => $service->id(),
71
                    'agent' => $service->hosting(),
72
                ]);
73
74
                RETRYING:
75
76
                yield msleep($sleep = rand(Defaults::ERROR_RETRY_MIN, Defaults::ERROR_RETRY_MAX));
77
78
                logger('consul')->notice('Service register retrying', [
79
                    'svc' => $service->id(),
80
                    'delay' => $sleep,
81
                ]);
82
            }
83
        }, null, $this->agent, $serviced)->fusion();
84
85
        return $serviced;
86
    }
87
}
88