| Conditions | 1 |
| Paths | 1 |
| Total Lines | 109 |
| Code Lines | 62 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 276 | public function testJobHealthCheck() |
||
| 277 | { |
||
| 278 | // Create a job and add it to the queue |
||
| 279 | $svc = $this->getService(); |
||
| 280 | $job = new TestQueuedJob(QueuedJob::IMMEDIATE); |
||
| 281 | $job->firstJob = true; |
||
| 282 | $id = $svc->queueJob($job); |
||
| 283 | $descriptor = QueuedJobDescriptor::get()->byID($id); |
||
| 284 | |||
| 285 | // Verify initial state is new and LastProcessedCount is not marked yet |
||
| 286 | $this->assertEquals(QueuedJob::STATUS_NEW, $descriptor->JobStatus); |
||
| 287 | $this->assertEquals(0, $descriptor->StepsProcessed); |
||
| 288 | $this->assertEquals(-1, $descriptor->LastProcessedCount); |
||
| 289 | $this->assertEquals(0, $descriptor->ResumeCounts); |
||
| 290 | |||
| 291 | // Loop 1 - Pick up new job and attempt to run it |
||
| 292 | // Job health should not attempt to cleanup unstarted jobs |
||
| 293 | $svc->checkJobHealth(QueuedJob::IMMEDIATE); |
||
| 294 | $nextJob = $svc->getNextPendingJob(QueuedJob::IMMEDIATE); |
||
| 295 | |||
| 296 | // Ensure that this is the next job ready to go |
||
| 297 | $descriptor = QueuedJobDescriptor::get()->byID($id); |
||
| 298 | $this->assertEquals($nextJob->ID, $descriptor->ID); |
||
| 299 | $this->assertEquals(QueuedJob::STATUS_NEW, $descriptor->JobStatus); |
||
| 300 | $this->assertEquals(0, $descriptor->StepsProcessed); |
||
| 301 | $this->assertEquals(-1, $descriptor->LastProcessedCount); |
||
| 302 | $this->assertEquals(0, $descriptor->ResumeCounts); |
||
| 303 | |||
| 304 | // Run 1 - Start the job (no work is done) |
||
| 305 | $descriptor->JobStatus = QueuedJob::STATUS_INIT; |
||
| 306 | $descriptor->write(); |
||
| 307 | |||
| 308 | // Assume that something bad happens at this point, the process dies during execution, and |
||
| 309 | // the task is re-initiated somewhere down the track |
||
| 310 | |||
| 311 | // Loop 2 - Detect broken job, and mark it for future checking. |
||
| 312 | $svc->checkJobHealth(QueuedJob::IMMEDIATE); |
||
| 313 | $nextJob = $svc->getNextPendingJob(QueuedJob::IMMEDIATE); |
||
| 314 | |||
| 315 | // Note that we don't immediately try to restart it until StepsProcessed = LastProcessedCount |
||
| 316 | $descriptor = QueuedJobDescriptor::get()->byID($id); |
||
| 317 | $this->assertFalse($nextJob); // Don't run it this round please! |
||
| 318 | $this->assertEquals(QueuedJob::STATUS_INIT, $descriptor->JobStatus); |
||
| 319 | $this->assertEquals(0, $descriptor->StepsProcessed); |
||
| 320 | $this->assertEquals(0, $descriptor->LastProcessedCount); |
||
| 321 | $this->assertEquals(0, $descriptor->ResumeCounts); |
||
| 322 | |||
| 323 | // Loop 3 - We've previously marked this job as broken, so restart it this round |
||
| 324 | // If no more work has been done on the job at this point, assume that we are able to |
||
| 325 | // restart it |
||
| 326 | $svc->checkJobHealth(QueuedJob::IMMEDIATE); |
||
| 327 | $nextJob = $svc->getNextPendingJob(QueuedJob::IMMEDIATE); |
||
| 328 | |||
| 329 | // This job is resumed and exeuction is attempted this round |
||
| 330 | $descriptor = QueuedJobDescriptor::get()->byID($id); |
||
| 331 | $this->assertEquals($nextJob->ID, $descriptor->ID); |
||
| 332 | $this->assertEquals(QueuedJob::STATUS_WAIT, $descriptor->JobStatus); |
||
| 333 | $this->assertEquals(0, $descriptor->StepsProcessed); |
||
| 334 | $this->assertEquals(0, $descriptor->LastProcessedCount); |
||
| 335 | $this->assertEquals(1, $descriptor->ResumeCounts); |
||
| 336 | |||
| 337 | // Run 2 - First restart (work is done) |
||
| 338 | $descriptor->JobStatus = QueuedJob::STATUS_RUN; |
||
| 339 | $descriptor->StepsProcessed++; // Essentially delays the next restart by 1 loop |
||
| 340 | $descriptor->write(); |
||
| 341 | |||
| 342 | // Once again, at this point, assume the job fails and crashes |
||
| 343 | |||
| 344 | // Loop 4 - Assuming a job has LastProcessedCount < StepsProcessed we are in the same |
||
| 345 | // situation as step 2. |
||
| 346 | // Because the last time the loop ran, StepsProcessed was incremented, |
||
| 347 | // this indicates that it's likely that another task could be working on this job, so |
||
| 348 | // don't run this. |
||
| 349 | $svc->checkJobHealth(QueuedJob::IMMEDIATE); |
||
| 350 | $nextJob = $svc->getNextPendingJob(QueuedJob::IMMEDIATE); |
||
| 351 | |||
| 352 | $descriptor = QueuedJobDescriptor::get()->byID($id); |
||
| 353 | $this->assertFalse($nextJob); // Don't run jobs we aren't sure should be restarted |
||
| 354 | $this->assertEquals(QueuedJob::STATUS_RUN, $descriptor->JobStatus); |
||
| 355 | $this->assertEquals(1, $descriptor->StepsProcessed); |
||
| 356 | $this->assertEquals(1, $descriptor->LastProcessedCount); |
||
| 357 | $this->assertEquals(1, $descriptor->ResumeCounts); |
||
| 358 | |||
| 359 | // Loop 5 - Job is again found to not have been restarted since last iteration, so perform second |
||
| 360 | // restart. The job should be attempted to run this loop |
||
| 361 | $svc->checkJobHealth(QueuedJob::IMMEDIATE); |
||
| 362 | $nextJob = $svc->getNextPendingJob(QueuedJob::IMMEDIATE); |
||
| 363 | |||
| 364 | // This job is resumed and exeuction is attempted this round |
||
| 365 | $descriptor = QueuedJobDescriptor::get()->byID($id); |
||
| 366 | $this->assertEquals($nextJob->ID, $descriptor->ID); |
||
| 367 | $this->assertEquals(QueuedJob::STATUS_WAIT, $descriptor->JobStatus); |
||
| 368 | $this->assertEquals(1, $descriptor->StepsProcessed); |
||
| 369 | $this->assertEquals(1, $descriptor->LastProcessedCount); |
||
| 370 | $this->assertEquals(2, $descriptor->ResumeCounts); |
||
| 371 | |||
| 372 | // Run 3 - Second and last restart (no work is done) |
||
| 373 | $descriptor->JobStatus = QueuedJob::STATUS_RUN; |
||
| 374 | $descriptor->write(); |
||
| 375 | |||
| 376 | // Loop 6 - As no progress has been made since loop 3, we can mark this as dead |
||
| 377 | $svc->checkJobHealth(QueuedJob::IMMEDIATE); |
||
| 378 | $nextJob = $svc->getNextPendingJob(QueuedJob::IMMEDIATE); |
||
| 379 | |||
| 380 | // Since no StepsProcessed has been done, don't wait another loop to mark this as dead |
||
| 381 | $descriptor = QueuedJobDescriptor::get()->byID($id); |
||
| 382 | $this->assertEquals(QueuedJob::STATUS_PAUSED, $descriptor->JobStatus); |
||
| 383 | $this->assertEmpty($nextJob); |
||
| 384 | } |
||
| 385 | } |
||
| 450 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.