Phalcon\Queue\Beanstalk\Extended::addWorker PHP Method

addWorker() public method

Adds new worker to the pool.
public addWorker ( string $tube, callable $callback )
$tube string
$callback callable
    public function addWorker($tube, $callback)
    {
        if (!is_string($tube)) {
            throw new \InvalidArgumentException('The tube name must be a string.');
        }
        if (!is_callable($callback)) {
            throw new \InvalidArgumentException('The callback is invalid.');
        }
        $this->workers[$tube] = $callback;
    }

Usage Example

示例#1
1
 /**
  * @depends testShouldGetTubes
  */
 public function testShouldDoWork()
 {
     if (!class_exists('\\duncan3dc\\Helpers\\Fork')) {
         $this->markTestSkipped(sprintf('%s used as a dependency \\duncan3dc\\Helpers\\Fork. You can install it by using' . 'composer require "duncan3dc/fork-helper":"*"', get_class($this->client)));
     }
     $expected = ['test-tube-1' => '1', 'test-tube-2' => '2'];
     foreach ($expected as $tube => $value) {
         $this->client->addWorker($tube, function (Job $job) {
             // Store string "test-tube-%JOB_BODY%" in shared memory
             $memory = shmop_open($this->shmKey, 'c', 0644, $this->shmLimit);
             $output = trim(shmop_read($memory, 0, $this->shmLimit));
             $output .= sprintf("\ntest-tube-%s", $job->getBody());
             shmop_write($memory, $output, 0);
             shmop_close($memory);
             exit(1);
         });
         $this->client->putInTube($tube, $value);
     }
     $this->client->doWork();
     $memory = shmop_open($this->shmKey, 'a', 0, 0);
     $output = shmop_read($memory, 0, $this->shmLimit);
     $this->assertTrue(shmop_delete($memory));
     shmop_close($memory);
     $this->assertNotEmpty($output);
     // Compare number of items in expected list with lines in shared memory
     $this->assertEquals(count($expected), count(array_unique(explode("\n", trim($output)))));
 }
All Usage Examples Of Phalcon\Queue\Beanstalk\Extended::addWorker