Utils.php 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. /**
  3. *
  4. * @copyright Copyright (c) 2017, Daniel Calviño Sánchez (danxuliu@gmail.com)
  5. *
  6. * @license GNU AGPL version 3 or any later version
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License as
  10. * published by the Free Software Foundation, either version 3 of the
  11. * License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. class Utils {
  23. /**
  24. * Waits at most $timeout seconds for the given condition to be true,
  25. * checking it again every $timeoutStep seconds.
  26. *
  27. * Note that the timeout is no longer taken into account when a condition is
  28. * met; that is, true will be returned if the condition is met before the
  29. * timeout expires, but also if it is met exactly when the timeout expires.
  30. * For example, even if the timeout is set to 0, the condition will be
  31. * checked at least once, and true will be returned in that case if the
  32. * condition was met.
  33. *
  34. * @param \Closure $conditionCallback the condition to wait for, as a
  35. * function that returns a boolean.
  36. * @param float $timeout the number of seconds (decimals allowed) to wait at
  37. * most for the condition to be true.
  38. * @param float $timeoutStep the number of seconds (decimals allowed) to
  39. * wait before checking the condition again.
  40. * @return boolean true if the condition is met before (or exactly when) the
  41. * timeout expires, false otherwise.
  42. */
  43. public static function waitFor($conditionCallback, $timeout, $timeoutStep) {
  44. $elapsedTime = 0;
  45. $conditionMet = false;
  46. while (!($conditionMet = $conditionCallback()) && $elapsedTime < $timeout) {
  47. usleep($timeoutStep * 1000000);
  48. $elapsedTime += $timeoutStep;
  49. }
  50. return $conditionMet;
  51. }
  52. /**
  53. * Waits at most $timeout seconds for the server at the given URL to be up,
  54. * checking it again every $timeoutStep seconds.
  55. *
  56. * Note that it does not verify whether the URL returns a valid HTTP status
  57. * or not; it simply checks that the server at the given URL is accessible.
  58. *
  59. * @param string $url the URL for the server to check.
  60. * @param float $timeout the number of seconds (decimals allowed) to wait at
  61. * most for the server.
  62. * @param float $timeoutStep the number of seconds (decimals allowed) to
  63. * wait before checking the server again; by default, 0.5 seconds.
  64. * @return boolean true if the server was found, false otherwise.
  65. */
  66. public static function waitForServer($url, $timeout, $timeoutStep = 0.5) {
  67. $isServerUpCallback = function () use ($url) {
  68. $curlHandle = curl_init($url);
  69. // Returning the transfer as the result of curl_exec prevents the
  70. // transfer from being written to the output.
  71. curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, true);
  72. $transfer = curl_exec($curlHandle);
  73. curl_close($curlHandle);
  74. return $transfer !== false;
  75. };
  76. return self::waitFor($isServerUpCallback, $timeout, $timeoutStep);
  77. }
  78. }