SDeregister::deregistered()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
/**
3
 * Service deregister
4
 * User: moyo
5
 * Date: 18/09/2017
6
 * Time: 10:26 PM
7
 */
8
9
namespace Carno\Consul\Chips;
10
11
use Carno\Consul\APIs\AgentServiceDeregister;
12
use Carno\Consul\Contracts\Defaults;
13
use Carno\Consul\Types\Result;
14
use Carno\Consul\Types\Service;
15
use function Carno\Coroutine\async;
16
use function Carno\Coroutine\msleep;
17
use Carno\Promise\Promised;
18
use Throwable;
19
20
trait SDeregister
21
{
22
    /**
23
     * @return Promised
24
     */
25
    public function deregister() : Promised
26
    {
27
        return async(function (Service $service) {
28
            for (;;) {
29
                if (!$service->connected()) {
30
                    logger('consul')->info('Service has not been registered .. skip', ['svc' => $service->id()]);
31
                    return;
32
                }
33
34
                /**
35
                 * @var Result $result
36
                 */
37
38
                try {
39
                    $result = yield (new AgentServiceDeregister($service->hosting()))->service($service)->result();
40
                } catch (Throwable $e) {
41
                    logger('consul')->warning('Service deregistering error', [
42
                        'svc' => $service->id(),
43
                        'error' => sprintf('%s::%s', get_class($e), $e->getMessage()),
44
                    ]);
45
                    goto RETRYING;
46
                }
47
48
                if ($result->success()) {
49
                    return;
50
                }
51
52
                RETRYING:
53
54
                yield msleep($sleep = rand(Defaults::ERROR_RETRY_MIN, Defaults::ERROR_RETRY_MAX));
55
56
                logger('consul')->notice('Service deregister retrying', [
57
                    'svc' => $service->id(),
58
                    'delay' => $sleep,
59
                ]);
60
            }
61
        }, null, $this);
62
    }
63
64
    /**
65
     * @param Result $result
66
     * @return Result
67
     */
68
    public function deregistered(Result $result) : Result
69
    {
70
        $this->kaShutdown();
0 ignored issues
show
Bug introduced by
It seems like kaShutdown() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

70
        $this->/** @scrutinizer ignore-call */ 
71
               kaShutdown();
Loading history...
71
72
        return $result;
73
    }
74
}
75