/** * * @param \Qutee\Task $task * * @return \Qutee\Persistor\Pdo */ public function addTask(Task $task) { // Check if the task is unique and already exists if ($task->isUnique() && $this->_hasTaskByUniqueId($task->getUniqueId())) { return $this; } $statement = $this->_getPdo()->prepare(sprintf(' INSERT INTO %s SET name = :name, method_name = :method_name, data = :data, priority = :priority, unique_id = :unique_id, created_at = NOW() ', $this->_options['table_name'])); $statement->execute(array(':name' => $task->getName(), ':method_name' => $task->getMethodName(), ':data' => serialize($task), ':priority' => $task->getPriority(), ':unique_id' => $task->isUnique() ? $task->getUniqueId() : null)); return $this; }
/** * @covers \Qutee\Task::__sleep */ public function testSerializingAndUnserializingTask() { $data = array('test' => 'data'); $this->object->setName('TestTask'); $this->object->setData($data); $this->object->setMethodName('methodName'); $this->object->setPriority(Task::PRIORITY_HIGH); $this->object->setUniqueId('SomeUniqueId'); $serialized = serialize($this->object); $unserialized = unserialize($serialized); $this->assertInstanceOf('\\Qutee\\Task', $unserialized); $this->assertEquals('TestTask', $unserialized->getName()); $this->assertEquals('methodName', $unserialized->getMethodName()); $this->assertSame($data, $unserialized->getData()); $this->assertEquals(3, $unserialized->getPriority()); $this->assertTrue($unserialized->isUnique()); $this->assertEquals(md5('TestTaskSomeUniqueId'), $unserialized->getUniqueId()); }
/** * * @param \Qutee\Task $task * * @return string */ protected function _createKey(\Qutee\Task $task) { if ($task->isUnique()) { $key = sprintf('task:%s:%s', $task->getName(), $task->getUniqueId()); } else { $key = sprintf('task:%s:%s', $task->getName(), uniqid('', true)); } return $key; }
/** * Get class of the task, run it's default method or method specified in * task data [method] * * @param Task $task */ protected function _runTask(Task $task) { $taskClassName = $task->getClassName(); if (!class_exists($taskClassName)) { throw new \InvalidArgumentException(sprintf('Task class "%s" not found', $taskClassName)); } $taskObject = new $taskClassName(); if ($taskObject instanceof TaskInterface) { $taskObject->setData($task->getData()); $taskObject->run(); return $taskObject; } else { $methodName = $task->getMethodName(); $taskObject->{$methodName}($task->getData()); } }
*/ $loader = (require_once __DIR__ . "/../vendor/autoload.php"); $loader->add('Acme', __DIR__); use Qutee\Queue; use Qutee\Task; use Qutee\Worker; // Setup our queue with persistor of choice, preferably in // Dependency Injection Container $redisParams = array('host' => '127.0.0.1', 'port' => 6379); $queuePersistor = new Qutee\Persistor\Redis($redisParams); $queue = new Queue(); $queue->setPersistor($queuePersistor); // Create a task $task = new Task(); $task->setName('Acme/SendMail')->setData(array('to' => '*****@*****.**', 'from' => '*****@*****.**', 'subject' => 'Hi!', 'text' => 'It\'s your faithful QuTee!'))->setPriority(Task::PRIORITY_HIGH)->setUniqueId('*****@*****.**'); // Queue it $queue->addTask($task); // Or do this in one go, if you set the queue (bootstrap maybe?) Task::create('Acme/SendMail', array('to' => '*****@*****.**', 'from' => '*****@*****.**', 'subject' => 'Hi!', 'text' => 'It\'s your faithful QuTee!'), Task::PRIORITY_HIGH); // Send worker to do it $worker = new Worker(); $worker->setQueue($queue)->setPriority(Task::PRIORITY_HIGH); while (true) { try { if (null !== ($task = $worker->run())) { echo 'Ran task: ' . $task->getName() . PHP_EOL; } } catch (Exception $e) { echo 'Error: ' . $e->getMessage() . PHP_EOL; } }
/** * @covers \Qutee\Queue::getTasks */ public function testGettingTasksQueriesGivenPriority() { $task1 = new Task('task1'); $task1->setPriority(Task::PRIORITY_HIGH); $task2 = new Task('task2'); $task2->setPriority(Task::PRIORITY_LOW); $this->object->addTask($task1)->addTask($task2); $outputTasks = $this->object->getTasks(Task::PRIORITY_LOW); $this->assertTrue(count($outputTasks) == 1); $outputTask = reset($outputTasks); $this->assertEquals($outputTask->getName(), $task2->getName()); }
/** * @covers \Qutee\Queue::addTask */ public function testAddingUniqueTaskAddsOnlyOneTime() { $task = new \Qutee\Task(); $task->setName('test'); $task->setUniqueId('test'); $this->object->addTask($task); $this->object->addTask($task); $tasks = $this->object->getTasks(); $this->assertTrue(count($tasks) == 1); $outputTask = reset($tasks); $this->assertEquals($outputTask->getName(), $task->getName()); }