summaryrefslogtreecommitdiff
path: root/server/vendor/php-opencloud/common/src/Common/Resource/HasWaiterTrait.php
blob: 519be7e2ae0b625811f8b5ba399856deefa5d239 (plain)
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
<?php

namespace OpenCloud\Common\Resource;

use OpenCloud\Common\Error\BadResponseError;

/**
 * Contains reusable functionality for resources that have long operations which require waiting in
 * order to reach a particular state.
 *
 * @codeCoverageIgnore
 *
 * @package OpenCloud\Common\Resource
 */
trait HasWaiterTrait
{
    /**
     * Provides a blocking operation until the resource has reached a particular state. The method
     * will enter a loop, requesting feedback from the remote API until it sends back an appropriate
     * status.
     *
     * @param string $status      The state to be reached
     * @param int    $timeout     The maximum timeout. If the total time taken by the waiter has reached
     *                            or exceed this timeout, the blocking operation will immediately cease.
     * @param int    $sleepPeriod The amount of time to pause between each HTTP request.
     */
    public function waitUntil($status, $timeout = 60, $sleepPeriod = 1)
    {
        $startTime = time();

        while (true) {
            $this->retrieve();

            if ($this->status == $status || $this->shouldHalt($timeout, $startTime)) {
                break;
            }

            sleep($sleepPeriod);
        }
    }

    /**
     * Provides a blocking operation until the resource has reached a particular state. The method
     * will enter a loop, executing the callback until TRUE is returned. This provides great
     * flexibility.
     *
     * @param callable $fn          An anonymous function that will be executed on every iteration. You can
     *                              encapsulate your own logic to determine whether the resource has
     *                              successfully transitioned. When TRUE is returned by the callback,
     *                              the loop will end.
     * @param int|bool $timeout     The maximum timeout in seconds. If the total time taken by the waiter has reached
     *                              or exceed this timeout, the blocking operation will immediately cease. If FALSE
     *                              is provided, the timeout will never be considered.
     * @param int      $sleepPeriod The amount of time to pause between each HTTP request.
     */
    public function waitWithCallback(callable $fn, $timeout = 60, $sleepPeriod = 1)
    {
        $startTime = time();

        while (true) {
            $this->retrieve();

            $response = call_user_func_array($fn, [$this]);

            if ($response === true || $this->shouldHalt($timeout, $startTime)) {
                break;
            }

            sleep($sleepPeriod);
        }
    }

    /**
     * Internal method used to identify whether a timeout has been exceeded.
     *
     * @param bool|int $timeout
     * @param int      $startTime
     *
     * @return bool
     */
    private function shouldHalt($timeout, $startTime)
    {
        if ($timeout === false) {
            return false;
        }

        return time() - $startTime >= $timeout;
    }

    /**
     * Convenience method providing a blocking operation until the resource transitions to an
     * ``ACTIVE`` status.
     *
     * @param int|bool $timeout The maximum timeout in seconds. If the total time taken by the waiter has reached
     *                          or exceed this timeout, the blocking operation will immediately cease. If FALSE
     *                          is provided, the timeout will never be considered.
     */
    public function waitUntilActive($timeout = false)
    {
        $this->waitUntil('ACTIVE', $timeout);
    }

    public function waitUntilDeleted($timeout = 60, $sleepPeriod = 1)
    {
        $startTime = time();

        while (true) {
            try {
                $this->retrieve();
            } catch (BadResponseError $e) {
                if ($e->getResponse()->getStatusCode() === 404) {
                    break;
                }
                throw $e;
            }

            if ($this->shouldHalt($timeout, $startTime)) {
                break;
            }

            sleep($sleepPeriod);
        }
    }
}