Since: 2.3.1
Inheritance: extends Mmoreram\GearmanBundle\Service\Abstracts\AbstractGearmanService
コード例 #1
0
 /**
  * Test quietness
  *
  * @dataProvider dataQuietness
  */
 public function testQuietness($quiet, $noInteraction, $confirmation, $countWriteln, $countDescriber, $countClient, $countExecute)
 {
     $this->input->expects($this->any())->method('getOption')->will($this->returnValueMap(array(array('quiet', $quiet), array('no-interaction', $noInteraction))));
     $this->questionHelper->expects($this->any())->method('ask')->will($this->returnValue($confirmation));
     $this->output->expects($countWriteln)->method('writeln');
     $this->gearmanDescriber->expects($countDescriber)->method('describeJob')->will($this->returnValue('olakase'));
     $this->gearmanClient->expects($countClient)->method('getJob')->will($this->returnValue(array()));
     $this->gearmanExecute->expects($countExecute)->method('executeJob');
     $this->command->setGearmanClient($this->gearmanClient)->setGearmanDescriber($this->gearmanDescriber)->setGearmanExecute($this->gearmanExecute)->setKernel($this->kernel)->run($this->input, $this->output);
 }
コード例 #2
0
 public function testDispatchingEventsOnJob()
 {
     // Worker mock
     $worker = $this->getMockBuilder('\\GearmanWorker')->disableOriginalConstructor()->getMock();
     $worker->method('addServer')->willReturn(true);
     // Wrapper mock
     $workers = array(0 => array('className' => "Mmoreram\\GearmanBundle\\Tests\\Service\\Mocks\\SingleCleanFile", 'fileName' => dirname(__FILE__) . '/Mocks/SingleCleanFile.php', 'callableName' => null, 'description' => "test", 'service' => false, 'servers' => array(), 'iterations' => 1, 'timeout' => null, 'minimumExecutionTime' => null, 'jobs' => array(0 => array('callableName' => "test", 'methodName' => "test", 'realCallableName' => "test", 'jobPrefix' => NULL, 'realCallableNameNoPrefix' => "test", 'description' => "test", 'iterations' => 1, 'servers' => array(), 'defaultMethod' => "doBackground", 'minimumExecutionTime' => null, 'timeout' => null))));
     $wrapper = $this->getMockBuilder('Mmoreram\\GearmanBundle\\Service\\GearmanCacheWrapper')->disableOriginalConstructor()->getMock();
     $wrapper->method('getWorkers')->willReturn($workers);
     // Prepare a dispatcher to listen to tested events
     $startingFlag = false;
     $executedFlag = false;
     $dispatcher = new \Symfony\Component\EventDispatcher\EventDispatcher();
     $dispatcher->addListener(GearmanEvents::GEARMAN_WORK_STARTING, function () use(&$startingFlag) {
         $startingFlag = true;
     });
     $dispatcher->addListener(GearmanEvents::GEARMAN_WORK_EXECUTED, function () use(&$executedFlag) {
         $executedFlag = true;
     });
     // Create the service under test
     $service = new GearmanExecute($wrapper, array());
     $service->setEventDispatcher($dispatcher);
     // We need a job object, this part could be improved
     $object = new \Mmoreram\GearmanBundle\Tests\Service\Mocks\SingleCleanFile();
     // Finalize worker mock by making it call our job object
     // This is normally handled by Gearman, but for test purpose we must simulate it
     $worker->method('work')->will($this->returnCallback(function () use($service, $object) {
         $service->handleJob(new \GearmanJob(), array('job_object_instance' => $object, 'job_method' => 'myMethod', 'jobs' => array()));
         return true;
     }));
     // Execute a job :)
     $service->executeJob('test', array(), $worker);
     // Do we have the events ?
     $this->assertTrue($startingFlag);
     $this->assertTrue($executedFlag);
 }
コード例 #3
0
 /**
  * Executes the current command.
  *
  * @param InputInterface  $input  An InputInterface instance
  * @param OutputInterface $output An OutputInterface instance
  *
  * @return integer 0 if everything went fine, or an error code
  *
  * @throws \LogicException When this abstract class is not implemented
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     /**
      * @var DialogHelper $dialog
      */
     $dialog = $this->getHelperSet()->get('dialog');
     if (!$input->getOption('no-interaction') && !$dialog->askConfirmation($output, '<question>This will execute asked worker with all its jobs?</question>', 'y')) {
         return;
     }
     if (!$input->getOption('quiet')) {
         $output->writeln(sprintf('<info>[%s] loading...</info>', date('Y-m-d H:i:s')));
     }
     $worker = $input->getArgument('worker');
     $workerStructure = $this->gearmanClient->getWorker($worker);
     if (!$input->getOption('no-description') && !$input->getOption('quiet')) {
         $this->gearmanDescriber->describeWorker($output, $workerStructure, true);
     }
     if (!$input->getOption('quiet')) {
         $output->writeln(sprintf('<info>[%s] loaded. Ctrl+C to break</info>', date('Y-m-d H:i:s')));
     }
     $this->gearmanExecute->setOutput($output)->executeWorker($worker, array('iterations' => $input->getOption('iterations'), 'minimum_execution_time' => $input->getOption('minimum-execution-time'), 'timeout' => $input->getOption('timeout')));
 }
コード例 #4
0
 /**
  * Executes the current command.
  *
  * @param InputInterface  $input  An InputInterface instance
  * @param OutputInterface $output An OutputInterface instance
  *
  * @return integer 0 if everything went fine, or an error code
  *
  * @throws \LogicException When this abstract class is not implemented
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     /**
      * @var DialogHelper $dialog
      */
     $dialog = $this->getHelperSet()->get('dialog');
     if (!$input->getOption('no-interaction') && !$dialog->askConfirmation($output, '<question>This will execute asked job?</question>', 'y')) {
         return;
     }
     if (!$input->getOption('quiet')) {
         $output->writeln(sprintf('<info>[%s] loading...</info>', date('Y-m-d H:i:s')));
     }
     $job = $input->getArgument('job');
     $jobStructure = $this->gearmanClient->getJob($job);
     if (!$input->getOption('quiet')) {
         $this->gearmanDescriber->describeJob($output, $jobStructure, true);
     }
     if (!$input->getOption('quiet')) {
         $output->writeln(sprintf('<info>[%s] loaded. Ctrl+C to break</info>', date('Y-m-d H:i:s')));
     }
     $this->gearmanExecute->setOutput($output)->executeJob($job);
 }
コード例 #5
0
 /**
  * Executes the current command.
  *
  * @param InputInterface  $input  An InputInterface instance
  * @param OutputInterface $output An OutputInterface instance
  *
  * @return integer 0 if everything went fine, or an error code
  *
  * @throws \LogicException When this abstract class is not implemented
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     /**
      * @var QuestionHelper $question
      */
     $question = $this->getHelperSet()->get('question');
     if (!$input->getOption('no-interaction') && !$question->ask($input, $output, new ConfirmationQuestion('This will execute asked job?'))) {
         return;
     }
     if (!$input->getOption('quiet')) {
         $output->writeln(sprintf('<info>[%s] loading...</info>', date('Y-m-d H:i:s')));
     }
     $job = $input->getArgument('job');
     $jobStructure = $this->gearmanClient->getJob($job);
     if (!$input->getOption('no-description') && !$input->getOption('quiet')) {
         $this->gearmanDescriber->describeJob($output, $jobStructure, true);
     }
     if (!$input->getOption('quiet')) {
         $output->writeln(sprintf('<info>[%s] loaded. Ctrl+C to break</info>', date('Y-m-d H:i:s')));
     }
     $this->gearmanExecute->setOutput($output)->executeJob($job, array('iterations' => $input->getOption('iterations'), 'minimum_execution_time' => $input->getOption('minimum-execution-time'), 'timeout' => $input->getOption('timeout')));
 }