Passed
Pull Request — master (#4676)
by Nils
05:55
created
scripts/background_tasks___handler.php 2 patches
Spacing   +23 added lines, -23 removed lines patch added patch discarded remove patch
@@ -30,7 +30,7 @@  discard block
 block discarded – undo
30 30
 use TeampassClasses\ConfigManager\ConfigManager;
31 31
 
32 32
 require_once __DIR__.'/../sources/main.functions.php';
33
-require_once __DIR__ . '/taskLogger.php';
33
+require_once __DIR__.'/taskLogger.php';
34 34
 
35 35
 class BackgroundTasksHandler {
36 36
     private $settings;
@@ -55,7 +55,7 @@  discard block
 block discarded – undo
55 55
     public function processBackgroundTasks() {
56 56
         // Prevent multiple concurrent executions
57 57
         if (!$this->acquireProcessLock()) {
58
-            if (LOG_TASKS=== true) $this->logger->log('Process already running', 'INFO');
58
+            if (LOG_TASKS === true) $this->logger->log('Process already running', 'INFO');
59 59
             return false;
60 60
         }
61 61
 
@@ -64,7 +64,7 @@  discard block
 block discarded – undo
64 64
             $this->processTaskBatches();
65 65
             $this->performMaintenanceTasks();
66 66
         } catch (Exception $e) {
67
-            if (LOG_TASKS=== true) $this->logger->log('Task processing error: ' . $e->getMessage(), 'ERROR');
67
+            if (LOG_TASKS === true) $this->logger->log('Task processing error: '.$e->getMessage(), 'ERROR');
68 68
         } finally {
69 69
             $this->releaseProcessLock();
70 70
         }
@@ -83,7 +83,7 @@  discard block
 block discarded – undo
83 83
             return false;
84 84
         }
85 85
         
86
-        fwrite($fp, (string)getmypid());
86
+        fwrite($fp, (string) getmypid());
87 87
         return true;
88 88
     }
89 89
 
@@ -101,7 +101,7 @@  discard block
 block discarded – undo
101 101
     private function cleanupStaleTasks() {
102 102
         // Mark tasks as failed if they've been running too long
103 103
         DB::query(
104
-            'UPDATE ' . prefixTable('background_tasks') . ' 
104
+            'UPDATE '.prefixTable('background_tasks').' 
105 105
             SET is_in_progress = -1, 
106 106
                 finished_at = %i, 
107 107
                 status = "failed"
@@ -113,8 +113,8 @@  discard block
 block discarded – undo
113 113
 
114 114
         // Remove very old failed tasks
115 115
         DB::query(
116
-            'DELETE t, st FROM ' . prefixTable('background_tasks') . ' t
117
-            INNER JOIN ' . prefixTable('background_subtasks') . ' st ON (t.increment_id = st.task_id)
116
+            'DELETE t, st FROM '.prefixTable('background_tasks').' t
117
+            INNER JOIN ' . prefixTable('background_subtasks').' st ON (t.increment_id = st.task_id)
118 118
             WHERE t.finished_at > %i 
119 119
             AND t.status = %s',
120 120
             time() - $this->maxTimeBeforeRemoval,
@@ -131,7 +131,7 @@  discard block
 block discarded – undo
131 131
         
132 132
         // Check if the maximum number of parallel tasks is reached
133 133
         if ($runningTasks >= $this->maxParallelTasks) {
134
-            if (LOG_TASKS=== true) $this->logger->log('Wait ... '.$runningTasks.' out of '.$this->maxParallelTasks.' are already running ', 'INFO');
134
+            if (LOG_TASKS === true) $this->logger->log('Wait ... '.$runningTasks.' out of '.$this->maxParallelTasks.' are already running ', 'INFO');
135 135
             return;
136 136
         }
137 137
 
@@ -140,7 +140,7 @@  discard block
 block discarded – undo
140 140
         // Fetch next batch of tasks
141 141
         $tasks = DB::query(
142 142
             'SELECT increment_id, process_type, arguments 
143
-            FROM ' . prefixTable('background_tasks') . '
143
+            FROM ' . prefixTable('background_tasks').'
144 144
             WHERE is_in_progress = 0 
145 145
             AND (finished_at IS NULL OR finished_at = "")
146 146
             ORDER BY increment_id ASC
@@ -149,7 +149,7 @@  discard block
 block discarded – undo
149 149
         );
150 150
 
151 151
         foreach ($tasks as $task) {
152
-            if (LOG_TASKS=== true) $this->logger->log('Launching '.$task['increment_id'], 'INFO');
152
+            if (LOG_TASKS === true) $this->logger->log('Launching '.$task['increment_id'], 'INFO');
153 153
             $this->processIndividualTask($task);
154 154
         }
155 155
     }
@@ -160,7 +160,7 @@  discard block
 block discarded – undo
160 160
      * @param array $task The task to process.
161 161
      */
162 162
     private function processIndividualTask(array $task) {
163
-        if (LOG_TASKS=== true)  $this->logger->log('Starting task: ' . print_r($task, true), 'INFO');
163
+        if (LOG_TASKS === true)  $this->logger->log('Starting task: '.print_r($task, true), 'INFO');
164 164
 
165 165
         // Store progress in the database        
166 166
         DB::update(
@@ -177,18 +177,18 @@  discard block
 block discarded – undo
177 177
         // Prepare process
178 178
         $process = new Process([
179 179
             PHP_BINARY,
180
-            __DIR__ . '/background_tasks___worker.php',
180
+            __DIR__.'/background_tasks___worker.php',
181 181
             $task['increment_id'],
182 182
             $task['process_type'],
183 183
             $task['arguments']
184 184
         ]);
185 185
 
186 186
         // Launch process
187
-        try{
187
+        try {
188 188
             $process->mustRun();
189 189
 
190 190
         } catch (Exception $e) {
191
-            if (LOG_TASKS=== true) $this->logger->log('Error launching task: ' . $e->getMessage(), 'ERROR');
191
+            if (LOG_TASKS === true) $this->logger->log('Error launching task: '.$e->getMessage(), 'ERROR');
192 192
             DB::update(
193 193
                 prefixTable('background_tasks'),
194 194
                 [
@@ -209,7 +209,7 @@  discard block
 block discarded – undo
209 209
     private function countRunningTasks(): int {
210 210
         return DB::queryFirstField(
211 211
             'SELECT COUNT(*) 
212
-            FROM ' . prefixTable('background_tasks') . ' 
212
+            FROM ' . prefixTable('background_tasks').' 
213 213
             WHERE is_in_progress = 1'
214 214
         );
215 215
     }
@@ -230,10 +230,10 @@  discard block
 block discarded – undo
230 230
      */
231 231
     private function cleanMultipleItemsEdition() {
232 232
         DB::query(
233
-            'DELETE i1 FROM ' . prefixTable('items_edition') . ' i1
233
+            'DELETE i1 FROM '.prefixTable('items_edition').' i1
234 234
             JOIN (
235 235
                 SELECT user_id, item_id, MIN(timestamp) AS oldest_timestamp
236
-                FROM ' . prefixTable('items_edition') . '
236
+                FROM ' . prefixTable('items_edition').'
237 237
                 GROUP BY user_id, item_id
238 238
             ) i2 ON i1.user_id = i2.user_id AND i1.item_id = i2.item_id
239 239
             WHERE i1.timestamp > i2.oldest_timestamp'
@@ -246,7 +246,7 @@  discard block
 block discarded – undo
246 246
      */
247 247
     private function handleItemTokensExpiration() {
248 248
         DB::query(
249
-            'DELETE FROM ' . prefixTable('items_edition') . '
249
+            'DELETE FROM '.prefixTable('items_edition').'
250 250
             WHERE timestamp < %i',
251 251
             time() - ($this->settings['delay_item_edition'] * 60 ?: EDITION_LOCK_PERIOD)
252 252
         );
@@ -263,7 +263,7 @@  discard block
 block discarded – undo
263 263
         // 1. Get all finished tasks older than the cutoff timestamp
264 264
         //    and that are not in progress
265 265
         $tasks = DB::query(
266
-            'SELECT increment_id FROM ' . prefixTable('background_tasks') . '
266
+            'SELECT increment_id FROM '.prefixTable('background_tasks').'
267 267
             WHERE status = %s AND is_in_progress = %i AND finished_at < %s',
268 268
             'completed',
269 269
             -1,
@@ -278,19 +278,19 @@  discard block
 block discarded – undo
278 278
     
279 279
         // 2. Delete all subtasks related to these tasks
280 280
         DB::query(
281
-            'DELETE FROM ' . prefixTable('background_subtasks') . '
281
+            'DELETE FROM '.prefixTable('background_subtasks').'
282 282
             WHERE task_id IN %ls',
283 283
             $taskIds
284 284
         );
285 285
     
286 286
         // 3. Delete the tasks themselves
287 287
         DB::query(
288
-            'DELETE FROM ' . prefixTable('background_tasks') . '
288
+            'DELETE FROM '.prefixTable('background_tasks').'
289 289
             WHERE increment_id IN %ls',
290 290
             $taskIds
291 291
         );
292 292
     
293
-        if (LOG_TASKS=== true) $this->logger->log('Old finished tasks cleaned: ' . count($taskIds), 'INFO');
293
+        if (LOG_TASKS === true) $this->logger->log('Old finished tasks cleaned: '.count($taskIds), 'INFO');
294 294
     }
295 295
 }
296 296
 
@@ -304,5 +304,5 @@  discard block
 block discarded – undo
304 304
     $tasksHandler = new BackgroundTasksHandler($settings);
305 305
     $tasksHandler->processBackgroundTasks();
306 306
 } catch (Exception $e) {
307
-    error_log('Teampass Background Tasks Error: ' . $e->getMessage());
307
+    error_log('Teampass Background Tasks Error: '.$e->getMessage());
308 308
 }
309 309
\ No newline at end of file
Please login to merge, or discard this patch.
Braces   +21 added lines, -7 removed lines patch added patch discarded remove patch
@@ -55,7 +55,9 @@  discard block
 block discarded – undo
55 55
     public function processBackgroundTasks() {
56 56
         // Prevent multiple concurrent executions
57 57
         if (!$this->acquireProcessLock()) {
58
-            if (LOG_TASKS=== true) $this->logger->log('Process already running', 'INFO');
58
+            if (LOG_TASKS=== true) {
59
+                $this->logger->log('Process already running', 'INFO');
60
+            }
59 61
             return false;
60 62
         }
61 63
 
@@ -64,7 +66,9 @@  discard block
 block discarded – undo
64 66
             $this->processTaskBatches();
65 67
             $this->performMaintenanceTasks();
66 68
         } catch (Exception $e) {
67
-            if (LOG_TASKS=== true) $this->logger->log('Task processing error: ' . $e->getMessage(), 'ERROR');
69
+            if (LOG_TASKS=== true) {
70
+                $this->logger->log('Task processing error: ' . $e->getMessage(), 'ERROR');
71
+            }
68 72
         } finally {
69 73
             $this->releaseProcessLock();
70 74
         }
@@ -131,7 +135,9 @@  discard block
 block discarded – undo
131 135
         
132 136
         // Check if the maximum number of parallel tasks is reached
133 137
         if ($runningTasks >= $this->maxParallelTasks) {
134
-            if (LOG_TASKS=== true) $this->logger->log('Wait ... '.$runningTasks.' out of '.$this->maxParallelTasks.' are already running ', 'INFO');
138
+            if (LOG_TASKS=== true) {
139
+                $this->logger->log('Wait ... '.$runningTasks.' out of '.$this->maxParallelTasks.' are already running ', 'INFO');
140
+            }
135 141
             return;
136 142
         }
137 143
 
@@ -149,7 +155,9 @@  discard block
 block discarded – undo
149 155
         );
150 156
 
151 157
         foreach ($tasks as $task) {
152
-            if (LOG_TASKS=== true) $this->logger->log('Launching '.$task['increment_id'], 'INFO');
158
+            if (LOG_TASKS=== true) {
159
+                $this->logger->log('Launching '.$task['increment_id'], 'INFO');
160
+            }
153 161
             $this->processIndividualTask($task);
154 162
         }
155 163
     }
@@ -160,7 +168,9 @@  discard block
 block discarded – undo
160 168
      * @param array $task The task to process.
161 169
      */
162 170
     private function processIndividualTask(array $task) {
163
-        if (LOG_TASKS=== true)  $this->logger->log('Starting task: ' . print_r($task, true), 'INFO');
171
+        if (LOG_TASKS=== true) {
172
+            $this->logger->log('Starting task: ' . print_r($task, true), 'INFO');
173
+        }
164 174
 
165 175
         // Store progress in the database        
166 176
         DB::update(
@@ -188,7 +198,9 @@  discard block
 block discarded – undo
188 198
             $process->mustRun();
189 199
 
190 200
         } catch (Exception $e) {
191
-            if (LOG_TASKS=== true) $this->logger->log('Error launching task: ' . $e->getMessage(), 'ERROR');
201
+            if (LOG_TASKS=== true) {
202
+                $this->logger->log('Error launching task: ' . $e->getMessage(), 'ERROR');
203
+            }
192 204
             DB::update(
193 205
                 prefixTable('background_tasks'),
194 206
                 [
@@ -290,7 +302,9 @@  discard block
 block discarded – undo
290 302
             $taskIds
291 303
         );
292 304
     
293
-        if (LOG_TASKS=== true) $this->logger->log('Old finished tasks cleaned: ' . count($taskIds), 'INFO');
305
+        if (LOG_TASKS=== true) {
306
+            $this->logger->log('Old finished tasks cleaned: ' . count($taskIds), 'INFO');
307
+        }
294 308
     }
295 309
 }
296 310
 
Please login to merge, or discard this patch.