LockAgentWorker.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. namespace Doctrine\Tests\ORM\Functional\Locking;
  3. require_once __DIR__ . "/../../../TestInit.php";
  4. class LockAgentWorker
  5. {
  6. private $em;
  7. static public function run()
  8. {
  9. $lockAgent = new LockAgentWorker();
  10. $worker = new \GearmanWorker();
  11. $worker->addServer();
  12. $worker->addFunction("findWithLock", array($lockAgent, "findWithLock"));
  13. $worker->addFunction("dqlWithLock", array($lockAgent, "dqlWithLock"));
  14. $worker->addFunction('lock', array($lockAgent, 'lock'));
  15. while($worker->work()) {
  16. if ($worker->returnCode() != GEARMAN_SUCCESS) {
  17. echo "return_code: " . $worker->returnCode() . "\n";
  18. break;
  19. }
  20. }
  21. }
  22. protected function process($job, \Closure $do)
  23. {
  24. $fixture = $this->processWorkload($job);
  25. $s = microtime(true);
  26. $this->em->beginTransaction();
  27. $do($fixture, $this->em);
  28. sleep(1);
  29. $this->em->rollback();
  30. $this->em->clear();
  31. $this->em->close();
  32. $this->em->getConnection()->close();
  33. return (microtime(true) - $s);
  34. }
  35. public function findWithLock($job)
  36. {
  37. return $this->process($job, function($fixture, $em) {
  38. $entity = $em->find($fixture['entityName'], $fixture['entityId'], $fixture['lockMode']);
  39. });
  40. }
  41. public function dqlWithLock($job)
  42. {
  43. return $this->process($job, function($fixture, $em) {
  44. /* @var $query Doctrine\ORM\Query */
  45. $query = $em->createQuery($fixture['dql']);
  46. $query->setLockMode($fixture['lockMode']);
  47. $query->setParameters($fixture['dqlParams']);
  48. $result = $query->getResult();
  49. });
  50. }
  51. public function lock($job)
  52. {
  53. return $this->process($job, function($fixture, $em) {
  54. $entity = $em->find($fixture['entityName'], $fixture['entityId']);
  55. $em->lock($entity, $fixture['lockMode']);
  56. });
  57. }
  58. protected function processWorkload($job)
  59. {
  60. echo "Received job: " . $job->handle() . " for function " . $job->functionName() . "\n";
  61. $workload = $job->workload();
  62. $workload = unserialize($workload);
  63. if (!isset($workload['conn']) || !is_array($workload['conn'])) {
  64. throw new \InvalidArgumentException("Missing Database parameters");
  65. }
  66. $this->em = $this->createEntityManager($workload['conn']);
  67. if (!isset($workload['fixture'])) {
  68. throw new \InvalidArgumentException("Missing Fixture parameters");
  69. }
  70. return $workload['fixture'];
  71. }
  72. protected function createEntityManager($conn)
  73. {
  74. $config = new \Doctrine\ORM\Configuration();
  75. $config->setProxyDir(__DIR__ . '/../../../Proxies');
  76. $config->setProxyNamespace('MyProject\Proxies');
  77. $config->setAutoGenerateProxyClasses(true);
  78. $annotDriver = $config->newDefaultAnnotationDriver(array(__DIR__ . '/../../../Models/'));
  79. $config->setMetadataDriverImpl($annotDriver);
  80. $cache = new \Doctrine\Common\Cache\ArrayCache();
  81. $config->setMetadataCacheImpl($cache);
  82. $config->setQueryCacheImpl($cache);
  83. $config->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger());
  84. $em = \Doctrine\ORM\EntityManager::create($conn, $config);
  85. return $em;
  86. }
  87. }
  88. LockAgentWorker::run();