-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLtiInstanceCacheWarmerCommand.php
More file actions
137 lines (108 loc) · 4.64 KB
/
Copy pathLtiInstanceCacheWarmerCommand.php
File metadata and controls
137 lines (108 loc) · 4.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php
/*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; under version 2
* of the License (non-upgradable).
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Copyright (c) 2020 (original work) Open Assessment Technologies S.A.
*/
declare(strict_types=1);
namespace OAT\SimpleRoster\Command\Cache;
use Doctrine\ORM\EntityManagerInterface;
use OAT\SimpleRoster\Command\BlackfireProfilerTrait;
use OAT\SimpleRoster\Exception\DoctrineResultCacheImplementationNotFoundException;
use OAT\SimpleRoster\Repository\LtiInstanceRepository;
use Psr\Cache\CacheItemPoolInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Throwable;
class LtiInstanceCacheWarmerCommand extends Command
{
use BlackfireProfilerTrait;
public const NAME = 'roster:cache-warmup:lti-instance';
/** @var LtiInstanceRepository */
private LtiInstanceRepository $ltiInstanceRepository;
/** @var SymfonyStyle */
private SymfonyStyle $symfonyStyle;
private CacheItemPoolInterface $resultCache;
/** @var LoggerInterface */
private LoggerInterface $logger;
/** @var int */
private int $ltiInstancesCacheTtl;
public function __construct(
LtiInstanceRepository $ltiInstanceRepository,
EntityManagerInterface $entityManager,
LoggerInterface $cacheWarmupCacheWarmupLogger,
int $ltiInstancesCacheTtl
) {
$this->ltiInstanceRepository = $ltiInstanceRepository;
$this->logger = $cacheWarmupCacheWarmupLogger;
$this->ltiInstancesCacheTtl = $ltiInstancesCacheTtl;
$resultCacheImplementation = $entityManager->getConfiguration()->getResultCache();
if (!$resultCacheImplementation instanceof CacheItemPoolInterface) {
throw new DoctrineResultCacheImplementationNotFoundException(
'Doctrine result cache implementation is not configured.'
);
}
$this->resultCache = $resultCacheImplementation;
parent::__construct(self::NAME);
}
protected function configure(): void
{
parent::configure();
$this->addBlackfireProfilingOption();
$this->setDescription('LTI instance cache warmup');
}
protected function initialize(InputInterface $input, OutputInterface $output): void
{
$this->symfonyStyle = new SymfonyStyle($input, $output);
$this->symfonyStyle->title('Simple Roster - LTI Instance Cache Warmer');
}
/**
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
try {
$this->symfonyStyle->comment('Executing cache warmup...');
$this->resultCache->deleteItem(LtiInstanceRepository::CACHE_ID_ALL_LTI_INSTANCES);
// Refresh by query
$ltiInstances = $this->ltiInstanceRepository->findAllAsCollection();
if ($ltiInstances->isEmpty()) {
$this->symfonyStyle->warning('There are no LTI instances found in the database.');
return self::SUCCESS;
}
$this->logger->info(
sprintf('Result cache for %d LTI instances have been successfully warmed up.', count($ltiInstances)),
[
'cacheKey' => LtiInstanceRepository::CACHE_ID_ALL_LTI_INSTANCES,
'cacheTtl' => number_format($this->ltiInstancesCacheTtl),
]
);
$this->symfonyStyle->success(
sprintf(
'Result cache for %d LTI instances have been successfully warmed up. [TTL: %s seconds]',
count($ltiInstances),
number_format($this->ltiInstancesCacheTtl)
)
);
} catch (Throwable $exception) {
$this->symfonyStyle->error(sprintf('An unexpected error occurred: %s', $exception->getMessage()));
return self::FAILURE;
}
return self::SUCCESS;
}
}