2010-04-18 13:58:43 +00:00
|
|
|
<?php
|
2018-05-25 07:26:54 +00:00
|
|
|
|
2021-02-18 23:23:32 +00:00
|
|
|
namespace Resque\Test;
|
|
|
|
|
2010-04-18 13:58:43 +00:00
|
|
|
/**
|
2021-02-18 23:23:32 +00:00
|
|
|
* \Resque\Worker tests.
|
2010-04-18 13:58:43 +00:00
|
|
|
*
|
2018-05-25 09:03:48 +00:00
|
|
|
* @package Resque/Tests
|
2021-02-18 23:23:32 +00:00
|
|
|
* @author Daniel Mason <daniel@m2.nz>
|
2018-05-25 09:03:48 +00:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php
|
2010-04-18 13:58:43 +00:00
|
|
|
*/
|
2018-05-25 07:26:54 +00:00
|
|
|
|
2021-02-18 23:23:32 +00:00
|
|
|
class WorkerTest extends TestCase
|
2010-04-18 13:58:43 +00:00
|
|
|
{
|
2018-05-25 09:03:48 +00:00
|
|
|
public function testWorkerRegistersInList()
|
|
|
|
{
|
2021-02-18 23:23:32 +00:00
|
|
|
$worker = new \Resque\Worker('*');
|
|
|
|
$worker->setLogger(new \Resque\Log());
|
2018-05-25 09:03:48 +00:00
|
|
|
$worker->registerWorker();
|
|
|
|
|
|
|
|
// Make sure the worker is in the list
|
|
|
|
$this->assertTrue((bool)$this->redis->sismember('resque:workers', (string)$worker));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetAllWorkers()
|
|
|
|
{
|
|
|
|
$num = 3;
|
|
|
|
// Register a few workers
|
|
|
|
for ($i = 0; $i < $num; ++$i) {
|
2021-02-18 23:23:32 +00:00
|
|
|
$worker = new \Resque\Worker('queue_' . $i);
|
|
|
|
$worker->setLogger(new \Resque\Log());
|
2018-05-25 09:03:48 +00:00
|
|
|
$worker->registerWorker();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now try to get them
|
2021-02-18 23:23:32 +00:00
|
|
|
$this->assertEquals($num, count(\Resque\Worker::all()));
|
2018-05-25 09:03:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetWorkerById()
|
|
|
|
{
|
2021-02-18 23:23:32 +00:00
|
|
|
$worker = new \Resque\Worker('*');
|
|
|
|
$worker->setLogger(new \Resque\Log());
|
2018-05-25 09:03:48 +00:00
|
|
|
$worker->registerWorker();
|
|
|
|
|
2021-02-18 23:23:32 +00:00
|
|
|
$newWorker = \Resque\Worker::find((string)$worker);
|
2018-05-25 09:03:48 +00:00
|
|
|
$this->assertEquals((string)$worker, (string)$newWorker);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testInvalidWorkerDoesNotExist()
|
|
|
|
{
|
2021-02-18 23:23:32 +00:00
|
|
|
$this->assertFalse(\Resque\Worker::exists('blah'));
|
2018-05-25 09:03:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testWorkerCanUnregister()
|
|
|
|
{
|
2021-02-18 23:23:32 +00:00
|
|
|
$worker = new \Resque\Worker('*');
|
|
|
|
$worker->setLogger(new \Resque\Log());
|
2018-05-25 09:03:48 +00:00
|
|
|
$worker->registerWorker();
|
|
|
|
$worker->unregisterWorker();
|
|
|
|
|
2021-02-18 23:23:32 +00:00
|
|
|
$this->assertFalse(\Resque\Worker::exists((string)$worker));
|
|
|
|
$this->assertEquals([], \Resque\Worker::all());
|
2018-05-29 20:25:02 +00:00
|
|
|
$this->assertEquals([], $this->redis->smembers('resque:workers'));
|
2018-05-25 09:03:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testPausedWorkerDoesNotPickUpJobs()
|
|
|
|
{
|
2021-02-18 23:23:32 +00:00
|
|
|
$worker = new \Resque\Worker('*');
|
|
|
|
$worker->setLogger(new \Resque\Log());
|
2018-05-25 09:03:48 +00:00
|
|
|
$worker->pauseProcessing();
|
2021-02-18 23:23:32 +00:00
|
|
|
\Resque\Resque::enqueue('jobs', '\Resque\Test\TestJob');
|
2018-05-25 09:03:48 +00:00
|
|
|
$worker->work(0);
|
|
|
|
$worker->work(0);
|
2021-02-18 23:23:32 +00:00
|
|
|
$this->assertEquals(0, \Resque\Stat::get('processed'));
|
2018-05-25 09:03:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testResumedWorkerPicksUpJobs()
|
|
|
|
{
|
2021-02-18 23:23:32 +00:00
|
|
|
$worker = new \Resque\Worker('*');
|
|
|
|
$worker->setLogger(new \Resque\Log());
|
2018-05-25 09:03:48 +00:00
|
|
|
$worker->pauseProcessing();
|
2021-02-18 23:23:32 +00:00
|
|
|
\Resque\Resque::enqueue('jobs', '\Resque\Test\TestJob');
|
2018-05-25 09:03:48 +00:00
|
|
|
$worker->work(0);
|
2021-02-18 23:23:32 +00:00
|
|
|
$this->assertEquals(0, \Resque\Stat::get('processed'));
|
2018-05-25 09:03:48 +00:00
|
|
|
$worker->unPauseProcessing();
|
|
|
|
$worker->work(0);
|
2021-02-18 23:23:32 +00:00
|
|
|
$this->assertEquals(1, \Resque\Stat::get('processed'));
|
2018-05-25 09:03:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testWorkerCanWorkOverMultipleQueues()
|
|
|
|
{
|
2021-02-18 23:23:32 +00:00
|
|
|
$worker = new \Resque\Worker([
|
2018-05-25 09:03:48 +00:00
|
|
|
'queue1',
|
|
|
|
'queue2'
|
2018-05-29 20:25:02 +00:00
|
|
|
]);
|
2021-02-18 23:23:32 +00:00
|
|
|
$worker->setLogger(new \Resque\Log());
|
2018-05-25 09:03:48 +00:00
|
|
|
$worker->registerWorker();
|
2021-02-18 23:23:32 +00:00
|
|
|
\Resque\Resque::enqueue('queue1', '\Resque\Test\TestJob_1');
|
|
|
|
\Resque\Resque::enqueue('queue2', '\Resque\Test\TestJob_2');
|
2018-05-25 09:03:48 +00:00
|
|
|
|
|
|
|
$job = $worker->reserve();
|
|
|
|
$this->assertEquals('queue1', $job->queue);
|
|
|
|
|
|
|
|
$job = $worker->reserve();
|
|
|
|
$this->assertEquals('queue2', $job->queue);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testWorkerWorksQueuesInSpecifiedOrder()
|
|
|
|
{
|
2021-02-18 23:23:32 +00:00
|
|
|
$worker = new \Resque\Worker([
|
2018-05-25 09:03:48 +00:00
|
|
|
'high',
|
|
|
|
'medium',
|
|
|
|
'low'
|
2018-05-29 20:25:02 +00:00
|
|
|
]);
|
2021-02-18 23:23:32 +00:00
|
|
|
$worker->setLogger(new \Resque\Log());
|
2018-05-25 09:03:48 +00:00
|
|
|
$worker->registerWorker();
|
|
|
|
|
|
|
|
// Queue the jobs in a different order
|
2021-02-18 23:23:32 +00:00
|
|
|
\Resque\Resque::enqueue('low', '\Resque\Test\TestJob_1');
|
|
|
|
\Resque\Resque::enqueue('high', '\Resque\Test\TestJob_2');
|
|
|
|
\Resque\Resque::enqueue('medium', '\Resque\Test\TestJob_3');
|
2018-05-25 09:03:48 +00:00
|
|
|
|
|
|
|
// Now check we get the jobs back in the right order
|
|
|
|
$job = $worker->reserve();
|
|
|
|
$this->assertEquals('high', $job->queue);
|
|
|
|
|
|
|
|
$job = $worker->reserve();
|
|
|
|
$this->assertEquals('medium', $job->queue);
|
|
|
|
|
|
|
|
$job = $worker->reserve();
|
|
|
|
$this->assertEquals('low', $job->queue);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testWildcardQueueWorkerWorksAllQueues()
|
|
|
|
{
|
2021-02-18 23:23:32 +00:00
|
|
|
$worker = new \Resque\Worker('*');
|
|
|
|
$worker->setLogger(new \Resque\Log());
|
2018-05-25 09:03:48 +00:00
|
|
|
$worker->registerWorker();
|
|
|
|
|
2021-02-18 23:23:32 +00:00
|
|
|
\Resque\Resque::enqueue('queue1', '\Resque\Test\TestJob_1');
|
|
|
|
\Resque\Resque::enqueue('queue2', '\Resque\Test\TestJob_2');
|
2018-05-25 09:03:48 +00:00
|
|
|
|
|
|
|
$job = $worker->reserve();
|
|
|
|
$this->assertEquals('queue1', $job->queue);
|
|
|
|
|
|
|
|
$job = $worker->reserve();
|
|
|
|
$this->assertEquals('queue2', $job->queue);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testWorkerDoesNotWorkOnUnknownQueues()
|
|
|
|
{
|
2021-02-18 23:23:32 +00:00
|
|
|
$worker = new \Resque\Worker('queue1');
|
|
|
|
$worker->setLogger(new \Resque\Log());
|
2018-05-25 09:03:48 +00:00
|
|
|
$worker->registerWorker();
|
2021-02-18 23:23:32 +00:00
|
|
|
\Resque\Resque::enqueue('queue2', '\Resque\Test\TestJob');
|
2018-05-25 09:03:48 +00:00
|
|
|
|
|
|
|
$this->assertFalse($worker->reserve());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testWorkerClearsItsStatusWhenNotWorking()
|
|
|
|
{
|
2021-02-18 23:23:32 +00:00
|
|
|
\Resque\Resque::enqueue('jobs', '\Resque\Test\TestJob');
|
|
|
|
$worker = new \Resque\Worker('jobs');
|
|
|
|
$worker->setLogger(new \Resque\Log());
|
2018-05-25 09:03:48 +00:00
|
|
|
$job = $worker->reserve();
|
|
|
|
$worker->workingOn($job);
|
|
|
|
$worker->doneWorking();
|
2018-05-29 20:25:02 +00:00
|
|
|
$this->assertEquals([], $worker->job());
|
2018-05-25 09:03:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testWorkerRecordsWhatItIsWorkingOn()
|
|
|
|
{
|
2021-02-18 23:23:32 +00:00
|
|
|
$worker = new \Resque\Worker('jobs');
|
|
|
|
$worker->setLogger(new \Resque\Log());
|
2018-05-25 09:03:48 +00:00
|
|
|
$worker->registerWorker();
|
|
|
|
|
2018-05-29 20:25:02 +00:00
|
|
|
$payload = [
|
2021-02-18 23:23:32 +00:00
|
|
|
'class' => '\Resque\Test\TestJob'
|
2018-05-29 20:25:02 +00:00
|
|
|
];
|
2021-02-18 23:23:32 +00:00
|
|
|
$job = new \Resque\Job\Job('jobs', $payload);
|
2018-05-25 09:03:48 +00:00
|
|
|
$worker->workingOn($job);
|
|
|
|
|
|
|
|
$job = $worker->job();
|
|
|
|
$this->assertEquals('jobs', $job['queue']);
|
|
|
|
if (!isset($job['run_at'])) {
|
|
|
|
$this->fail('Job does not have run_at time');
|
|
|
|
}
|
|
|
|
$this->assertEquals($payload, $job['payload']);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testWorkerErasesItsStatsWhenShutdown()
|
|
|
|
{
|
2021-02-18 23:23:32 +00:00
|
|
|
\Resque\Resque::enqueue('jobs', '\Resque\Test\TestJob');
|
|
|
|
\Resque\Resque::enqueue('jobs', '\Resque\Test\InvalidJob');
|
2018-05-25 09:03:48 +00:00
|
|
|
|
2021-02-18 23:23:32 +00:00
|
|
|
$worker = new \Resque\Worker('jobs');
|
|
|
|
$worker->setLogger(new \Resque\Log());
|
2018-05-25 09:03:48 +00:00
|
|
|
$worker->work(0);
|
|
|
|
$worker->work(0);
|
|
|
|
|
|
|
|
$this->assertEquals(0, $worker->getStat('processed'));
|
|
|
|
$this->assertEquals(0, $worker->getStat('failed'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testWorkerCleansUpDeadWorkersOnStartup()
|
|
|
|
{
|
|
|
|
// Register a good worker
|
2021-02-18 23:23:32 +00:00
|
|
|
$goodWorker = new \Resque\Worker('jobs');
|
|
|
|
$goodWorker->setLogger(new \Resque\Log());
|
2018-05-25 09:03:48 +00:00
|
|
|
$goodWorker->registerWorker();
|
|
|
|
$workerId = explode(':', $goodWorker);
|
|
|
|
|
|
|
|
// Register some bad workers
|
2021-02-18 23:23:32 +00:00
|
|
|
$worker = new \Resque\Worker('jobs');
|
|
|
|
$worker->setLogger(new \Resque\Log());
|
2018-05-25 09:03:48 +00:00
|
|
|
$worker->setId($workerId[0] . ':1:jobs');
|
|
|
|
$worker->registerWorker();
|
|
|
|
|
2021-02-18 23:23:32 +00:00
|
|
|
$worker = new \Resque\Worker(['high', 'low']);
|
|
|
|
$worker->setLogger(new \Resque\Log());
|
2018-05-25 09:03:48 +00:00
|
|
|
$worker->setId($workerId[0] . ':2:high,low');
|
|
|
|
$worker->registerWorker();
|
|
|
|
|
2021-02-18 23:23:32 +00:00
|
|
|
$this->assertEquals(3, count(\Resque\Worker::all()));
|
2018-05-25 09:03:48 +00:00
|
|
|
|
|
|
|
$goodWorker->pruneDeadWorkers();
|
|
|
|
|
|
|
|
// There should only be $goodWorker left now
|
2021-02-18 23:23:32 +00:00
|
|
|
$this->assertEquals(1, count(\Resque\Worker::all()));
|
2018-05-25 09:03:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testDeadWorkerCleanUpDoesNotCleanUnknownWorkers()
|
|
|
|
{
|
|
|
|
// Register a bad worker on this machine
|
2021-02-18 23:23:32 +00:00
|
|
|
$worker = new \Resque\Worker('jobs');
|
|
|
|
$worker->setLogger(new \Resque\Log());
|
2018-05-25 09:03:48 +00:00
|
|
|
$workerId = explode(':', $worker);
|
|
|
|
$worker->setId($workerId[0] . ':1:jobs');
|
|
|
|
$worker->registerWorker();
|
|
|
|
|
|
|
|
// Register some other false workers
|
2021-02-18 23:23:32 +00:00
|
|
|
$worker = new \Resque\Worker('jobs');
|
|
|
|
$worker->setLogger(new \Resque\Log());
|
2018-05-25 09:03:48 +00:00
|
|
|
$worker->setId('my.other.host:1:jobs');
|
|
|
|
$worker->registerWorker();
|
|
|
|
|
2021-02-18 23:23:32 +00:00
|
|
|
$this->assertEquals(2, count(\Resque\Worker::all()));
|
2018-05-25 09:03:48 +00:00
|
|
|
|
|
|
|
$worker->pruneDeadWorkers();
|
|
|
|
|
|
|
|
// my.other.host should be left
|
2021-02-18 23:23:32 +00:00
|
|
|
$workers = \Resque\Worker::all();
|
2018-05-25 09:03:48 +00:00
|
|
|
$this->assertEquals(1, count($workers));
|
|
|
|
$this->assertEquals((string)$worker, (string)$workers[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testWorkerFailsUncompletedJobsOnExit()
|
|
|
|
{
|
2021-02-18 23:23:32 +00:00
|
|
|
$worker = new \Resque\Worker('jobs');
|
|
|
|
$worker->setLogger(new \Resque\Log());
|
2018-05-25 09:03:48 +00:00
|
|
|
$worker->registerWorker();
|
|
|
|
|
2018-05-29 20:25:02 +00:00
|
|
|
$payload = [
|
2021-02-18 23:23:32 +00:00
|
|
|
'class' => '\Resque\Test\TestJob'
|
2018-05-29 20:25:02 +00:00
|
|
|
];
|
2021-02-18 23:23:32 +00:00
|
|
|
$job = new \Resque\Job\Job('jobs', $payload);
|
2018-05-25 09:03:48 +00:00
|
|
|
|
|
|
|
$worker->workingOn($job);
|
|
|
|
$worker->unregisterWorker();
|
|
|
|
|
2021-02-18 23:23:32 +00:00
|
|
|
$this->assertEquals(1, \Resque\Stat::get('failed'));
|
2018-05-25 09:03:48 +00:00
|
|
|
}
|
2013-03-12 08:55:03 +00:00
|
|
|
|
|
|
|
public function testBlockingListPop()
|
|
|
|
{
|
2021-02-18 23:23:32 +00:00
|
|
|
$worker = new \Resque\Worker('jobs');
|
|
|
|
$worker->setLogger(new \Resque\Log());
|
2013-03-12 08:55:03 +00:00
|
|
|
$worker->registerWorker();
|
|
|
|
|
2021-02-18 23:23:32 +00:00
|
|
|
\Resque\Resque::enqueue('jobs', '\Resque\Test\TestJob_1');
|
|
|
|
\Resque\Resque::enqueue('jobs', '\Resque\Test\TestJob_2');
|
2013-03-12 08:55:03 +00:00
|
|
|
|
|
|
|
$i = 1;
|
2018-05-25 09:03:48 +00:00
|
|
|
while ($job = $worker->reserve(true, 2)) {
|
2021-02-18 23:23:32 +00:00
|
|
|
$this->assertEquals('\Resque\Test\TestJob_' . $i, $job->payload['class']);
|
2013-03-12 08:55:03 +00:00
|
|
|
|
2018-05-25 09:03:48 +00:00
|
|
|
if ($i == 2) {
|
2013-03-12 08:55:03 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
$i++;
|
|
|
|
}
|
|
|
|
|
2018-05-25 09:00:53 +00:00
|
|
|
$this->assertEquals(2, $i);
|
2013-03-12 08:55:03 +00:00
|
|
|
}
|
2010-04-18 13:58:43 +00:00
|
|
|
}
|