Add cleanup after request for RoadRunner

This commit is contained in:
Kacper Donat 2021-04-21 21:29:07 +02:00
parent f9ee00b29b
commit 64f1621e04
10 changed files with 163 additions and 15 deletions

View File

@ -0,0 +1,31 @@
server {
root /var/www/front/public/;
server_name cojedzie.localhost;
location /_profiler/ {
try_files $uri $uri/ @api;
}
location /bundles/ {
try_files $uri $uri/ @api;
}
location /api/ {
try_files $uri $uri/ @api;
}
location / {
try_files $uri $uri/ @frontend;
}
location @frontend {
proxy_pass http://frontend:3000;
proxy_intercept_errors on;
}
location @api {
proxy_pass http://api:8080;
proxy_intercept_errors on;
}
}

View File

@ -1,7 +1,5 @@
FROM php:7.4-fpm-alpine
ENV DATABASE_URL="sqlite:///var/db/app.db"
COPY --from=mlocati/php-extension-installer /usr/bin/install-php-extensions /usr/local/bin/
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer

29
api/rr.Dockerfile Normal file
View File

@ -0,0 +1,29 @@
FROM cojedzie/api:latest-rr
COPY --from=mlocati/php-extension-installer /usr/bin/install-php-extensions /usr/local/bin/
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
RUN install-php-extensions xdebug-^3.0;
RUN apk add git;
# XDebug
RUN echo "xdebug.mode=debug" >> $PHP_INI_DIR/conf.d/docker-php-ext-xdebug.ini && \
echo "xdebug.client_host=172.17.0.1" >> $PHP_INI_DIR/conf.d/docker-php-ext-xdebug.ini && \
echo "xdebug.start_with_request=On" >> $PHP_INI_DIR/conf.d/docker-php-ext-xdebug.ini;
# Blackfire
RUN version=$(php -r "echo PHP_MAJOR_VERSION.PHP_MINOR_VERSION;") \
&& curl -A "Docker" -o /tmp/blackfire-probe.tar.gz -D - -L -s https://blackfire.io/api/v1/releases/probe/php/linux/amd64/$version \
&& mkdir -p /tmp/blackfire \
&& tar zxpf /tmp/blackfire-probe.tar.gz -C /tmp/blackfire \
&& mv /tmp/blackfire/blackfire-*.so $(php -r "echo ini_get ('extension_dir');")/blackfire.so \
&& printf "extension=blackfire.so\nblackfire.agent_socket=tcp://blackfire:8707\n" > $PHP_INI_DIR/conf.d/blackfire.ini \
&& rm -rf /tmp/blackfire /tmp/blackfire-probe.tar.gz
# Timezone
RUN ln -snf /usr/share/zoneinfo/Europe/Warsaw /etc/localtime && \
echo "date.timezone = Europe/Warsaw" >> /usr/local/etc/php/conf.d/datetime.ini;
WORKDIR /var/www
EXPOSE 9001

View File

@ -29,7 +29,7 @@ class WithDestinationsDatabaseHandler implements PostProcessingHandler
if ($this->converter instanceof CacheableConverter) {
$this->converter = clone $this->converter;
$this->converter->flushCache();
$this->converter->reset();
}
}

View File

@ -28,11 +28,11 @@ class ZtmGdanskProvider implements Provider
private $stops;
private $tracks;
private $messages;
/** @var ProviderEntity */
private $entity;
private $trips;
private ProviderEntity $entity;
private EntityManagerInterface $em;
public function getName(): string
{
return 'MZKZG - Trójmiasto';
@ -68,7 +68,9 @@ class ZtmGdanskProvider implements Provider
ZtmGdanskMessageRepository $messages,
ReferenceFactory $referenceFactory
) {
$provider = $em->getReference(ProviderEntity::class, $this->getIdentifier());
$this->em = $em;
$provider = $this->refreshProviderEntity();
$lines = $lines->withProvider($provider);
$stops = $stops->withProvider($provider);
@ -117,6 +119,13 @@ class ZtmGdanskProvider implements Provider
public function getLastUpdate(): ?Carbon
{
$this->refreshProviderEntity();
return $this->entity->getUpdateDate();
}
private function refreshProviderEntity(): ProviderEntity
{
return $this->entity = $this->em->getReference(ProviderEntity::class, $this->getIdentifier());
}
}

View File

@ -45,7 +45,7 @@ class AggregateConverter implements Converter, CacheableConverter
return clone $this->cachedConverters;
}
public function flushCache()
public function reset()
{
$this->ensureCachedConverters();
@ -53,7 +53,7 @@ class AggregateConverter implements Converter, CacheableConverter
->cachedConverters
->filter(instance(CacheableConverter::class))
->each(function (CacheableConverter $converter) {
$converter->flushCache();
$converter->reset();
})
;
}

View File

@ -2,7 +2,8 @@
namespace App\Service;
interface CacheableConverter extends Converter
use Symfony\Contracts\Service\ResetInterface;
interface CacheableConverter extends Converter, ResetInterface
{
public function flushCache();
}

View File

@ -2,14 +2,12 @@
namespace App\Service;
use App\Entity\{Entity, LineEntity, OperatorEntity, StopEntity, TrackEntity, TripEntity, TripStopEntity};
use App\Entity\{Entity, LineEntity, OperatorEntity, StopEntity, TrackEntity, TripEntity};
use App\Model\{Line, Location, Operator, ScheduledStop, Stop, Track, Trip};
use App\Service\Proxy\ReferenceFactory;
use Doctrine\ORM\PersistentCollection;
use Doctrine\ORM\Proxy\Proxy;
use Kadet\Functional as f;
use Kadet\Functional\Transforms as t;
use const Kadet\Functional\_;
final class EntityConverter implements Converter, RecursiveConverter, CacheableConverter
{
@ -177,7 +175,7 @@ final class EntityConverter implements Converter, RecursiveConverter, CacheableC
return $entity instanceof Entity;
}
public function flushCache()
public function reset()
{
$this->cache = [];
}

View File

@ -0,0 +1,39 @@
<?php
namespace App\Subscriber;
use App\Service\Converter;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\TerminateEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Contracts\Service\ResetInterface;
class RequestCleanupSubscriber implements EventSubscriberInterface
{
/** @var ContainerInterface */
private ContainerInterface $container;
private Converter $converter;
public function __construct(ContainerInterface $container, Converter $converter)
{
$this->container = $container;
$this->converter = $converter;
}
public static function getSubscribedEvents()
{
return [
KernelEvents::TERMINATE => ['onTerminate']
];
}
public function onTerminate(TerminateEvent $event)
{
$this->container->get('doctrine')->reset();
if ($this->converter instanceof ResetInterface) {
$this->converter->reset();
}
}
}

43
docker-compose.rr.yml Normal file
View File

@ -0,0 +1,43 @@
version: '3.4'
services:
nginx:
image: nginx:latest
ports:
- "8080:80"
volumes:
- ./front:/var/www/front:cached
- ./api:/var/www/api:cached
- .docker-compose/nginx/cojedzie-rr.conf:/etc/nginx/conf.d/cojedzie-rr.conf
api:
build:
context: ./api
dockerfile: rr.Dockerfile
env_file:
- .docker-compose/api/.env
- api/.env.local
environment:
- TRUSTED_PROXIES=172.0.0.0/8
ports:
- 8888:8080
volumes:
- ./api:/var/www:cached
- .docker-compose/api/log.conf:/usr/local/etc/php-fpm.d/zz-log.conf
command: ["rr", "serve", "-c", ".rr.yaml"]
frontend:
image: node:15.2.1
working_dir: /app
environment:
- APP_API=http://nginx/api
volumes:
- ./front:/app
command: ["node", "build/server.js"]
blackfire:
image: blackfire/blackfire
ports: ["8707"]
environment:
- BLACKFIRE_SERVER_ID
- BLACKFIRE_SERVER_TOKEN