Completed
Push — master ( 31c40f...3d7ddc )
by Marco
13s
created

LockAgentWorker::run()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 13
nc 3
nop 0
1
<?php
2
3
namespace Doctrine\Tests\ORM\Functional\Locking;
4
5
use Doctrine\Common\Cache\ArrayCache;
6
use Doctrine\DBAL\Logging\EchoSQLLogger;
7
use Doctrine\ORM\Configuration;
8
use Doctrine\ORM\EntityManager;
9
10
class LockAgentWorker
11
{
12
    private $em;
13
14
    static public function run()
15
    {
16
        $lockAgent = new LockAgentWorker();
17
18
        $worker = new \GearmanWorker();
19
        $worker->addServer(
20
            isset($_SERVER['GEARMAN_HOST']) ? $_SERVER['GEARMAN_HOST'] : null,
21
            isset($_SERVER['GEARMAN_PORT']) ? $_SERVER['GEARMAN_PORT'] : 4730
22
        );
23
        $worker->addFunction("findWithLock", [$lockAgent, "findWithLock"]);
24
        $worker->addFunction("dqlWithLock", [$lockAgent, "dqlWithLock"]);
25
        $worker->addFunction('lock', [$lockAgent, 'lock']);
26
27
        while($worker->work()) {
28
            if ($worker->returnCode() != GEARMAN_SUCCESS) {
29
                echo "return_code: " . $worker->returnCode() . "\n";
30
                break;
31
            }
32
        }
33
    }
34
35
    protected function process($job, \Closure $do)
36
    {
37
        $fixture = $this->processWorkload($job);
38
39
        $s = microtime(true);
40
        $this->em->beginTransaction();
41
        $do($fixture, $this->em);
42
43
        sleep(1);
44
        $this->em->rollback();
45
        $this->em->clear();
46
        $this->em->close();
47
        $this->em->getConnection()->close();
48
49
        return (microtime(true) - $s);
50
    }
51
52
    public function findWithLock($job)
53
    {
54
        return $this->process($job, function($fixture, $em) {
55
            $entity = $em->find($fixture['entityName'], $fixture['entityId'], $fixture['lockMode']);
56
        });
57
    }
58
59
    public function dqlWithLock($job)
60
    {
61
        return $this->process($job, function($fixture, $em) {
62
            /* @var $query Doctrine\ORM\Query */
63
            $query = $em->createQuery($fixture['dql']);
64
            $query->setLockMode($fixture['lockMode']);
65
            $query->setParameters($fixture['dqlParams']);
66
            $result = $query->getResult();
67
        });
68
    }
69
70
    public function lock($job)
71
    {
72
        return $this->process($job, function($fixture, $em) {
73
            $entity = $em->find($fixture['entityName'], $fixture['entityId']);
74
            $em->lock($entity, $fixture['lockMode']);
75
        });
76
    }
77
78
    protected function processWorkload($job)
79
    {
80
        echo "Received job: " . $job->handle() . " for function " . $job->functionName() . "\n";
81
82
        $workload = $job->workload();
83
        $workload = unserialize($workload);
84
85
        if (!isset($workload['conn']) || !is_array($workload['conn'])) {
86
            throw new \InvalidArgumentException("Missing Database parameters");
87
        }
88
89
        $this->em = $this->createEntityManager($workload['conn']);
90
91
        if (!isset($workload['fixture'])) {
92
            throw new \InvalidArgumentException("Missing Fixture parameters");
93
        }
94
        return $workload['fixture'];
95
    }
96
97
    protected function createEntityManager($conn)
98
    {
99
        $config = new Configuration();
100
        $config->setProxyDir(__DIR__ . '/../../../Proxies');
101
        $config->setProxyNamespace('MyProject\Proxies');
102
        $config->setAutoGenerateProxyClasses(true);
103
104
        $annotDriver = $config->newDefaultAnnotationDriver([__DIR__ . '/../../../Models/'], true);
105
        $config->setMetadataDriverImpl($annotDriver);
106
107
        $cache = new ArrayCache();
108
        $config->setMetadataCacheImpl($cache);
109
        $config->setQueryCacheImpl($cache);
110
        $config->setSQLLogger(new EchoSQLLogger());
111
112
        $em = EntityManager::create($conn, $config);
113
114
        return $em;
115
    }
116
}
117
118
LockAgentWorker::run();
119