SseEventCache.php
134 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPStaging\Framework\Logger; |
| 4 | |
| 5 | use WPStaging\Framework\Adapter\Directory; |
| 6 | use WPStaging\Framework\Utils\Cache\Cache; |
| 7 | |
| 8 | /** |
| 9 | * This class is used to cache the events for the SSE (Server-Sent Events) stream. |
| 10 | * It stores the events in a cache file and allows to push new events, load existing events, |
| 11 | * It is used by BackgroundLogger to push the events to the SSE stream. |
| 12 | */ |
| 13 | class SseEventCache |
| 14 | { |
| 15 | /** |
| 16 | * @var string |
| 17 | */ |
| 18 | const EVENT_TYPE_TASK = 'task'; |
| 19 | |
| 20 | /** |
| 21 | * @var string |
| 22 | */ |
| 23 | const EVENT_TYPE_MEMORY_EXHAUST = 'memory_exhaust'; |
| 24 | |
| 25 | /** |
| 26 | * @var string |
| 27 | */ |
| 28 | const EVENT_TYPE_FATAL_ERROR = 'fatal_error'; |
| 29 | |
| 30 | /** |
| 31 | * @var string |
| 32 | */ |
| 33 | const EVENT_TYPE_COMPLETE = 'complete'; |
| 34 | |
| 35 | /** |
| 36 | * @var string |
| 37 | */ |
| 38 | protected $cacheDirectory = ''; |
| 39 | |
| 40 | /** |
| 41 | * @var int |
| 42 | */ |
| 43 | protected $count = 0; |
| 44 | |
| 45 | /** |
| 46 | * @var array |
| 47 | */ |
| 48 | protected $events = []; |
| 49 | |
| 50 | /** |
| 51 | * @var Cache |
| 52 | */ |
| 53 | protected $cache; |
| 54 | |
| 55 | public function __construct(Cache $cache, Directory $directory) |
| 56 | { |
| 57 | $this->cacheDirectory = $directory->getSseCacheDirectory(); |
| 58 | $this->cache = $cache; |
| 59 | $this->cache->setPath($this->cacheDirectory); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * @return void |
| 64 | */ |
| 65 | public function deleteSseCacheFiles() |
| 66 | { |
| 67 | if (!file_exists($this->cacheDirectory)) { |
| 68 | return; |
| 69 | } |
| 70 | |
| 71 | $iterator = new \DirectoryIterator($this->cacheDirectory); |
| 72 | foreach ($iterator as $fileInfo) { |
| 73 | if ($fileInfo->isFile() && strpos($fileInfo->getFilename(), 'sse.cache.php') !== false) { |
| 74 | unlink($fileInfo->getPathname()); |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | public function setJobId(string $jobId, bool $checkIfExist = false) |
| 80 | { |
| 81 | $this->cache->setFilename($jobId . '.sse'); |
| 82 | if ($checkIfExist && !$this->cache->isValid(false)) { |
| 83 | return false; |
| 84 | } |
| 85 | |
| 86 | return true; |
| 87 | } |
| 88 | |
| 89 | public function push(array $log) |
| 90 | { |
| 91 | // Reload to absorb concurrent appends from sibling Logger instances in the same request. |
| 92 | $this->load(); |
| 93 | |
| 94 | $this->events[] = $log; |
| 95 | |
| 96 | $this->count++; |
| 97 | $this->cache->save($this->events); |
| 98 | } |
| 99 | |
| 100 | public function load() |
| 101 | { |
| 102 | // PHP's stat cache otherwise hides cross-process writes from the SSE polling loop. |
| 103 | $filePath = $this->cache->getFilePath(); |
| 104 | if ($filePath !== '') { |
| 105 | clearstatcache(true, $filePath); |
| 106 | } |
| 107 | |
| 108 | if (!$this->cache->isValid()) { |
| 109 | return; |
| 110 | } |
| 111 | |
| 112 | $this->events = $this->cache->get([]); |
| 113 | if (!is_array($this->events)) { |
| 114 | $this->events = []; |
| 115 | } |
| 116 | |
| 117 | $this->count = count($this->events); |
| 118 | } |
| 119 | |
| 120 | public function getCount() |
| 121 | { |
| 122 | return $this->count; |
| 123 | } |
| 124 | |
| 125 | public function getEvents(int $offset = 0) |
| 126 | { |
| 127 | if ($offset >= $this->count) { |
| 128 | return []; |
| 129 | } |
| 130 | |
| 131 | return array_slice($this->events, $offset); |
| 132 | } |
| 133 | } |
| 134 |