tenweb-speed-optimizer
/
vendor
/
10web-utils
/
10web-queue
/
src
/
TenWebQueue
/
Traits
/
QueueRestart.php
tenweb-speed-optimizer
/
vendor
/
10web-utils
/
10web-queue
/
src
/
TenWebQueue
/
Traits
Last commit date
QueueRestart.php
3 years ago
QueueRestart.php
110 lines
| 1 | <?php |
| 2 | namespace TenWebQueue\Traits; |
| 3 | |
| 4 | trait QueueRestart |
| 5 | { |
| 6 | /** |
| 7 | * @var float $startTime |
| 8 | */ |
| 9 | private $startTime = 0; |
| 10 | |
| 11 | /** |
| 12 | * @param $itemsCount |
| 13 | * @param $itemsAllowedCount |
| 14 | * |
| 15 | * @return bool |
| 16 | */ |
| 17 | public function ifRestartNeeded($itemsCount, $itemsAllowedCount = 20) |
| 18 | { |
| 19 | $maxExecTime = ini_get('max_execution_time'); |
| 20 | $scriptExecTime = microtime(true) - $this->getStartTime(); |
| 21 | |
| 22 | if ($scriptExecTime >= (int)$maxExecTime - 10 || $itemsCount >= $itemsAllowedCount) { |
| 23 | |
| 24 | return true; |
| 25 | } |
| 26 | |
| 27 | return false; |
| 28 | } |
| 29 | |
| 30 | public function restart($route, $body) |
| 31 | { |
| 32 | /*$ch = curl_init(); |
| 33 | |
| 34 | curl_setopt($ch, CURLOPT_URL, $route); |
| 35 | curl_setopt($ch, CURLOPT_POST, true); |
| 36 | curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); |
| 37 | curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); |
| 38 | curl_setopt($ch, CURLOPT_TIMEOUT, 0.1); |
| 39 | curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0.1); |
| 40 | curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array('restart' => 1))); |
| 41 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
| 42 | curl_exec($ch); |
| 43 | |
| 44 | curl_close($ch);*/ |
| 45 | //todo change this to curl |
| 46 | try { |
| 47 | $route = $route . '&c=' . rand(1, 5000); |
| 48 | wp_remote_post($route, array('method' => 'POST', 'sslverify' => false, 'timeout' => 0.1, 'body' => $body)); |
| 49 | |
| 50 | } catch (\Exception $e) { |
| 51 | |
| 52 | } |
| 53 | |
| 54 | die('{"tenweb_queue_restart": "1"}'); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * @param $object |
| 59 | * @param $path |
| 60 | * |
| 61 | * @return void |
| 62 | */ |
| 63 | public static function writeObjectInfile($object, $path) |
| 64 | { |
| 65 | $content = serialize($object); |
| 66 | file_put_contents($path, $content); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * @param $path |
| 71 | * |
| 72 | * @return mixed|null |
| 73 | */ |
| 74 | public static function readObjectFromFile($path) |
| 75 | { |
| 76 | if (is_file($path)) { |
| 77 | return unserialize(file_get_contents($path)); |
| 78 | } |
| 79 | |
| 80 | return null; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * @param $path |
| 85 | * |
| 86 | */ |
| 87 | public static function deleteObjectFile($path) |
| 88 | { |
| 89 | if (is_file($path)) { |
| 90 | unlink($path); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * @return float |
| 96 | */ |
| 97 | public function getStartTime() |
| 98 | { |
| 99 | return $this->startTime; |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * @param float $startTime |
| 104 | */ |
| 105 | public function setStartTime($startTime) |
| 106 | { |
| 107 | $this->startTime = $startTime; |
| 108 | } |
| 109 | } |
| 110 |