Adapter
6 months ago
Analytics
8 months ago
Assets
6 months ago
BackgroundProcessing
6 months ago
CloningProcess
6 months ago
Collection
3 years ago
Command
5 years ago
Component
8 months ago
DI
6 months ago
Database
6 months ago
DependencyResolver
2 years ago
Exceptions
2 years ago
Facades
6 months ago
Filesystem
6 months ago
Interfaces
5 years ago
Job
6 months ago
Language
1 year ago
Logger
6 months ago
Mails
6 months ago
Network
8 months ago
Newsfeed
8 months ago
Notices
6 months ago
Performance
8 months ago
Permalinks
11 months ago
Queue
8 months ago
Rest
1 year ago
Security
6 months ago
Settings
6 months ago
TemplateEngine
6 months ago
ThirdParty
8 months ago
Traits
6 months ago
Utils
6 months ago
AnalyticsServiceProvider.php
11 months ago
AssetServiceProvider.php
1 year ago
CommonServiceProvider.php
6 months ago
ErrorHandler.php
8 months ago
NoticeServiceProvider.php
11 months ago
SettingsServiceProvider.php
2 years ago
SiteInfo.php
8 months ago
Url.php
3 years ago
ErrorHandler.php
121 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPStaging\Framework; |
| 4 | |
| 5 | use WPStaging\Core\WPStaging; |
| 6 | use WPStaging\Framework\Job\JobTransientCache; |
| 7 | use WPStaging\Framework\Logger\SseEventCache; |
| 8 | |
| 9 | /** |
| 10 | * @package WPStaging\Framework |
| 11 | */ |
| 12 | class ErrorHandler |
| 13 | { |
| 14 | /** @var string */ |
| 15 | const ERROR_FILE_EXTENSION = '.wpstgerror'; |
| 16 | |
| 17 | public function registerShutdownHandler() |
| 18 | { |
| 19 | register_shutdown_function([$this, 'shutdownHandler']); |
| 20 | } |
| 21 | |
| 22 | public function shutdownHandler() |
| 23 | { |
| 24 | if (!defined('WPSTG_REQUEST')) { |
| 25 | return; |
| 26 | } |
| 27 | |
| 28 | if (!defined('WPSTG_UPLOADS_DIR')) { |
| 29 | return; |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * Requests for which to check for memory exhaustion |
| 34 | * Using hardcoded values below to avoid loading all classes |
| 35 | * @var array $wpStagingRequests |
| 36 | */ |
| 37 | $wpStagingRequests = [ |
| 38 | 'wpstg_backup', // @see WPStaging\Backup\Ajax\Backup::WPSTG_REQUEST |
| 39 | 'wpstg_restore', // @see WPStaging\Backup\Ajax\Restore::WPSTG_REQUEST |
| 40 | 'wpstg_cloning', // @see WPStaging\Backend\Modules\Jobs\Cloning::WPSTG_REQUEST, |
| 41 | 'wpstg_remote_sync_pull', // @see WPStaging\Pro\RemoteSync\BackgroundProcessing\PreparePull::WPSTG_REQUEST |
| 42 | ]; |
| 43 | |
| 44 | $wpStagingRequest = WPSTG_REQUEST; |
| 45 | if (!in_array($wpStagingRequest, $wpStagingRequests, true)) { |
| 46 | return; |
| 47 | } |
| 48 | |
| 49 | $error = error_get_last(); |
| 50 | if (!is_array($error)) { |
| 51 | return; |
| 52 | } |
| 53 | |
| 54 | if ($error['type'] !== E_ERROR) { |
| 55 | return; |
| 56 | } |
| 57 | |
| 58 | preg_match('/Allowed memory size of (\d+) bytes exhausted \(tried to allocate (\d+) bytes\)/', $error['message'], $data); |
| 59 | if (!is_array($data) || count($data) !== 3) { |
| 60 | $data['time'] = date('Y/m/d H:i:s', time()); // @see WPStaging\Core\Utils\Logger::LOG_DATETIME_FORMAT, use hardcoded value to avoid loading class |
| 61 | $this->logSseEvent($data, false); |
| 62 | return; |
| 63 | } |
| 64 | |
| 65 | // Temporary file to store the error message |
| 66 | $errorTmpFile = WPSTG_UPLOADS_DIR . $wpStagingRequest . self::ERROR_FILE_EXTENSION; |
| 67 | |
| 68 | $fileHandler = fopen($errorTmpFile, 'w'); |
| 69 | |
| 70 | $data = [ |
| 71 | 'memoryUsage' => memory_get_usage(true), |
| 72 | 'peakMemoryUsage' => memory_get_peak_usage(true), |
| 73 | 'phpMemoryLimit' => ini_get('memory_limit'), |
| 74 | 'wpMemoryLimit' => defined('WP_MEMORY_LIMIT') ? WP_MEMORY_LIMIT : '', |
| 75 | 'allowedMemoryLimit' => $data[1], |
| 76 | 'exhaustedMemorySize' => $data[2], |
| 77 | 'time' => date('Y/m/d H:i:s', time()), // @see WPStaging\Core\Utils\Logger::LOG_DATETIME_FORMAT, use hardcoded value to avoid loading class |
| 78 | ]; |
| 79 | |
| 80 | if (is_resource($fileHandler)) { |
| 81 | fwrite($fileHandler, json_encode($data)); |
| 82 | fclose($fileHandler); |
| 83 | } |
| 84 | |
| 85 | $this->logSseEvent($data); |
| 86 | } |
| 87 | |
| 88 | private function logSseEvent(array $data, bool $isMemoryExhaust = true) |
| 89 | { |
| 90 | /** |
| 91 | * @var JobTransientCache $jobTransientCache |
| 92 | */ |
| 93 | $jobTransientCache = WPStaging::make(JobTransientCache::class); |
| 94 | |
| 95 | $jobId = $jobTransientCache->getJobId(); |
| 96 | if (empty($jobId)) { |
| 97 | return; |
| 98 | } |
| 99 | |
| 100 | $message = $isMemoryExhaust ? |
| 101 | "Job failed, Memory exceed allowed size! Allowed memory: {$data['allowedMemoryLimit']} bytes. Exceeded memory: {$data['exhaustedMemorySize']} bytes" : |
| 102 | "Job failed due to a fatal error! Error data: " . print_r($data, true); |
| 103 | |
| 104 | $data['jobId'] = $jobId; |
| 105 | $data['message'] = $message; |
| 106 | |
| 107 | /** |
| 108 | * @var SseEventCache $sseEventCache |
| 109 | */ |
| 110 | $sseEventCache = WPStaging::make(SseEventCache::class); |
| 111 | $sseEventCache->setJobId($jobId); |
| 112 | $sseEventCache->load(); |
| 113 | $sseEventCache->push([ |
| 114 | 'type' => $isMemoryExhaust ? SseEventCache::EVENT_TYPE_MEMORY_EXHAUST : SseEventCache::EVENT_TYPE_FATAL_ERROR, |
| 115 | 'data' => $data, |
| 116 | ]); |
| 117 | |
| 118 | $jobTransientCache->failJob('', $message); |
| 119 | } |
| 120 | } |
| 121 |