Add profiler helper notifications and optimize cache usage.

This commit is contained in:
Buster "Silver Eagle" Neece 2021-04-18 00:03:22 -05:00
parent 8f900af32f
commit d734b8da96
No known key found for this signature in database
GPG Key ID: 6D9E12FF03411F4E
7 changed files with 146 additions and 6 deletions

View File

@ -58,7 +58,9 @@ ENV LANG="en_US.UTF-8" \
COMPOSER_PLUGIN_MODE="false" \
ADDITIONAL_MEDIA_SYNC_WORKER_COUNT=0 \
PROFILING_EXTENSION_ENABLED=0 \
PROFILING_EXTENSION_ALWAYS_ON=0
PROFILING_EXTENSION_ALWAYS_ON=0 \
PROFILING_EXTENSION_HTTP_KEY=dev \
PROFILING_EXTENSION_HTTP_IP_WHITELIST=127.0.0.1
# Entrypoint and default command
ENTRYPOINT ["/usr/local/bin/uptime_wait"]

View File

@ -150,6 +150,10 @@ return function (App\EventDispatcher $dispatcher) {
Event\GetNotifications::class,
App\Notification\Check\SyncTaskCheck::class
);
$dispatcher->addCallableListener(
Event\GetNotifications::class,
App\Notification\Check\ProfilerAdvisorCheck::class
);
$dispatcher->addCallableListener(
Event\Media\GetAlbumArt::class,

View File

@ -150,14 +150,22 @@ return [
Psr\Log\LoggerInterface $logger,
ContainerInterface $di
) {
$arrayAdapter = new Symfony\Component\Cache\Adapter\ArrayAdapter();
$arrayAdapter->setLogger($logger);
if ($environment->isTesting()) {
$adapter = new Symfony\Component\Cache\Adapter\ArrayAdapter();
} else {
$adapter = new Symfony\Component\Cache\Adapter\RedisAdapter($di->get(Redis::class));
return $arrayAdapter;
}
$adapter->setLogger($logger);
return $adapter;
$redisAdapter = new Symfony\Component\Cache\Adapter\RedisAdapter($di->get(Redis::class));
$redisAdapter->setLogger($logger);
return new Symfony\Component\Cache\Adapter\ChainAdapter(
[
$arrayAdapter,
$redisAdapter,
]
);
},
Psr\Cache\CacheItemPoolInterface::class => DI\get(

View File

@ -9,6 +9,7 @@ use App\EventDispatcher;
use App\MessageQueue\LogWorkerExceptionSubscriber;
use App\MessageQueue\QueueManager;
use App\MessageQueue\ReloadSettingsMiddleware;
use App\MessageQueue\ResetArrayCacheMiddleware;
use Psr\Log\LoggerInterface;
use Symfony\Component\Messenger\EventListener\StopWorkerOnTimeLimitListener;
use Symfony\Component\Messenger\MessageBus;
@ -42,6 +43,7 @@ class ProcessCommand extends CommandAbstract
$eventDispatcher->addServiceSubscriber(ClearEntityManagerSubscriber::class);
$eventDispatcher->addServiceSubscriber(LogWorkerExceptionSubscriber::class);
$eventDispatcher->addServiceSubscriber(ReloadSettingsMiddleware::class);
$eventDispatcher->addServiceSubscriber(ResetArrayCacheMiddleware::class);
if ($runtime <= 0) {
$runtime = $environment->isProduction()

View File

@ -48,6 +48,10 @@ class Environment
public const LOG_LEVEL = 'LOG_LEVEL';
public const PROFILING_EXTENSION_ENABLED = 'PROFILING_EXTENSION_ENABLED';
public const PROFILING_EXTENSION_ALWAYS_ON = 'PROFILING_EXTENSION_ALWAYS_ON';
public const PROFILING_EXTENSION_HTTP_KEY = 'PROFILING_EXTENSION_HTTP_KEY';
// Database and Cache Configuration Variables
public const DB_HOST = 'MYSQL_HOST';
public const DB_PORT = 'MYSQL_PORT';
@ -290,4 +294,19 @@ class Environment
'db' => (int)($this->data[self::REDIS_DB] ?? 1),
];
}
public function isProfilingExtensionEnabled(): bool
{
return (1 === (int)($this->data[self::PROFILING_EXTENSION_ENABLED] ?? 0));
}
public function isProfilingExtensionAlwaysOn(): bool
{
return (1 === (int)($this->data[self::PROFILING_EXTENSION_ALWAYS_ON] ?? 0));
}
public function getProfilingExtensionHttpKey(): string
{
return $this->data[self::PROFILING_EXTENSION_HTTP_KEY] ?? 'dev';
}
}

View File

@ -0,0 +1,37 @@
<?php
namespace App\MessageQueue;
use Symfony\Component\Cache\ResettableInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Messenger\Event\WorkerMessageReceivedEvent;
use Symfony\Contracts\Cache\CacheInterface;
class ResetArrayCacheMiddleware implements EventSubscriberInterface
{
protected CacheInterface $cache;
public function __construct(CacheInterface $cache)
{
$this->cache = $cache;
}
/**
* @inheritDoc
*/
public static function getSubscribedEvents()
{
return [
WorkerMessageReceivedEvent::class => [
['resetArrayCache', -100],
],
];
}
public function resetArrayCache(WorkerMessageReceivedEvent $event): void
{
if ($this->cache instanceof ResettableInterface) {
$this->cache->reset();
}
}
}

View File

@ -0,0 +1,68 @@
<?php
namespace App\Notification\Check;
use App\Acl;
use App\Entity\Api\Notification;
use App\Environment;
use App\Event\GetNotifications;
use App\Session\Flash;
class ProfilerAdvisorCheck
{
protected Environment $environment;
public function __construct(Environment $environment)
{
$this->environment = $environment;
}
public function __invoke(GetNotifications $event): void
{
// This notification is for full administrators only.
$request = $event->getRequest();
$acl = $request->getAcl();
if (!$acl->isAllowed(Acl::GLOBAL_ALL)) {
return;
}
if (!$this->environment->isDocker()) {
return;
}
$profilerIsEnabled = $this->environment->isProfilingExtensionEnabled();
if (!$profilerIsEnabled) {
return;
}
$notification = new Notification();
$notification->title = __('The performance profiling extension is currently enabled on this installation.');
$notification->body = __(
'You can track the execution time and memory usage of any AzuraCast page or application ' .
'from the profiler page.',
);
$notification->type = Flash::INFO;
$notification->actionLabel = __('Profiler Control Panel');
$notification->actionUrl = '/?' . http_build_query(
[
'SPX_UI_URI' => '/',
'SPX_KEY' => $this->environment->getProfilingExtensionHttpKey(),
]
);
$event->addNotification($notification);
if ($this->environment->isProfilingExtensionAlwaysOn()) {
$notification = new Notification();
$notification->title = __('Performance profiling is currently enabled for all requests.');
$notification->body = __(
'This can have an adverse impact on system performance. ' .
'You should disable this when possible.'
);
$notification->type = Flash::WARNING;
$event->addNotification($notification);
}
}
}