Bring Redis back for app and Centrifugo caches.

This commit is contained in:
Buster Neece 2022-12-06 12:52:54 -06:00
parent c39c366f17
commit 75d1967957
No known key found for this signature in database
GPG Key ID: F1D2E64A0005E80E
12 changed files with 87 additions and 73 deletions

View File

@ -6,17 +6,16 @@ declare(strict_types=1);
error_reporting(E_ALL & ~E_NOTICE & ~E_STRICT);
ini_set('display_errors', '1');
class Spinner
final class Spinner
{
protected array $frames = [];
private readonly int $length;
protected int $length;
private int $current = 0;
protected int $current = 0;
public function __construct(array $frames)
public function __construct(
private readonly array $frames
)
{
$this->frames = $frames;
$this->length = count($this->frames);
}
@ -41,15 +40,15 @@ class Spinner
}
}
class UptimeWait
final class UptimeWait
{
protected Spinner $spinner;
private Spinner $spinner;
protected int $timeout = 180;
private int $timeout = 180;
protected int $retryInterval = 1;
private int $retryInterval = 1;
protected bool $debugMode = false;
private bool $debugMode = false;
public function __construct()
{
@ -89,7 +88,7 @@ class UptimeWait
die(1);
}
protected function checkDatabase(): bool
private function checkDatabase(): bool
{
try {
$dbOptions = [
@ -119,7 +118,7 @@ class UptimeWait
}
}
protected function checkRedis(): bool
private function checkRedis(): bool
{
$enableRedis = $this->envToBool($_ENV['ENABLE_REDIS'] ?? true);
$redisHost = $_ENV['REDIS_HOST'] ?? 'localhost';
@ -156,7 +155,7 @@ class UptimeWait
}
}
protected function envToBool(string|bool $value): bool
private function envToBool(string|bool $value): bool
{
if (is_bool($value)) {
return $value;
@ -167,11 +166,11 @@ class UptimeWait
|| '1' === $value;
}
protected function println(string $line): void
private function println(string $line): void
{
echo $line . "\n";
}
}
$uptimeWait = new UptimeWait;
$uptimeWait = new UptimeWait();
$uptimeWait->run();

View File

@ -11,15 +11,11 @@ use Psr\Container\ContainerInterface;
return [
// Slim interface
Slim\Interfaces\RouteCollectorInterface::class => static function (Slim\App $app) {
return $app->getRouteCollector();
},
Slim\Interfaces\RouteCollectorInterface::class => static fn(Slim\App $app) => $app->getRouteCollector(),
Slim\Interfaces\RouteParserInterface::class => static function (
Slim\Interfaces\RouteParserInterface::class => static fn(
Slim\Interfaces\RouteCollectorInterface $routeCollector
) {
return $routeCollector->getRouteParser();
},
) => $routeCollector->getRouteParser(),
// URL Router helper
App\Http\RouterInterface::class => DI\Get(App\Http\Router::class),
@ -453,28 +449,22 @@ return [
);
},
Symfony\Component\Mailer\Mailer::class => static function (
Symfony\Component\Mailer\Mailer::class => static fn(
Symfony\Component\Mailer\Transport\TransportInterface $transport,
Symfony\Component\Messenger\MessageBus $messageBus,
Psr\EventDispatcher\EventDispatcherInterface $eventDispatcher
) {
return new Symfony\Component\Mailer\Mailer(
$transport,
$messageBus,
$eventDispatcher
);
},
) => new Symfony\Component\Mailer\Mailer($transport, $messageBus, $eventDispatcher),
Symfony\Component\Mailer\MailerInterface::class => DI\get(
Symfony\Component\Mailer\Mailer::class
),
// Supervisor manager
Supervisor\SupervisorInterface::class => static function (
Supervisor\SupervisorInterface::class => static fn(
Environment $environment,
Psr\Log\LoggerInterface $logger
) {
$client = new fXmlRpc\Client(
) => new Supervisor\Supervisor(
new fXmlRpc\Client(
'http://localhost/RPC2',
new fXmlRpc\Transport\PsrTransport(
new GuzzleHttp\Psr7\HttpFactory(),
@ -484,10 +474,9 @@ return [
],
])
)
);
return new Supervisor\Supervisor($client, $logger);
},
),
$logger
),
// NowPlaying Adapter factory
NowPlaying\AdapterFactory::class => static function (

View File

@ -5,6 +5,7 @@ services:
ports:
- "127.0.0.1:3306:3306" # MariaDB
- "127.0.0.1:6025:6025" # Centrifugo
- "127.0.0.1:6379:6379" # Redis
volumes:
- $PWD/util/local_ssl/default.crt:/var/azuracast/acme/ssl.crt:ro
- $PWD/util/local_ssl/default.key:/var/azuracast/acme/ssl.key:ro

View File

@ -201,7 +201,10 @@ class Settings implements Stringable
}
#[
OA\Property(description: "Whether to use high-performance static JSON for Now Playing data updates.", example: "false"),
OA\Property(
description: "Whether to use high-performance static JSON for Now Playing data updates.",
example: "false"
),
ORM\Column,
Groups(self::GROUP_GENERAL)
]

View File

@ -300,6 +300,11 @@ final class Environment
return $dbSettings;
}
public function useLocalDatabase(): bool
{
return 'localhost' === ($this->data[self::DB_HOST] ?? 'localhost');
}
public function enableRedis(): bool
{
return self::envToBool($this->data[self::ENABLE_REDIS] ?? true);
@ -323,6 +328,11 @@ final class Environment
return $redisSettings;
}
public function useLocalRedis(): bool
{
return $this->enableRedis() && 'localhost' === ($this->data[self::REDIS_HOST] ?? 'localhost');
}
public function isProfilingExtensionEnabled(): bool
{
return self::envToBool($this->data[self::PROFILING_EXTENSION_ENABLED] ?? false);

View File

@ -4,6 +4,7 @@ declare(strict_types=1);
namespace App\Service;
use App\Environment;
use App\Exception\SupervisorException;
use App\Service\ServiceControl\ServiceData;
use Supervisor\Exception\Fault\BadNameException;
@ -26,7 +27,7 @@ final class ServiceControl
{
$services = [];
foreach (self::getServiceNames() as $name => $description) {
foreach ($this->getServiceNames() as $name => $description) {
try {
$isRunning = in_array(
$this->supervisor->getProcess($name)->getState(),
@ -52,7 +53,7 @@ final class ServiceControl
public function restart(string $service): void
{
$serviceNames = self::getServiceNames();
$serviceNames = $this->getServiceNames();
if (!isset($serviceNames[$service])) {
throw new \InvalidArgumentException(
sprintf('Service "%s" is not managed by AzuraCast.', $service)
@ -71,9 +72,9 @@ final class ServiceControl
}
}
public static function getServiceNames(): array
public function getServiceNames(): array
{
return [
$services = [
'beanstalkd' => __('Message queue delivery service'),
'cron' => __('Runs routine synchronized tasks'),
'mariadb' => __('Database'),
@ -94,6 +95,10 @@ final class ServiceControl
unset($services['mariadb']);
}
if (!$this->environment->useLocalRedis()) {
unset($services['redis']);
}
return $services;
}
}

View File

@ -1,7 +1,7 @@
unixsocket /run/redis/redis.sock
unixsocketperm 666
bind 127.0.0.1
bind 0.0.0.0
port 6379
save ""

View File

@ -1,26 +0,0 @@
{
"allow_anonymous_connect_without_token": true,
"api_insecure": true,
"admin": true,
"admin_insecure": true,
"port": 6020,
"internal_port": 6025,
"websocket_disable": true,
"uni_websocket": true,
"uni_sse": true,
"uni_http_stream": true,
"allowed_origins": [
"*"
],
"namespaces": [
{
"name": "station",
"history_size": 1,
"history_ttl": "30s",
"allow_subscribe_for_client": true,
"allow_subscribe_for_anonymous": true,
"allow_history_for_client": true,
"allow_history_for_anonymous": true
}
]
}

View File

@ -0,0 +1,27 @@
allow_anonymous_connect_without_token: true
api_insecure: true
admin: true
admin_insecure: true
port: 6020
internal_port: 6025
websocket_disable: true
uni_websocket: true
uni_sse: true
uni_http_stream: true
allowed_origins:
- "*"
namespaces:
- name: "station"
history_size: 1
history_ttl: "30s"
allow_subscribe_for_client: true
allow_subscribe_for_anonymous: true
allow_history_for_client: true
allow_history_for_anonymous: true
{{if isTrue .Env.ENABLE_REDIS }}
engine: "redis"
redis_address: "{{ .Env.REDIS_HOST }}:{{ default .Env.REDIS_PORT "6379" }}"
redis_db: 0
{{end}}

View File

@ -1,5 +1,5 @@
[program:centrifugo]
command=centrifugo -c /var/azuracast/centrifugo/config.json
command=centrifugo -c /var/azuracast/centrifugo/config.yaml
dir=/var/azuracast/centrifugo
user=azuracast
priority=700

View File

@ -3,5 +3,5 @@ set -e
set -x
mkdir -p /var/azuracast/centrifugo
cp /bd_build/web/centrifugo/config.json /var/azuracast/centrifugo/config.json
cp /bd_build/web/centrifugo/config.yaml.tmpl /var/azuracast/centrifugo/config.yaml.tmpl

View File

@ -0,0 +1,6 @@
#!/bin/bash
ENABLE_REDIS=${ENABLE_REDIS:-true}
export ENABLE_REDIS
dockerize -template "/var/azuracast/centrifugo/config.yaml.tmpl:/var/azuracast/centrifugo/config.yaml"