Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like QueuedJobService often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use QueuedJobService, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 50 | class QueuedJobService |
||
| 51 | { |
||
| 52 | use Configurable; |
||
| 53 | use Injectable; |
||
| 54 | use Extensible; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @config |
||
| 58 | * @var int |
||
| 59 | */ |
||
| 60 | private static $stall_threshold = 3; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * How much ram will we allow before pausing and releasing the memory? |
||
| 64 | * |
||
| 65 | * For instance, set to 268435456 (256MB) to pause this process if used memory exceeds |
||
| 66 | * this value. This needs to be set to a value lower than the php_ini max_memory as |
||
| 67 | * the system will otherwise crash before shutdown can be handled gracefully. |
||
| 68 | * |
||
| 69 | * This was increased to 256MB for SilverStripe 4.x as framework uses more memory than 3.x |
||
| 70 | * |
||
| 71 | * @var int |
||
| 72 | * @config |
||
| 73 | */ |
||
| 74 | private static $memory_limit = 268435456; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Optional time limit (in seconds) to run the service before restarting to release resources. |
||
| 78 | * |
||
| 79 | * Defaults to no limit. |
||
| 80 | * |
||
| 81 | * @var int |
||
| 82 | * @config |
||
| 83 | */ |
||
| 84 | private static $time_limit = 0; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Disable health checks that usually occur when a runner first picks up a queue. Note that completely disabling |
||
| 88 | * health checks could result in many jobs that are always marked as running - that will never be restarted. If |
||
| 89 | * this option is disabled you may alternatively use the build task |
||
| 90 | * |
||
| 91 | * @see \Symbiote\QueuedJobs\Tasks\CheckJobHealthTask |
||
| 92 | * |
||
| 93 | * @var bool |
||
| 94 | * @config |
||
| 95 | */ |
||
| 96 | private static $disable_health_check = false; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Maximum number of jobs that can be initialised at any one time. |
||
| 100 | * |
||
| 101 | * Prevents too many jobs getting into this state in case something goes wrong with the child processes. |
||
| 102 | * We shouldn't have too many jobs in the initialising state anyway. |
||
| 103 | * |
||
| 104 | * Valid values: |
||
| 105 | * 0 - unlimited (default) |
||
| 106 | * greater than 0 - maximum number of jobs in initialised state |
||
| 107 | * |
||
| 108 | * @var int |
||
| 109 | * @config |
||
| 110 | */ |
||
| 111 | private static $max_init_jobs = 0; |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Timestamp (in seconds) when the queue was started |
||
| 115 | * |
||
| 116 | * @var int |
||
| 117 | */ |
||
| 118 | protected $startedAt = 0; |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Should "immediate" jobs be managed using the shutdown function? |
||
| 122 | * |
||
| 123 | * It is recommended you set up an inotify watch and use that for |
||
| 124 | * triggering immediate jobs. See the wiki for more information |
||
| 125 | * |
||
| 126 | * @var boolean |
||
| 127 | * @config |
||
| 128 | */ |
||
| 129 | private static $use_shutdown_function = true; |
||
| 130 | |||
| 131 | /** |
||
| 132 | * The location for immediate jobs to be stored in |
||
| 133 | * |
||
| 134 | * @var string |
||
| 135 | * @config |
||
| 136 | */ |
||
| 137 | private static $cache_dir = 'queuedjobs'; |
||
| 138 | |||
| 139 | /** |
||
| 140 | * @var DefaultQueueHandler |
||
| 141 | */ |
||
| 142 | public $queueHandler; |
||
| 143 | |||
| 144 | /** |
||
| 145 | * |
||
| 146 | * @var TaskRunnerEngine |
||
| 147 | */ |
||
| 148 | public $queueRunner; |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Config controlled list of default/required jobs |
||
| 152 | * |
||
| 153 | * @var array |
||
| 154 | */ |
||
| 155 | public $defaultJobs = []; |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Register our shutdown handler |
||
| 159 | */ |
||
| 160 | public function __construct() |
||
| 161 | { |
||
| 162 | // bind a shutdown function to process all 'immediate' queued jobs if needed, but only in CLI mode |
||
| 163 | if (static::config()->get('use_shutdown_function') && Director::is_cli()) { |
||
| 164 | register_shutdown_function([$this, 'onShutdown']); |
||
| 165 | } |
||
| 166 | if (Config::inst()->get(Email::class, 'queued_job_admin_email') == '') { |
||
| 167 | Config::modify()->set( |
||
| 168 | Email::class, |
||
| 169 | 'queued_job_admin_email', |
||
| 170 | Config::inst()->get(Email::class, 'admin_email') |
||
| 171 | ); |
||
| 172 | } |
||
| 173 | } |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Adds a job to the queue to be started |
||
| 177 | * |
||
| 178 | * @param QueuedJob $job The job to start. |
||
| 179 | * @param null|string $startAfter The date (in Y-m-d H:i:s format) to start execution after |
||
| 180 | * @param null|int $userId The ID of a user to execute the job as. Defaults to the current user |
||
| 181 | * @param null|int $queueName |
||
| 182 | * @param array $options options which are passed to the updateJobDescriptorBeforeQueued extension point |
||
| 183 | * @return int |
||
| 184 | * @throws ValidationException |
||
| 185 | */ |
||
| 186 | public function queueJob(QueuedJob $job, $startAfter = null, $userId = null, $queueName = null, array $options = []) |
||
| 187 | { |
||
| 188 | $signature = $job->getSignature(); |
||
| 189 | |||
| 190 | // see if we already have this job in a queue |
||
| 191 | $filter = [ |
||
| 192 | 'Signature' => $signature, |
||
| 193 | 'JobStatus' => [ |
||
| 194 | QueuedJob::STATUS_NEW, |
||
| 195 | QueuedJob::STATUS_INIT, |
||
| 196 | ], |
||
| 197 | ]; |
||
| 198 | |||
| 199 | $existing = QueuedJobDescriptor::get() |
||
| 200 | ->filter($filter) |
||
| 201 | ->first(); |
||
| 202 | |||
| 203 | if ($existing && $existing->ID) { |
||
| 204 | return $existing->ID; |
||
| 205 | } |
||
| 206 | |||
| 207 | $jobDescriptor = new QueuedJobDescriptor(); |
||
| 208 | $jobDescriptor->JobTitle = $job->getTitle(); |
||
| 209 | $jobDescriptor->JobType = $queueName ? $queueName : $job->getJobType(); |
||
| 210 | $jobDescriptor->Signature = $signature; |
||
| 211 | $jobDescriptor->Implementation = get_class($job); |
||
| 212 | $jobDescriptor->StartAfter = $startAfter; |
||
| 213 | |||
| 214 | // no user provided - fallback to job user default |
||
| 215 | if ($userId === null) { |
||
| 216 | $userId = $job->getRunAsMember(); |
||
| 217 | } |
||
| 218 | |||
| 219 | // still no user - fallback to current user |
||
| 220 | if ($userId === null) { |
||
| 221 | if (Security::getCurrentUser() && Security::getCurrentUser()->exists()) { |
||
| 222 | // current user available |
||
| 223 | $runAsID = Security::getCurrentUser()->ID; |
||
| 224 | } else { |
||
| 225 | // current user unavailable |
||
| 226 | $runAsID = 0; |
||
| 227 | } |
||
| 228 | } else { |
||
| 229 | $runAsID = $userId; |
||
| 230 | } |
||
| 231 | |||
| 232 | $jobDescriptor->RunAsID = $runAsID; |
||
|
|
|||
| 233 | |||
| 234 | // copy data |
||
| 235 | $this->copyJobToDescriptor($job, $jobDescriptor); |
||
| 236 | |||
| 237 | // use this to populate custom data columns before job is queued |
||
| 238 | $this->extend('updateJobDescriptorBeforeQueued', $jobDescriptor, $job, $options); |
||
| 239 | |||
| 240 | $jobDescriptor->write(); |
||
| 241 | |||
| 242 | $this->startJob($jobDescriptor, $startAfter); |
||
| 243 | |||
| 244 | return $jobDescriptor->ID; |
||
| 245 | } |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Start a job (or however the queue handler determines it should be started) |
||
| 249 | * |
||
| 250 | * @param QueuedJobDescriptor $jobDescriptor |
||
| 251 | * @param string $startAfter |
||
| 252 | */ |
||
| 253 | public function startJob($jobDescriptor, $startAfter = null) |
||
| 254 | { |
||
| 255 | if ($startAfter && strtotime($startAfter) > DBDatetime::now()->getTimestamp()) { |
||
| 256 | $this->queueHandler->scheduleJob($jobDescriptor, $startAfter); |
||
| 257 | } else { |
||
| 258 | // immediately start it on the queue, however that works |
||
| 259 | $this->queueHandler->startJobOnQueue($jobDescriptor); |
||
| 260 | } |
||
| 261 | } |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Check if maximum number of jobs are currently initialised. |
||
| 265 | * |
||
| 266 | * @return bool |
||
| 267 | */ |
||
| 268 | public function isAtMaxJobs() |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Copies data from a job into a descriptor for persisting |
||
| 288 | * |
||
| 289 | * @param QueuedJob $job |
||
| 290 | * @param QueuedJobDescriptor $jobDescriptor |
||
| 291 | */ |
||
| 292 | protected function copyJobToDescriptor($job, $jobDescriptor) |
||
| 306 | |||
| 307 | /** |
||
| 308 | * @param QueuedJobDescriptor $jobDescriptor |
||
| 309 | * @param QueuedJob $job |
||
| 310 | */ |
||
| 311 | protected function copyDescriptorToJob($jobDescriptor, $job) |
||
| 332 | |||
| 333 | /** |
||
| 334 | * Check the current job queues and see if any of the jobs currently in there should be started. If so, |
||
| 335 | * return the next job that should be executed |
||
| 336 | * |
||
| 337 | * @param string $type Job type |
||
| 338 | * |
||
| 339 | * @return QueuedJobDescriptor|false |
||
| 340 | */ |
||
| 341 | public function getNextPendingJob($type = null) |
||
| 379 | |||
| 380 | /** |
||
| 381 | * Runs an explicit check on all currently running jobs to make sure their "processed" count is incrementing |
||
| 382 | * between each run. If it's not, then we need to flag it as paused due to an error. |
||
| 383 | * |
||
| 384 | * This typically happens when a PHP fatal error is thrown, which can't be picked up by the error |
||
| 385 | * handler or exception checker; in this case, we detect these stalled jobs later and fix (try) to |
||
| 386 | * fix them |
||
| 387 | * |
||
| 388 | * @param int $queue The queue to check against |
||
| 389 | */ |
||
| 390 | public function checkJobHealth($queue = null) |
||
| 442 | |||
| 443 | /** |
||
| 444 | * Checks through ll the scheduled jobs that are expected to exist |
||
| 445 | */ |
||
| 446 | public function checkDefaultJobs($queue = null) |
||
| 526 | |||
| 527 | /** |
||
| 528 | * Attempt to restart a stalled job |
||
| 529 | * |
||
| 530 | * @param QueuedJobDescriptor $stalledJob |
||
| 531 | * |
||
| 532 | * @return bool True if the job was successfully restarted |
||
| 533 | */ |
||
| 534 | protected function restartStalledJob($stalledJob) |
||
| 578 | |||
| 579 | /** |
||
| 580 | * Prepares the given jobDescriptor for execution. Returns the job that |
||
| 581 | * will actually be run in a state ready for executing. |
||
| 582 | * |
||
| 583 | * Note that this is called each time a job is picked up to be executed from the cron |
||
| 584 | * job - meaning that jobs that are paused and restarted will have 'setup()' called on them again, |
||
| 585 | * so your job MUST detect that and act accordingly. |
||
| 586 | * |
||
| 587 | * @param QueuedJobDescriptor $jobDescriptor |
||
| 588 | * The Job descriptor of a job to prepare for execution |
||
| 589 | * |
||
| 590 | * @return QueuedJob|boolean |
||
| 591 | * @throws Exception |
||
| 592 | */ |
||
| 593 | protected function initialiseJob(QueuedJobDescriptor $jobDescriptor) |
||
| 623 | |||
| 624 | /** |
||
| 625 | * Given a {@link QueuedJobDescriptor} mark the job as initialised. Works sort of like a mutex. |
||
| 626 | * Currently a database lock isn't entirely achievable, due to database adapters not supporting locks. |
||
| 627 | * This may still have a race condition, but this should minimise the possibility. |
||
| 628 | * Side effect is the job status will be changed to "Initialised". |
||
| 629 | * |
||
| 630 | * Assumption is the job has a status of "Queued" or "Wait". |
||
| 631 | * |
||
| 632 | * @param QueuedJobDescriptor $jobDescriptor |
||
| 633 | * |
||
| 634 | * @return boolean |
||
| 635 | */ |
||
| 636 | protected function grabMutex(QueuedJobDescriptor $jobDescriptor) |
||
| 658 | |||
| 659 | /** |
||
| 660 | * Start the actual execution of a job. |
||
| 661 | * The assumption is the jobID refers to a {@link QueuedJobDescriptor} that is status set as "Queued". |
||
| 662 | * |
||
| 663 | * This method will continue executing until the job says it's completed |
||
| 664 | * |
||
| 665 | * @param int $jobId |
||
| 666 | * The ID of the job to start executing |
||
| 667 | * |
||
| 668 | * @return boolean |
||
| 669 | * @throws Exception |
||
| 670 | */ |
||
| 671 | public function runJob($jobId) |
||
| 968 | |||
| 969 | /** |
||
| 970 | * @param QueuedJobDescriptor $jobDescriptor |
||
| 971 | * @param QueuedJob $job |
||
| 972 | * @param Exception|\Throwable $e |
||
| 973 | * @throws ValidationException |
||
| 974 | */ |
||
| 975 | protected function handleBrokenJobException(QueuedJobDescriptor $jobDescriptor, QueuedJob $job, $e) |
||
| 988 | |||
| 989 | /** |
||
| 990 | * @param Member $runAsUser |
||
| 991 | * @param Member|null $originalUser |
||
| 992 | * @return null|Member |
||
| 993 | */ |
||
| 994 | protected function setRunAsUser(Member $runAsUser, Member $originalUser = null) |
||
| 1022 | |||
| 1023 | /** |
||
| 1024 | * @param Member|null $runAsUser |
||
| 1025 | * @param Member|null $originalUser |
||
| 1026 | */ |
||
| 1027 | protected function unsetRunAsUser(Member $runAsUser = null, Member $originalUser = null) |
||
| 1052 | |||
| 1053 | /** |
||
| 1054 | * Start timer |
||
| 1055 | */ |
||
| 1056 | protected function markStarted() |
||
| 1062 | |||
| 1063 | /** |
||
| 1064 | * Is execution time too long? |
||
| 1065 | * |
||
| 1066 | * @return bool True if the script has passed the configured time_limit |
||
| 1067 | */ |
||
| 1068 | protected function hasPassedTimeLimit() |
||
| 1083 | |||
| 1084 | /** |
||
| 1085 | * Is memory usage too high? |
||
| 1086 | * |
||
| 1087 | * @return bool |
||
| 1088 | */ |
||
| 1089 | protected function isMemoryTooHigh() |
||
| 1095 | |||
| 1096 | /** |
||
| 1097 | * Get peak memory usage of this application |
||
| 1098 | * |
||
| 1099 | * @return float |
||
| 1100 | */ |
||
| 1101 | protected function getMemoryUsage() |
||
| 1108 | |||
| 1109 | /** |
||
| 1110 | * Determines the memory limit (in bytes) for this application |
||
| 1111 | * Limits to the smaller of memory_limit configured via php.ini or silverstripe config |
||
| 1112 | * |
||
| 1113 | * @return float Memory limit in bytes |
||
| 1114 | */ |
||
| 1115 | protected function getMemoryLimit() |
||
| 1129 | |||
| 1130 | /** |
||
| 1131 | * Calculate the current memory limit of the server |
||
| 1132 | * |
||
| 1133 | * @return float |
||
| 1134 | */ |
||
| 1135 | protected function getPHPMemoryLimit() |
||
| 1139 | |||
| 1140 | /** |
||
| 1141 | * Convert memory limit string to bytes. |
||
| 1142 | * Based on implementation in install.php5 |
||
| 1143 | * |
||
| 1144 | * @param string $memString |
||
| 1145 | * |
||
| 1146 | * @return float |
||
| 1147 | */ |
||
| 1148 | protected function parseMemory($memString) |
||
| 1163 | |||
| 1164 | protected function humanReadable($size) |
||
| 1169 | |||
| 1170 | |||
| 1171 | /** |
||
| 1172 | * Gets a list of all the current jobs (or jobs that have recently finished) |
||
| 1173 | * |
||
| 1174 | * @param string $type |
||
| 1175 | * if we're after a particular job list |
||
| 1176 | * @param int $includeUpUntil |
||
| 1177 | * The number of seconds to include jobs that have just finished, allowing a job list to be built that |
||
| 1178 | * includes recently finished jobs |
||
| 1179 | * |
||
| 1180 | * @return DataList|QueuedJobDescriptor[] |
||
| 1181 | */ |
||
| 1182 | public function getJobList($type = null, $includeUpUntil = 0) |
||
| 1189 | |||
| 1190 | /** |
||
| 1191 | * Return the SQL filter used to get the job list - this is used by the UI for displaying the job list... |
||
| 1192 | * |
||
| 1193 | * @param string $type |
||
| 1194 | * if we're after a particular job list |
||
| 1195 | * @param int $includeUpUntil |
||
| 1196 | * The number of seconds to include jobs that have just finished, allowing a job list to be built that |
||
| 1197 | * includes recently finished jobs |
||
| 1198 | * |
||
| 1199 | * @return string |
||
| 1200 | */ |
||
| 1201 | public function getJobListFilter($type = null, $includeUpUntil = 0) |
||
| 1220 | |||
| 1221 | /** |
||
| 1222 | * Process the job queue with the current queue runner |
||
| 1223 | * |
||
| 1224 | * @param string $queue |
||
| 1225 | */ |
||
| 1226 | public function runQueue($queue) |
||
| 1234 | |||
| 1235 | /** |
||
| 1236 | * Process all jobs from a given queue |
||
| 1237 | * |
||
| 1238 | * @param string $name The job queue to completely process |
||
| 1239 | */ |
||
| 1240 | public function processJobQueue($name) |
||
| 1265 | |||
| 1266 | /** |
||
| 1267 | * When PHP shuts down, we want to process all of the immediate queue items |
||
| 1268 | * |
||
| 1269 | * We use the 'getNextPendingJob' method, instead of just iterating the queue, to ensure |
||
| 1270 | * we ignore paused or stalled jobs. |
||
| 1271 | */ |
||
| 1272 | public function onShutdown() |
||
| 1276 | |||
| 1277 | /** |
||
| 1278 | * Get a logger |
||
| 1279 | * |
||
| 1280 | * @return LoggerInterface |
||
| 1281 | */ |
||
| 1282 | public function getLogger() |
||
| 1286 | } |
||
| 1287 |
Since your code implements the magic setter
_set, this function will be called for any write access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.