Completed
Push — master ( a0c922...91a544 )
by
unknown
32:52 queued 10s
created
core/Command/Background/JobWorker.php 2 patches
Indentation   +154 added lines, -154 removed lines patch added patch discarded remove patch
@@ -20,158 +20,158 @@
 block discarded – undo
20 20
 
21 21
 class JobWorker extends JobBase {
22 22
 
23
-	public function __construct(
24
-		protected IJobList $jobList,
25
-		protected LoggerInterface $logger,
26
-		private ITempManager $tempManager,
27
-		private SetupManager $setupManager,
28
-	) {
29
-		parent::__construct($jobList, $logger);
30
-	}
31
-	protected function configure(): void {
32
-		parent::configure();
33
-
34
-		$this
35
-			->setName('background-job:worker')
36
-			->setDescription('Run a background job worker')
37
-			->addArgument(
38
-				'job-classes',
39
-				InputArgument::OPTIONAL | InputArgument::IS_ARRAY,
40
-				'The classes of the jobs to look for in the database'
41
-			)
42
-			->addOption(
43
-				'once',
44
-				null,
45
-				InputOption::VALUE_NONE,
46
-				'Only execute the worker once (as a regular cron execution would do it)'
47
-			)
48
-			->addOption(
49
-				'interval',
50
-				'i',
51
-				InputOption::VALUE_OPTIONAL,
52
-				'Interval in seconds in which the worker should repeat already processed jobs (set to 0 for no repeat)',
53
-				1
54
-			)
55
-			->addOption(
56
-				'stop_after',
57
-				't',
58
-				InputOption::VALUE_OPTIONAL,
59
-				'Duration after which the worker should stop and exit. The worker won\'t kill a potential running job, it will exit after this job has finished running (supported values are: "30" or "30s" for 30 seconds, "10m" for 10 minutes and "2h" for 2 hours)'
60
-			)
61
-		;
62
-	}
63
-
64
-	protected function execute(InputInterface $input, OutputInterface $output): int {
65
-		$startTime = time();
66
-		$stopAfterOptionValue = $input->getOption('stop_after');
67
-		$stopAfterSeconds = $stopAfterOptionValue === null
68
-			? null
69
-			: $this->parseStopAfter($stopAfterOptionValue);
70
-		if ($stopAfterSeconds !== null) {
71
-			$output->writeln('<info>Background job worker will stop after ' . $stopAfterSeconds . ' seconds</info>');
72
-		}
73
-
74
-		$jobClasses = $input->getArgument('job-classes');
75
-		$jobClasses = empty($jobClasses) ? null : $jobClasses;
76
-
77
-		if ($jobClasses !== null) {
78
-			// at least one class is invalid
79
-			foreach ($jobClasses as $jobClass) {
80
-				if (!class_exists($jobClass)) {
81
-					$output->writeln('<error>Invalid job class: ' . $jobClass . '</error>');
82
-					return 1;
83
-				}
84
-			}
85
-		}
86
-
87
-		while (true) {
88
-			// Stop if we exceeded stop_after value
89
-			if ($stopAfterSeconds !== null && ($startTime + $stopAfterSeconds) < time()) {
90
-				$output->writeln('stop_after time has been exceeded, exiting...', OutputInterface::VERBOSITY_VERBOSE);
91
-				break;
92
-			}
93
-			// Handle canceling of the process
94
-			try {
95
-				$this->abortIfInterrupted();
96
-			} catch (InterruptedException $e) {
97
-				$output->writeln('<info>Background job worker stopped</info>');
98
-				break;
99
-			}
100
-
101
-			$this->printSummary($input, $output);
102
-
103
-			usleep(50000);
104
-			$job = $this->jobList->getNext(false, $jobClasses);
105
-			if (!$job) {
106
-				if ($input->getOption('once') === true) {
107
-					if ($jobClasses === null) {
108
-						$output->writeln('No job is currently queued', OutputInterface::VERBOSITY_VERBOSE);
109
-					} else {
110
-						$output->writeln('No job of classes [' . implode(', ', $jobClasses) . '] is currently queued', OutputInterface::VERBOSITY_VERBOSE);
111
-					}
112
-					$output->writeln('Exiting...', OutputInterface::VERBOSITY_VERBOSE);
113
-					break;
114
-				}
115
-
116
-				$output->writeln('Waiting for new jobs to be queued', OutputInterface::VERBOSITY_VERBOSE);
117
-				if ((int)$input->getOption('interval') === 0) {
118
-					break;
119
-				}
120
-				// Re-check interval for new jobs
121
-				sleep((int)$input->getOption('interval'));
122
-				continue;
123
-			}
124
-
125
-			$output->writeln('Running job ' . get_class($job) . ' with ID ' . $job->getId());
126
-
127
-			if ($output->isVerbose()) {
128
-				$this->printJobInfo($job->getId(), $job, $output);
129
-			}
130
-
131
-			$job->start($this->jobList);
132
-			$output->writeln('Job ' . $job->getId() . ' has finished', OutputInterface::VERBOSITY_VERBOSE);
133
-
134
-			// clean up after unclean jobs
135
-			$this->setupManager->tearDown();
136
-			$this->tempManager->clean();
137
-
138
-			$this->jobList->setLastJob($job);
139
-			$this->jobList->unlockJob($job);
140
-
141
-			if ($input->getOption('once') === true) {
142
-				break;
143
-			}
144
-		}
145
-
146
-		return 0;
147
-	}
148
-
149
-	private function printSummary(InputInterface $input, OutputInterface $output): void {
150
-		if (!$output->isVeryVerbose()) {
151
-			return;
152
-		}
153
-		$output->writeln('<comment>Summary</comment>');
154
-
155
-		$counts = [];
156
-		foreach ($this->jobList->countByClass() as $row) {
157
-			$counts[] = $row;
158
-		}
159
-		$this->writeTableInOutputFormat($input, $output, $counts);
160
-	}
161
-
162
-	private function parseStopAfter(string $value): ?int {
163
-		if (is_numeric($value)) {
164
-			return (int)$value;
165
-		}
166
-		if (preg_match("/^(\d+)s$/i", $value, $matches)) {
167
-			return (int)$matches[0];
168
-		}
169
-		if (preg_match("/^(\d+)m$/i", $value, $matches)) {
170
-			return 60 * ((int)$matches[0]);
171
-		}
172
-		if (preg_match("/^(\d+)h$/i", $value, $matches)) {
173
-			return 60 * 60 * ((int)$matches[0]);
174
-		}
175
-		return null;
176
-	}
23
+    public function __construct(
24
+        protected IJobList $jobList,
25
+        protected LoggerInterface $logger,
26
+        private ITempManager $tempManager,
27
+        private SetupManager $setupManager,
28
+    ) {
29
+        parent::__construct($jobList, $logger);
30
+    }
31
+    protected function configure(): void {
32
+        parent::configure();
33
+
34
+        $this
35
+            ->setName('background-job:worker')
36
+            ->setDescription('Run a background job worker')
37
+            ->addArgument(
38
+                'job-classes',
39
+                InputArgument::OPTIONAL | InputArgument::IS_ARRAY,
40
+                'The classes of the jobs to look for in the database'
41
+            )
42
+            ->addOption(
43
+                'once',
44
+                null,
45
+                InputOption::VALUE_NONE,
46
+                'Only execute the worker once (as a regular cron execution would do it)'
47
+            )
48
+            ->addOption(
49
+                'interval',
50
+                'i',
51
+                InputOption::VALUE_OPTIONAL,
52
+                'Interval in seconds in which the worker should repeat already processed jobs (set to 0 for no repeat)',
53
+                1
54
+            )
55
+            ->addOption(
56
+                'stop_after',
57
+                't',
58
+                InputOption::VALUE_OPTIONAL,
59
+                'Duration after which the worker should stop and exit. The worker won\'t kill a potential running job, it will exit after this job has finished running (supported values are: "30" or "30s" for 30 seconds, "10m" for 10 minutes and "2h" for 2 hours)'
60
+            )
61
+        ;
62
+    }
63
+
64
+    protected function execute(InputInterface $input, OutputInterface $output): int {
65
+        $startTime = time();
66
+        $stopAfterOptionValue = $input->getOption('stop_after');
67
+        $stopAfterSeconds = $stopAfterOptionValue === null
68
+            ? null
69
+            : $this->parseStopAfter($stopAfterOptionValue);
70
+        if ($stopAfterSeconds !== null) {
71
+            $output->writeln('<info>Background job worker will stop after ' . $stopAfterSeconds . ' seconds</info>');
72
+        }
73
+
74
+        $jobClasses = $input->getArgument('job-classes');
75
+        $jobClasses = empty($jobClasses) ? null : $jobClasses;
76
+
77
+        if ($jobClasses !== null) {
78
+            // at least one class is invalid
79
+            foreach ($jobClasses as $jobClass) {
80
+                if (!class_exists($jobClass)) {
81
+                    $output->writeln('<error>Invalid job class: ' . $jobClass . '</error>');
82
+                    return 1;
83
+                }
84
+            }
85
+        }
86
+
87
+        while (true) {
88
+            // Stop if we exceeded stop_after value
89
+            if ($stopAfterSeconds !== null && ($startTime + $stopAfterSeconds) < time()) {
90
+                $output->writeln('stop_after time has been exceeded, exiting...', OutputInterface::VERBOSITY_VERBOSE);
91
+                break;
92
+            }
93
+            // Handle canceling of the process
94
+            try {
95
+                $this->abortIfInterrupted();
96
+            } catch (InterruptedException $e) {
97
+                $output->writeln('<info>Background job worker stopped</info>');
98
+                break;
99
+            }
100
+
101
+            $this->printSummary($input, $output);
102
+
103
+            usleep(50000);
104
+            $job = $this->jobList->getNext(false, $jobClasses);
105
+            if (!$job) {
106
+                if ($input->getOption('once') === true) {
107
+                    if ($jobClasses === null) {
108
+                        $output->writeln('No job is currently queued', OutputInterface::VERBOSITY_VERBOSE);
109
+                    } else {
110
+                        $output->writeln('No job of classes [' . implode(', ', $jobClasses) . '] is currently queued', OutputInterface::VERBOSITY_VERBOSE);
111
+                    }
112
+                    $output->writeln('Exiting...', OutputInterface::VERBOSITY_VERBOSE);
113
+                    break;
114
+                }
115
+
116
+                $output->writeln('Waiting for new jobs to be queued', OutputInterface::VERBOSITY_VERBOSE);
117
+                if ((int)$input->getOption('interval') === 0) {
118
+                    break;
119
+                }
120
+                // Re-check interval for new jobs
121
+                sleep((int)$input->getOption('interval'));
122
+                continue;
123
+            }
124
+
125
+            $output->writeln('Running job ' . get_class($job) . ' with ID ' . $job->getId());
126
+
127
+            if ($output->isVerbose()) {
128
+                $this->printJobInfo($job->getId(), $job, $output);
129
+            }
130
+
131
+            $job->start($this->jobList);
132
+            $output->writeln('Job ' . $job->getId() . ' has finished', OutputInterface::VERBOSITY_VERBOSE);
133
+
134
+            // clean up after unclean jobs
135
+            $this->setupManager->tearDown();
136
+            $this->tempManager->clean();
137
+
138
+            $this->jobList->setLastJob($job);
139
+            $this->jobList->unlockJob($job);
140
+
141
+            if ($input->getOption('once') === true) {
142
+                break;
143
+            }
144
+        }
145
+
146
+        return 0;
147
+    }
148
+
149
+    private function printSummary(InputInterface $input, OutputInterface $output): void {
150
+        if (!$output->isVeryVerbose()) {
151
+            return;
152
+        }
153
+        $output->writeln('<comment>Summary</comment>');
154
+
155
+        $counts = [];
156
+        foreach ($this->jobList->countByClass() as $row) {
157
+            $counts[] = $row;
158
+        }
159
+        $this->writeTableInOutputFormat($input, $output, $counts);
160
+    }
161
+
162
+    private function parseStopAfter(string $value): ?int {
163
+        if (is_numeric($value)) {
164
+            return (int)$value;
165
+        }
166
+        if (preg_match("/^(\d+)s$/i", $value, $matches)) {
167
+            return (int)$matches[0];
168
+        }
169
+        if (preg_match("/^(\d+)m$/i", $value, $matches)) {
170
+            return 60 * ((int)$matches[0]);
171
+        }
172
+        if (preg_match("/^(\d+)h$/i", $value, $matches)) {
173
+            return 60 * 60 * ((int)$matches[0]);
174
+        }
175
+        return null;
176
+    }
177 177
 }
Please login to merge, or discard this patch.
Spacing   +11 added lines, -11 removed lines patch added patch discarded remove patch
@@ -68,7 +68,7 @@  discard block
 block discarded – undo
68 68
 			? null
69 69
 			: $this->parseStopAfter($stopAfterOptionValue);
70 70
 		if ($stopAfterSeconds !== null) {
71
-			$output->writeln('<info>Background job worker will stop after ' . $stopAfterSeconds . ' seconds</info>');
71
+			$output->writeln('<info>Background job worker will stop after '.$stopAfterSeconds.' seconds</info>');
72 72
 		}
73 73
 
74 74
 		$jobClasses = $input->getArgument('job-classes');
@@ -78,7 +78,7 @@  discard block
 block discarded – undo
78 78
 			// at least one class is invalid
79 79
 			foreach ($jobClasses as $jobClass) {
80 80
 				if (!class_exists($jobClass)) {
81
-					$output->writeln('<error>Invalid job class: ' . $jobClass . '</error>');
81
+					$output->writeln('<error>Invalid job class: '.$jobClass.'</error>');
82 82
 					return 1;
83 83
 				}
84 84
 			}
@@ -107,29 +107,29 @@  discard block
 block discarded – undo
107 107
 					if ($jobClasses === null) {
108 108
 						$output->writeln('No job is currently queued', OutputInterface::VERBOSITY_VERBOSE);
109 109
 					} else {
110
-						$output->writeln('No job of classes [' . implode(', ', $jobClasses) . '] is currently queued', OutputInterface::VERBOSITY_VERBOSE);
110
+						$output->writeln('No job of classes ['.implode(', ', $jobClasses).'] is currently queued', OutputInterface::VERBOSITY_VERBOSE);
111 111
 					}
112 112
 					$output->writeln('Exiting...', OutputInterface::VERBOSITY_VERBOSE);
113 113
 					break;
114 114
 				}
115 115
 
116 116
 				$output->writeln('Waiting for new jobs to be queued', OutputInterface::VERBOSITY_VERBOSE);
117
-				if ((int)$input->getOption('interval') === 0) {
117
+				if ((int) $input->getOption('interval') === 0) {
118 118
 					break;
119 119
 				}
120 120
 				// Re-check interval for new jobs
121
-				sleep((int)$input->getOption('interval'));
121
+				sleep((int) $input->getOption('interval'));
122 122
 				continue;
123 123
 			}
124 124
 
125
-			$output->writeln('Running job ' . get_class($job) . ' with ID ' . $job->getId());
125
+			$output->writeln('Running job '.get_class($job).' with ID '.$job->getId());
126 126
 
127 127
 			if ($output->isVerbose()) {
128 128
 				$this->printJobInfo($job->getId(), $job, $output);
129 129
 			}
130 130
 
131 131
 			$job->start($this->jobList);
132
-			$output->writeln('Job ' . $job->getId() . ' has finished', OutputInterface::VERBOSITY_VERBOSE);
132
+			$output->writeln('Job '.$job->getId().' has finished', OutputInterface::VERBOSITY_VERBOSE);
133 133
 
134 134
 			// clean up after unclean jobs
135 135
 			$this->setupManager->tearDown();
@@ -161,16 +161,16 @@  discard block
 block discarded – undo
161 161
 
162 162
 	private function parseStopAfter(string $value): ?int {
163 163
 		if (is_numeric($value)) {
164
-			return (int)$value;
164
+			return (int) $value;
165 165
 		}
166 166
 		if (preg_match("/^(\d+)s$/i", $value, $matches)) {
167
-			return (int)$matches[0];
167
+			return (int) $matches[0];
168 168
 		}
169 169
 		if (preg_match("/^(\d+)m$/i", $value, $matches)) {
170
-			return 60 * ((int)$matches[0]);
170
+			return 60 * ((int) $matches[0]);
171 171
 		}
172 172
 		if (preg_match("/^(\d+)h$/i", $value, $matches)) {
173
-			return 60 * 60 * ((int)$matches[0]);
173
+			return 60 * 60 * ((int) $matches[0]);
174 174
 		}
175 175
 		return null;
176 176
 	}
Please login to merge, or discard this patch.