PluginProbe ʕ •ᴥ•ʔ
WP STAGING – WordPress Backup, Restore, Migration & Clone / 4.7.3
WP STAGING – WordPress Backup, Restore, Migration & Clone v4.7.3
4.9.1 4.9.0 4.8.1 trunk 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.1.0 3.1.1 3.1.2 3.1.3 3.1.4 3.10.0 3.2.0 3.3.1 3.3.2 3.3.3 3.4.1 3.4.3 3.5.0 3.6.0 3.7.1 3.8.0 3.8.1 3.8.2 3.8.3 3.8.4 3.8.5 3.8.6 3.8.7 3.9.0 3.9.1 3.9.2 3.9.3 3.9.4 4.0.0 4.1.0 4.1.1 4.1.2 4.1.3 4.1.4 4.2.0 4.2.1 4.3.0 4.3.1 4.3.2 4.4.0 4.5.0 4.6.0 4.7.0 4.7.1 4.7.2 4.7.3 4.8.0
wp-staging / Framework / ErrorHandler.php
wp-staging / Framework Last commit date
Adapter 2 months ago Analytics 4 months ago Assets 2 months ago BackgroundProcessing 3 months ago CloningProcess 4 months ago Collection 3 years ago Command 5 years ago Component 8 months ago DI 6 months ago Database 2 months ago DependencyResolver 2 years ago Exceptions 2 years ago Facades 2 months ago Filesystem 2 months ago Interfaces 5 years ago Job 3 months ago Language 3 months ago Logger 2 months ago Mails 3 months ago Network 2 months ago Newsfeed 4 months ago Notices 2 months ago Performance 2 months ago Permalinks 11 months ago Queue 4 months ago Rest 2 months ago Security 5 months ago Settings 2 months ago TemplateEngine 6 months ago ThirdParty 5 months ago Traits 2 months ago Utils 2 months ago AnalyticsServiceProvider.php 11 months ago AssetServiceProvider.php 1 year ago CommonServiceProvider.php 3 months ago ErrorHandler.php 8 months ago NoticeServiceProvider.php 11 months ago SettingsServiceProvider.php 2 months ago SiteInfo.php 5 months ago Url.php 4 months 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