1
|
|
|
<?php |
2
|
|
|
namespace Tarioch\EveapiFetcherBundle\Component\Worker; |
3
|
|
|
|
4
|
|
|
use JMS\DiExtraBundle\Annotation as DI; |
5
|
|
|
use Doctrine\ORM\EntityManager; |
6
|
|
|
use Pheal\Exceptions\PhealException; |
7
|
|
|
use Psr\Log\LoggerInterface; |
8
|
|
|
use Symfony\Component\Stopwatch\Stopwatch; |
9
|
|
|
use Tarioch\EveapiFetcherBundle\Component\Section\SectionApiFactory; |
10
|
|
|
use Doctrine\DBAL\LockMode; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @DI\Service(public=false) |
14
|
|
|
*/ |
15
|
|
|
class ApiUpdater |
16
|
|
|
{ |
17
|
|
|
const ERROR_MAX = 20; |
18
|
|
|
|
19
|
|
|
private $logger; |
20
|
|
|
private $entityManager; |
21
|
|
|
private $apiTimeCalculator; |
22
|
|
|
private $sectionApiFactory; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @DI\InjectParams({ |
26
|
|
|
* "entityManager" = @DI\Inject("doctrine.orm.eveapi_entity_manager"), |
27
|
|
|
* "logger" = @DI\Inject("logger"), |
28
|
|
|
* "apiTimeCalculator" = @DI\Inject("tarioch.eveapi_fetcher_bundle.component.worker.api_time_calculator"), |
29
|
|
|
* "sectionApiFactory" = @DI\Inject("tarioch.eveapi_fetcher_bundle.component.section.section_api_factory") |
30
|
|
|
* }) |
31
|
|
|
*/ |
32
|
|
|
public function __construct( |
33
|
|
|
EntityManager $entityManager, |
34
|
|
|
LoggerInterface $logger, |
35
|
|
|
ApiTimeCalculator $apiTimeCalculator, |
36
|
|
|
SectionApiFactory $sectionApiFactory |
37
|
|
|
) { |
38
|
|
|
$this->entityManager = $entityManager; |
39
|
|
|
$this->logger = $logger; |
40
|
|
|
$this->apiTimeCalculator = $apiTimeCalculator; |
41
|
|
|
$this->sectionApiFactory = $sectionApiFactory; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function update($apiCallId) |
45
|
|
|
{ |
46
|
|
|
print('CallId ' . $apiCallId . "\n"); |
47
|
|
|
$em = $this->entityManager; |
48
|
|
|
$call = $em->find('TariochEveapiFetcherBundle:ApiCall', $apiCallId); |
49
|
|
|
if ($call !== null && $call->getOwner() !== null) { |
50
|
|
|
$repo = $em->getRepository('TariochEveapiFetcherBundle:AccountCharacter'); |
51
|
|
|
$owners = $repo->findByCharacterId($call->getOwner()->getCharacterId()); |
52
|
|
|
if (count($owners) > 1) { |
53
|
|
|
foreach ($owners as $owner) { |
54
|
|
|
$entity = 'TariochEveapiFetcherBundle:AccountCharacter'; |
55
|
|
|
$em->find($entity, $owner->getId(), LockMode::PESSIMISTIC_WRITE); |
56
|
|
|
$this->logger->info('{callId}: locked for owner {ownerId}', array( |
57
|
|
|
'callId' => $apiCallId, |
58
|
|
|
'ownerId' => $owner->getId() |
59
|
|
|
)); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
if ($this->apiTimeCalculator->isCallStillValid($call)) { |
64
|
|
|
$api = $call->getApi(); |
65
|
|
|
$apiInfo = $api->getSection() . ' ' . $api->getName(); |
66
|
|
|
|
67
|
|
|
$stopwatch = new Stopwatch(); |
68
|
|
|
$stopwatch->start($apiInfo); |
69
|
|
|
try { |
70
|
|
|
$sectionApi = $this->sectionApiFactory->create($call); |
71
|
|
|
|
72
|
|
|
$cachedUntil = $sectionApi->update($call); |
73
|
|
|
|
74
|
|
|
$call->clearErrorCount(); |
75
|
|
|
$call->setCachedUntil($cachedUntil); |
76
|
|
|
} catch (PhealException $e) { |
77
|
|
|
$this->logger->info('{callId}: Api call failed', array('callId' => $apiCallId, 'exception' => $e)); |
78
|
|
|
$call->increaseErrorCount(); |
79
|
|
|
if ($call->getErrorCount() > self::ERROR_MAX) { |
80
|
|
|
$call->setActive(false); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
$call->setEarliestNextCall($this->apiTimeCalculator->calculateEarliestNextCall($call)); |
84
|
|
|
|
85
|
|
|
$event = $stopwatch->stop($apiInfo); |
86
|
|
|
$this->logger->info('{callId}: {apiInfo} duration: {duration} memory: {memory}', array( |
87
|
|
|
'callId' => $apiCallId, |
88
|
|
|
'apiInfo' => $apiInfo, |
89
|
|
|
'duration' => $event->getDuration(), |
90
|
|
|
'memory' => $event->getMemory() |
91
|
|
|
)); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|