/**
  * This is not an *all* test since serialization of jobs is a per-JQStore choice...
  * @testdox JQWorker gracefully handles Exceptions thrown during unserialize()/__wakeup() of jobs by failing the job.
  */
 function testJqJobsCatchesUnserializeExceptions()
 {
     $mJob = $this->jqStore->enqueue(new SampleExceptionalUnserializerJob("custom data"));
     $this->assertNotNull($mJob->getJob(), "Verifying that job is legit...");
     $this->assertEquals("custom data", $mJob->getJob()->data, "Verifying job data...");
     $mJobArray = $mJob->toArray();
     $serializedJob = $mJobArray['job'];
     // Start a worker to run the jobs.
     $w = new JQWorker($this->jqStore, array('queueName' => 'test', 'exitIfNoJobs' => true, 'silent' => true, 'enableJitter' => false));
     $w->start();
     // have to re-fetch job since db state changed...
     $mJob = $this->jqStore->get($mJob->getJobId());
     // we only get here if the worker cleanly exited.
     $this->assertEquals(JQManagedJob::STATUS_FAILED, $mJob->getStatus());
     // ensure that the "job" is still as expected...
     $this->assertEquals($serializedJob, $mJob->getJob(), "Serialized JQJob data has been adulterated.");
     // ensure that the failed message says something about de-serialization failure?
     $this->assertEquals(JQManagedJob::STATUS_FAILED, $mJob->getStatus(), "Job should be marked as failed.");
     $this->assertEquals("JQManagedJob.job is not a JQJob instance.", $mJob->getErrorMessage(), "Unexpected job error message.");
 }
Exemple #2
0
 public static function handleShutdown()
 {
     $w = new JQWorker(self::$jqStore, array('exitIfNoJobs' => true, 'silent' => true));
     $w->start();
 }
Exemple #3
0
 /**
  * @testdox Test Basic JQJobs Processing using JQStore_Propel
  */
 function testJQJobs()
 {
     $q = $this->jqStore;
     $this->assertEquals(0, $q->count('test'));
     // Add jobs
     foreach (range(1, 10) as $i) {
         $q->enqueue(new QuietSimpleJob($i));
     }
     $this->assertEquals(10, $q->count());
     $this->assertEquals(10, $q->count('test'));
     $this->assertEquals(10, $q->count('test', JQManagedJob::STATUS_QUEUED));
     // Start a worker to run the jobs.
     $w = new JQWorker($q, array('queueName' => 'test', 'exitIfNoJobs' => true, 'silent' => true, 'enableJitter' => false));
     $w->start();
     $this->assertEquals(10, $w->jobsProcessed());
     $this->assertEquals(0, $q->count('test'));
 }
***********************************************************************************************************
  This test makes sure that a fatal error during job execution will result in the job being marked failed.
  This exercises our shutdown error detection.

  This test works if you see output indicating:
    - a job starts running
    - then a fatal error
    - finally "Status change: running => failed"
***********************************************************************************************************


EXPLAIN;
class SampleFatalJob extends SampleLoggingJob
{
    function run(JQManagedJob $mJob)
    {
        // causes a FATAL error
        $foo->bar();
    }
}
// run a job that will FATAL
$q = new JQStore_Array();
$goodJob = $q->enqueue(new SampleFatalJob());
if ($q->count('test') !== 1) {
    throw new Exception("assert failed");
}
SampleJobCounter::reset();
// Start a worker to run the job.
$w = new JQWorker($q, array('queueName' => 'test', 'exitIfNoJobs' => true, 'verbose' => true, 'enableJitter' => false));
$w->start();
Exemple #5
0
 function testJobsAutoRetryOnFailure()
 {
     // create a queuestore
     $maxAttempts = 5;
     $q = new JQStore_Array();
     $mJob = $q->enqueue(new SampleFailJob(array('maxAttempts' => $maxAttempts)));
     // Start a worker to run the jobs.
     $w = new JQWorker($q, array('queueName' => 'test', 'exitIfNoJobs' => true, 'silent' => true, 'enableJitter' => false));
     $w->start();
     $this->assertEquals(0, $q->count('test', 'queued'));
     $this->assertEquals(1, $q->count('test', 'failed'));
     $this->assertEquals($maxAttempts, $mJob->getAttemptNumber());
 }