PluginProbe ʕ •ᴥ•ʔ
WP STAGING – WordPress Backup, Restore, Migration & Clone / 4.3.1
WP STAGING – WordPress Backup, Restore, Migration & Clone v4.3.1
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 11 months ago Analytics 10 months ago Assets 9 months ago BackgroundProcessing 9 months ago CloningProcess 9 months ago Collection 3 years ago Command 5 years ago Component 2 years ago DI 1 year ago Database 9 months ago DependencyResolver 2 years ago Exceptions 2 years ago Facades 9 months ago Filesystem 9 months ago Interfaces 5 years ago Job 9 months ago Language 1 year ago Logger 9 months ago Mails 1 year ago Network 9 months ago Newsfeed 10 months ago Notices 11 months ago Performance 1 year ago Permalinks 11 months ago Queue 1 year ago Rest 1 year ago Security 11 months ago Settings 1 year ago TemplateEngine 11 months ago ThirdParty 1 year ago Traits 9 months ago Utils 9 months ago AnalyticsServiceProvider.php 11 months ago AssetServiceProvider.php 1 year ago CommonServiceProvider.php 11 months ago ErrorHandler.php 9 months ago NoticeServiceProvider.php 11 months ago SettingsServiceProvider.php 2 years ago SiteInfo.php 11 months ago Url.php 3 years ago
ErrorHandler.php
116 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 return;
61 }
62
63 // Temporary file to store the error message
64 $errorTmpFile = WPSTG_UPLOADS_DIR . $wpStagingRequest . self::ERROR_FILE_EXTENSION;
65
66 $fileHandler = fopen($errorTmpFile, 'w');
67
68 $data = [
69 'memoryUsage' => memory_get_usage(true),
70 'peakMemoryUsage' => memory_get_peak_usage(true),
71 'phpMemoryLimit' => ini_get('memory_limit'),
72 'wpMemoryLimit' => defined('WP_MEMORY_LIMIT') ? WP_MEMORY_LIMIT : '',
73 'allowedMemoryLimit' => $data[1],
74 'exhaustedMemorySize' => $data[2],
75 'time' => date('Y/m/d H:i:s', time()), // @see WPStaging\Core\Utils\Logger::LOG_DATETIME_FORMAT, use hardcoded value to avoid loading class
76 ];
77
78 if (is_resource($fileHandler)) {
79 fwrite($fileHandler, json_encode($data));
80 fclose($fileHandler);
81 }
82
83 $this->logSseEvent($data);
84 }
85
86 private function logSseEvent(array $data)
87 {
88 /**
89 * @var JobTransientCache $jobTransientCache
90 */
91 $jobTransientCache = WPStaging::make(JobTransientCache::class);
92
93 $jobId = $jobTransientCache->getJobId();
94 if (empty($jobId)) {
95 return;
96 }
97
98 $message = "Job failed, Memory exceed allowed size! Allowed memory: {$data['allowedMemoryLimit']} bytes. Exceeded memory: {$data['exhaustedMemorySize']} bytes";
99 $data['jobId'] = $jobId;
100 $data['message'] = $message;
101
102 /**
103 * @var SseEventCache $sseEventCache
104 */
105 $sseEventCache = WPStaging::make(SseEventCache::class);
106 $sseEventCache->setJobId($jobId);
107 $sseEventCache->load();
108 $sseEventCache->push([
109 'type' => SseEventCache::EVENT_TYPE_MEMORY_EXHAUST,
110 'data' => $data,
111 ]);
112
113 $jobTransientCache->failJob('', $message);
114 }
115 }
116