PluginProbe ʕ •ᴥ•ʔ
WP STAGING – WordPress Backup, Restore, Migration & Clone / 4.1.4
WP STAGING – WordPress Backup, Restore, Migration & Clone v4.1.4
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 / Job / JobTransientCache.php
wp-staging / Framework / Job Last commit date
Ajax 1 year ago BackgroundProcessing 1 year ago Dto 1 year ago Exception 1 year ago Interfaces 1 year ago Jobs 1 year ago Task 1 year ago AbstractJob.php 1 year ago JobProvider.php 1 year ago JobServiceProvider.php 1 year ago JobTransientCache.php 1 year ago ProcessLock.php 1 year ago
JobTransientCache.php
175 lines
1 <?php
2
3 namespace WPStaging\Framework\Job;
4
5 /**
6 * It is used to cache the current running job data in a transient.
7 * So it can be used for BackgroundLogger to push the events to the SSE stream.
8 */
9 class JobTransientCache
10 {
11 /**
12 * This is the time in seconds that the job transient will be kept.
13 * This is used to show the current job status in the UI.
14 * @var int
15 */
16 const JOB_TRANSIENT_EXPIRY = 60 * 60 * 6; // 6 hours
17
18 /**
19 * This is the time in seconds that the job transient will be kept after the job is completed.
20 * Instead of deleting the transient immediately, we reduce it expiry to 15 seconds, to every open SSE stream
21 * can get the latest status of the job.
22 * @var int
23 */
24 const JOB_TRANSIENT_EXPIRY_ON_COMPLETE = 15; // 15 seconds
25
26 /**
27 * This is the transient key that will be used to store the current job data.
28 * @var string
29 */
30 const TRANSIENT_CURRENT_JOB = 'wpstg_current_job';
31
32 /**
33 * @var string
34 */
35 const STATUS_RUNNING = 'running';
36
37 /**
38 * @var string
39 */
40 const STATUS_SUCCESS = 'success';
41
42 /**
43 * @var string
44 */
45 const STATUS_FAILED = 'failed';
46
47 /**
48 * @var string
49 */
50 const STATUS_CANCELLED = 'cancelled';
51
52 /**
53 * @var string
54 */
55 const JOB_TYPE_BACKUP = 'Backup';
56
57 /**
58 * @var string
59 */
60 const JOB_TYPE_RESTORE = 'Restore';
61
62 /**
63 * @var string
64 */
65 const JOB_TYPE_CANCEL = 'Cancel';
66
67 /**
68 * @var string
69 */
70 const JOB_TYPE_STAGING = 'Staging';
71
72 /**
73 * @var string
74 */
75 const JOB_TYPE_STAGING_DELETE = 'Staging_Delete';
76
77 const CANCELABLE_JOBS = [
78 self::JOB_TYPE_BACKUP,
79 self::JOB_TYPE_RESTORE,
80 self::JOB_TYPE_STAGING,
81 ];
82
83 /**
84 * @param string $jobId
85 * @param string $jobTitle
86 * @param string $jobType
87 * @param string $queueId
88 * @return void
89 */
90 public function startJob(string $jobId, string $jobTitle, string $jobType = 'job', string $queueId = '')
91 {
92 $jobData = [
93 'jobId' => $jobId,
94 'title' => $jobTitle,
95 'type' => $jobType,
96 'status' => self::STATUS_RUNNING,
97 'start' => time(),
98 'queueId' => $queueId,
99 ];
100
101 delete_transient(self::TRANSIENT_CURRENT_JOB);
102 set_transient(self::TRANSIENT_CURRENT_JOB, $jobData, self::JOB_TRANSIENT_EXPIRY);
103 }
104
105 /**
106 * @return void
107 */
108 public function completeJob()
109 {
110 $this->stopJob(self::STATUS_SUCCESS);
111 }
112
113 /**
114 * @return void
115 */
116 public function cancelJob(string $jobTitle)
117 {
118 $this->stopJob(self::STATUS_CANCELLED, $jobTitle);
119 }
120
121 /**
122 * @return void
123 */
124 public function failJob()
125 {
126 $this->stopJob(self::STATUS_FAILED);
127 }
128
129 /**
130 * @return array|null
131 */
132 public function getJob()
133 {
134 $jobData = get_transient(self::TRANSIENT_CURRENT_JOB);
135 if (empty($jobData['jobId'])) {
136 return null;
137 }
138
139 return $jobData;
140 }
141
142 public function getJobId(): string
143 {
144 $jobData = $this->getJob();
145 if (empty($jobData['jobId'])) {
146 return '';
147 }
148
149 return $jobData['jobId'];
150 }
151
152 public function getJobStatus(): string
153 {
154 $jobData = $this->getJob();
155 if (empty($jobData['status'])) {
156 return '';
157 }
158
159 return $jobData['status'];
160 }
161
162 private function stopJob(string $status, string $title = '')
163 {
164 $jobData = $this->getJob();
165 $jobData['status'] = $status;
166 if (!empty($title)) {
167 $jobData['title'] = $title;
168 }
169
170 // This will make sure to update the expiry as well if the status was already the same!
171 delete_transient(self::TRANSIENT_CURRENT_JOB);
172 set_transient(self::TRANSIENT_CURRENT_JOB, $jobData, self::JOB_TRANSIENT_EXPIRY_ON_COMPLETE);
173 }
174 }
175