示例#1
0
 /**
  * WorkerTest?
  * @testdox Worker option exitAfterNJobs: NULL means never exit, positive integer will exit after N jobs
  */
 function testWorkerOptionExitAfterNJobs()
 {
     // create a queuestore
     $q = new JQStore_Array();
     $this->assertEquals(0, $q->count('test'));
     // Add jobs
     foreach (range(1, 10) as $i) {
         $q->enqueue(new SampleJob());
     }
     $this->assertEquals(10, $q->count());
     $this->assertEquals(10, $q->count('test'));
     $this->assertEquals(10, $q->count('test', JQManagedJob::STATUS_QUEUED));
     SampleJobCounter::reset();
     // Start a worker to run 1 job
     $w = new JQWorker($q, array('queueName' => 'test', 'exitAfterNJobs' => 1, 'exitIfNoJobs' => true, 'silent' => true, 'enableJitter' => false));
     $w->start();
     $this->assertEquals(1, SampleJobCounter::count());
     $this->assertEquals(9, $q->count('test'));
     // Start a worker to run 2 jobs
     $w = new JQWorker($q, array('queueName' => 'test', 'exitAfterNJobs' => 2, 'exitIfNoJobs' => true, 'silent' => true, 'enableJitter' => false));
     $w->start();
     $this->assertEquals(3, SampleJobCounter::count());
     $this->assertEquals(7, $q->count('test'));
     // Start a worker to run remaining
     $w = new JQWorker($q, array('queueName' => 'test', 'exitAfterNJobs' => NULL, 'exitIfNoJobs' => true, 'silent' => true, 'enableJitter' => false));
     $w->start();
     $this->assertEquals(10, SampleJobCounter::count());
     $this->assertEquals(0, $q->count('test'));
 }
print <<<EXPLAIN
***********************************************************************************************************
  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();
示例#3
0
 function run(JQManagedJob $mJob)
 {
     SampleJobCounter::increment();
     return JQManagedJob::STATUS_COMPLETED;
 }