From adad8cea817ba671152f8dd6ec7377d1698a3add Mon Sep 17 00:00:00 2001 From: Aldarien Date: Fri, 16 Jun 2023 21:44:35 -0400 Subject: [PATCH 1/6] v0.5.0 --- Dockerfile | 6 +++ app/bin/console | 3 ++ app/common/Command/UpdateIp.php | 36 ++++++++++++++++++ app/common/Command/Watch.php | 40 ++++++++++++++++++++ app/common/Service/Ipify.php | 27 ++++++++++++++ app/common/Service/Remote.php | 13 +++++++ app/common/Service/Repository.php | 41 ++++++++++++++++++++ app/common/Wrapper/App.php | 19 ++++++++++ app/composer.json | 22 +++++++++++ app/crontab | 1 + app/public/index.php | 7 ++++ app/setup/app.php | 40 ++++++++++++++++++++ app/setup/composer.php | 6 +++ app/setup/middlewares/commands.php | 3 ++ app/setup/settings/01_env.php | 19 ++++++++++ app/setup/setups/commands.php | 8 ++++ app/setup/setups/services.php | 60 ++++++++++++++++++++++++++++++ docker-compose.yml | 9 +++++ 18 files changed, 360 insertions(+) create mode 100644 Dockerfile create mode 100755 app/bin/console create mode 100644 app/common/Command/UpdateIp.php create mode 100644 app/common/Command/Watch.php create mode 100644 app/common/Service/Ipify.php create mode 100644 app/common/Service/Remote.php create mode 100644 app/common/Service/Repository.php create mode 100644 app/common/Wrapper/App.php create mode 100644 app/composer.json create mode 100644 app/crontab create mode 100644 app/public/index.php create mode 100644 app/setup/app.php create mode 100644 app/setup/composer.php create mode 100644 app/setup/middlewares/commands.php create mode 100644 app/setup/settings/01_env.php create mode 100644 app/setup/setups/commands.php create mode 100644 app/setup/setups/services.php create mode 100644 docker-compose.yml diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..861c6c2 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,6 @@ +FROM php:cli + +RUN apt-get update && apt-get install -yq --no-install-recommends cron && rm -r /var/lib/apt/lists/* \ + && docker-php-ext-install pdo_mysql + +CMD [ "cron", "-f", "-L", "15" ] diff --git a/app/bin/console b/app/bin/console new file mode 100755 index 0000000..b7e44fd --- /dev/null +++ b/app/bin/console @@ -0,0 +1,3 @@ +#!/bin/bash + +php /app/public/index.php "$@" diff --git a/app/common/Command/UpdateIp.php b/app/common/Command/UpdateIp.php new file mode 100644 index 0000000..4a98f8c --- /dev/null +++ b/app/common/Command/UpdateIp.php @@ -0,0 +1,36 @@ +service->update(); + return Command::SUCCESS; + } catch (PDOException $e) { + $this->logger->warning($e); + return Command::FAILURE; + } catch (Exception $e) { + $this->logger->error($e); + return Command::FAILURE; + } + } +} diff --git a/app/common/Command/Watch.php b/app/common/Command/Watch.php new file mode 100644 index 0000000..28f7c1f --- /dev/null +++ b/app/common/Command/Watch.php @@ -0,0 +1,40 @@ +period); + $current = new DateTimeImmutable(); + while(true) { + $now = new DateTimeImmutable(); + if ($now->diff($current) === $period) { + $this->runUpdate(); + $current = $now; + } + } + return Command::SUCCESS; + } + protected function runUpdate(): void + { + $command = '/app/bin/console update'; + shell_exec($command); + } +} diff --git a/app/common/Service/Ipify.php b/app/common/Service/Ipify.php new file mode 100644 index 0000000..fc415a6 --- /dev/null +++ b/app/common/Service/Ipify.php @@ -0,0 +1,27 @@ +logger->debug('Getting IP'); + $response = $this->client->get('?format=json'); + if (round($response->getCode() / 100, 0) !== 2) { + throw new Exception("Could not connect to '{$this->client->base_uri}'"); + } + $body = $response->getBody(); + $json = json_decode($body->getContents()); + if (!isset($json->ip)) { + throw new Exception('Missing `ip` in JSON response'); + } + return $json->ip; + } +} diff --git a/app/common/Service/Remote.php b/app/common/Service/Remote.php new file mode 100644 index 0000000..5a1131f --- /dev/null +++ b/app/common/Service/Remote.php @@ -0,0 +1,13 @@ +ipService->get(); + $this->dbService->update($ip); + } +} diff --git a/app/common/Service/Repository.php b/app/common/Service/Repository.php new file mode 100644 index 0000000..f358748 --- /dev/null +++ b/app/common/Service/Repository.php @@ -0,0 +1,41 @@ +logger->debug('Updating Database'); + + $old_ip = $this->getOld(); + $this->logger->debug($old_ip); + + if ($old_ip === $ip) { + $this->logger->debug('No change in IP'); + return; + } + + $this->doUpdate(); + $this->logger->debug('Updated IP'); + } + + protected function getOld(): string + { + $query = "SELECT `ip` FROM `{$this->table}` WHERE `host` = ?"; + $statement = $this->connection->prepare($query); + $statement->execute(['vialdelamaza']); + + return $statement->fetch()['ip']; + } + protected function doUpdate(string $ip): void + { + $query = "UPDATE `remote_ip` SET `ip` = ?, `updated` = CURRENT_TIMESTAMP() WHERE `host` = ?"; + $statement = $this->connection->prepare($query); + $statement->execute([$ip, 'vialdelamaza']); + } +} diff --git a/app/common/Wrapper/App.php b/app/common/Wrapper/App.php new file mode 100644 index 0000000..3c5107a --- /dev/null +++ b/app/common/Wrapper/App.php @@ -0,0 +1,19 @@ +container; + } + public function setContainer(ContainerInterface $container): App + { + $this->container = $container; + return $this; + } +} diff --git a/app/composer.json b/app/composer.json new file mode 100644 index 0000000..78aa08d --- /dev/null +++ b/app/composer.json @@ -0,0 +1,22 @@ +{ + "name": "provm/remote_ip", + "type": "project", + "require": { + "guzzlehttp/guzzle": "^7.7", + "monolog/monolog": "^3.3", + "php-di/php-di": "^7.0", + "symfony/console": "^6.3", + "thecodingmachine/safe": "^2.5" + }, + "require-dev": { + "phpunit/phpunit": "^10.2" + }, + "autoload": { + "psr-4": { + "ProVM\\": "common/" + } + }, + "config": { + "sort-packages": true + } +} diff --git a/app/crontab b/app/crontab new file mode 100644 index 0000000..388f26f --- /dev/null +++ b/app/crontab @@ -0,0 +1 @@ +*/20 * * * 1-5 /app/bin/console diff --git a/app/public/index.php b/app/public/index.php new file mode 100644 index 0000000..0606ece --- /dev/null +++ b/app/public/index.php @@ -0,0 +1,7 @@ +run(); diff --git a/app/setup/app.php b/app/setup/app.php new file mode 100644 index 0000000..b5df709 --- /dev/null +++ b/app/setup/app.php @@ -0,0 +1,40 @@ +isDir()) { + continue; + } + $builder->addDefinitions($file->getRealPath()); + } + } + $app = new App(); + $app->setContainer($builder->build()); + $folder = implode(DIRECTORY_SEPARATOR, [__DIR__, 'middlewares']); + if (file_exists($folder)) { + $files = new FilesystemIterator($folder); + foreach($files as $file) { + if ($file->isDir()) { + continue; + } + require_once $file->getRealPath(); + } + } + return $app; +} +return buildApp(); diff --git a/app/setup/composer.php b/app/setup/composer.php new file mode 100644 index 0000000..b451f96 --- /dev/null +++ b/app/setup/composer.php @@ -0,0 +1,6 @@ +add($app->getContainer()->get(ProVM\Command\Watch::class)); +$app->add($app->getContainer()->get(ProVM\Command\UpdateIp::class)); diff --git a/app/setup/settings/01_env.php b/app/setup/settings/01_env.php new file mode 100644 index 0000000..f991737 --- /dev/null +++ b/app/setup/settings/01_env.php @@ -0,0 +1,19 @@ + function() { + return new DI\Container([ + 'host' => $_ENV['MYSQL_HOST'], + 'name' => $_ENV['MYSQL_DATABASE'], + 'user' => function() { + return new DI\Container([ + 'name' => $_ENV['MYSQL_USER'], + 'password' => $_ENV['MYSQL_PASSWORD'] + ]); + }, + 'table' => 'remote_ip', + ]); + }, + 'uri' => 'https://api64.ipify.org', + 'period' => 'PT20M', + 'retries' => 5 +]; diff --git a/app/setup/setups/commands.php b/app/setup/setups/commands.php new file mode 100644 index 0000000..d4ee224 --- /dev/null +++ b/app/setup/setups/commands.php @@ -0,0 +1,8 @@ + function(ContainerInterface $container) { + return new ProVM\Command\Watch($container->get('period')); + } +]; diff --git a/app/setup/setups/services.php b/app/setup/setups/services.php new file mode 100644 index 0000000..9161c39 --- /dev/null +++ b/app/setup/setups/services.php @@ -0,0 +1,60 @@ + function(ContainerInterface $container) { + return new Monolog\Logger('file', [ + new Monolog\Handler\FilterHandler( + new Monolog\Handler\RotatingFileHandler('/var/log/remote.debug.log'), + Monolog\Level::Debug, + Monolog\Level::Warning + ), + new Monolog\Handler\FilterHandler( + new Monolog\Handler\RotatingFileHandler('/var/log/remote.error.log'), + Monolog\Level::Error + ) + ], [ + new Monolog\Processor\PsrLogMessageProcessor(), + new Monolog\Processor\IntrospectionProcessor(), + new Monolog\Processor\MemoryUsageProcessor(), + new Monolog\Processor\MemoryPeakUsageProcessor(), + ]); + }, + Psr\Http\Client\ClientInterface::class => function(ContainerInterface $container) { + return new GuzzleHttp\Client([ + 'base_uri' => $container->get('uri') + ]); + }, + ProVM\Service\Ipify::class => function(ContainerInterface $container) { + return new ProVM\Service\Ipify( + $container->get(Psr\Http\Client\ClientInterface::class), + $container->get(Psr\Log\LoggerInterface::class) + ); + }, + PDO::class => function(ContainerInterface $container) { + $database = $container->get('database'); + $retries = $container->get('retries'); + $r = 0; + $exception = null; + while($r < $retries) { + try { + $dsn = "mysql:host={$database->get('host')};dbname={$database->get('name')}"; + return new PDO($dsn, $database->get('user')->get('name'), $database->get('user')->get('password')); + } catch (PDOException $e) { + if ($exception !== null) { + $e = new PDOException($e->getMessage(), $e->getCode(), $exception); + } + $exception = $e; + $container->get(Psr\Log\LoggerInterface::class)->debug('Retrying Connection'); + } + } + throw $exception; + }, + ProVM\Service\Repository::class => function(ContainerInterface $container) { + return new ProVM\Service\Repository( + $container->get(PDO::class), + $container->get('database')->get('table'), + $container->get(Psr\Log\LoggerInterface::class) + ); + } +]; diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..e43fdf3 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,9 @@ +services: + remote_ip: + container_name: remote_ip + build: . + restart: unless-stopped + env_file: .env + volumes: + - ./app:/app + - ./app/crontab:/var/spool/cron/crontabs/root From 2231ba929690dac7e13a7fc707e4289ff1302466 Mon Sep 17 00:00:00 2001 From: Aldarien Date: Fri, 16 Jun 2023 21:44:55 -0400 Subject: [PATCH 2/6] Env file --- .env.sample | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 .env.sample diff --git a/.env.sample b/.env.sample new file mode 100644 index 0000000..7c4f64b --- /dev/null +++ b/.env.sample @@ -0,0 +1,4 @@ +MYSQL_HOST=provm.cl +MYSQL_DATABASE=incoviba +MYSQL_USER=remote_incoviba +MYSQL_PASSWORD=43918b603b84dd8bb29fd29a0ea21ba751de5dc90b26f36c From 53d58c31a7e4bb4807eab22b201553f57caa6b71 Mon Sep 17 00:00:00 2001 From: Aldarien Date: Fri, 16 Jun 2023 22:34:35 -0400 Subject: [PATCH 3/6] Ignore PHPStorm --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 6b7471f..2a46529 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ **/vendor/ **/*.env **/*.lock +**/.idea/ From 03d4c243a458a1bf80e320704d3bd99c50274786 Mon Sep 17 00:00:00 2001 From: Aldarien Date: Fri, 16 Jun 2023 22:34:59 -0400 Subject: [PATCH 4/6] Ignore logs --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 2a46529..aa7a937 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ **/*.env **/*.lock **/.idea/ +**/logs/ From 781858a90585313ca60457757c386985a42b3026 Mon Sep 17 00:00:00 2001 From: Aldarien Date: Fri, 16 Jun 2023 22:35:41 -0400 Subject: [PATCH 5/6] Fixes and cleanups --- Dockerfile | 6 ++++-- app/common/Command/UpdateIp.php | 2 +- app/common/Command/Watch.php | 3 ++- app/common/Service/Ipify.php | 6 +++--- app/setup/setups/services.php | 1 + 5 files changed, 11 insertions(+), 7 deletions(-) diff --git a/Dockerfile b/Dockerfile index 861c6c2..439dcaf 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,6 +1,8 @@ FROM php:cli -RUN apt-get update && apt-get install -yq --no-install-recommends cron && rm -r /var/lib/apt/lists/* \ - && docker-php-ext-install pdo_mysql +RUN apt-get update && apt-get install -yq --no-install-recommends cron && rm -r /var/lib/apt/lists/* +RUN docker-php-ext-install pdo_mysql CMD [ "cron", "-f", "-L", "15" ] +#ENTRYPOINT [ "/app/bin/console" ] +#CMD [ "/app/bin/console", "watch" ] diff --git a/app/common/Command/UpdateIp.php b/app/common/Command/UpdateIp.php index 4a98f8c..c6d3284 100644 --- a/app/common/Command/UpdateIp.php +++ b/app/common/Command/UpdateIp.php @@ -15,7 +15,7 @@ use ProVM\Service\Remote; )] class UpdateIp extends Command { - public function __construct(protected Remote $service, protected LoggerInterface $logger, ?string $name = 'update') + public function __construct(protected Remote $service, protected LoggerInterface $logger, string $name = 'update') { parent::__construct($name); } diff --git a/app/common/Command/Watch.php b/app/common/Command/Watch.php index 28f7c1f..5bf5e87 100644 --- a/app/common/Command/Watch.php +++ b/app/common/Command/Watch.php @@ -7,6 +7,7 @@ use Psr\Log\LoggerInterface; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; +use function Safe\shell_exec; #[AsCommand( name: 'watch', @@ -14,7 +15,7 @@ use Symfony\Component\Console\Output\OutputInterface; )] class Watch extends Command { - public function __construct(protected string $period, ?string $name = 'watch') + public function __construct(protected string $period, string $name = 'watch') { parent::__construct($name); } diff --git a/app/common/Service/Ipify.php b/app/common/Service/Ipify.php index fc415a6..1ed9d1a 100644 --- a/app/common/Service/Ipify.php +++ b/app/common/Service/Ipify.php @@ -8,14 +8,14 @@ use function Safe\json_decode; class Ipify { - public function __construct(protected ClientInterface $client, protected LoggerInterface $logger) {} + public function __construct(protected ClientInterface $client, protected string $uri, protected LoggerInterface $logger) {} public function get(): string { $this->logger->debug('Getting IP'); $response = $this->client->get('?format=json'); - if (round($response->getCode() / 100, 0) !== 2) { - throw new Exception("Could not connect to '{$this->client->base_uri}'"); + if (round($response->getStatusCode() / 100, 0) != 2) { + throw new Exception("Could not connect to '{$this->uri}'"); } $body = $response->getBody(); $json = json_decode($body->getContents()); diff --git a/app/setup/setups/services.php b/app/setup/setups/services.php index 9161c39..b7150fd 100644 --- a/app/setup/setups/services.php +++ b/app/setup/setups/services.php @@ -28,6 +28,7 @@ return [ ProVM\Service\Ipify::class => function(ContainerInterface $container) { return new ProVM\Service\Ipify( $container->get(Psr\Http\Client\ClientInterface::class), + $container->get('uri'), $container->get(Psr\Log\LoggerInterface::class) ); }, From 8d8eb84e20b938bf807314690892e578b3e036c5 Mon Sep 17 00:00:00 2001 From: Aldarien Date: Sun, 18 Jun 2023 19:20:06 -0400 Subject: [PATCH 6/6] Optimized connection to db --- Dockerfile | 5 +--- Prod.Dockerfile | 27 ++++++++++++++++++++ app/common/Command/UpdateIp.php | 6 +++++ app/common/Command/Watch.php | 13 ++++++++-- app/common/Service/Connector.php | 41 +++++++++++++++++++++++++++++++ app/common/Service/Ipify.php | 1 + app/common/Service/Repository.php | 10 ++++---- app/public/index.php | 8 +++++- app/setup/middlewares/log.php | 2 ++ app/setup/settings/01_env.php | 11 ++++++--- app/setup/setups/commands.php | 8 ++++-- app/setup/setups/services.php | 32 +++++++++--------------- docker-compose.yml | 3 ++- 13 files changed, 128 insertions(+), 39 deletions(-) create mode 100644 Prod.Dockerfile create mode 100644 app/common/Service/Connector.php create mode 100644 app/setup/middlewares/log.php diff --git a/Dockerfile b/Dockerfile index 439dcaf..786fbc3 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,8 +1,5 @@ FROM php:cli -RUN apt-get update && apt-get install -yq --no-install-recommends cron && rm -r /var/lib/apt/lists/* RUN docker-php-ext-install pdo_mysql -CMD [ "cron", "-f", "-L", "15" ] -#ENTRYPOINT [ "/app/bin/console" ] -#CMD [ "/app/bin/console", "watch" ] +WORKDIR /app diff --git a/Prod.Dockerfile b/Prod.Dockerfile new file mode 100644 index 0000000..70603ea --- /dev/null +++ b/Prod.Dockerfile @@ -0,0 +1,27 @@ +FROM php:cli AS build + +RUN apt-get update && apt-get install -yq --no-install-recommends git unzip && rm -r /var/lib/apt/lists/* +COPY --from=composer /usr/bin/composer /usr/bin/composer + +USER 1000 +WORKDIR /code +RUN git clone --branch master http://git.provm.cl/ProVM/remote_ip.git /code +RUN composer -d /code/app install + +FROM php:cli + +ENV MYSQL_HOST '' +ENV MYSQL_DATABASE '' +ENV MYSQL_USER '' +ENV MYSQL_PASSWORD '' + +RUN docker-php-ext-install pdo_mysql + +WORKDIR /app +COPY --from=build /code/app /app + +ENTRYPOINT [ "/app/bin/console" ] + +CMD [ "/app/bin/console", "watch" ] +#RUN apt-get update && apt-get install -yq --no-install-recommends cron && rm -r /var/lib/apt/lists/* && cp /app/crontab /var/spool/cron/crontabs/root +#CMD [ "cron", "-f", "-L", "15" ] diff --git a/app/common/Command/UpdateIp.php b/app/common/Command/UpdateIp.php index c6d3284..2128483 100644 --- a/app/common/Command/UpdateIp.php +++ b/app/common/Command/UpdateIp.php @@ -8,6 +8,7 @@ use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; use ProVM\Service\Remote; +use Symfony\Component\Console\Style\SymfonyStyle; #[AsCommand( name: 'update', @@ -22,8 +23,13 @@ class UpdateIp extends Command public function execute(InputInterface $input, OutputInterface $output): int { + $io = new SymfonyStyle($input, $output); + $io->title('Update IP'); + try { + $io->info('Obtaining IP and updating database'); $this->service->update(); + $io->info('Done'); return Command::SUCCESS; } catch (PDOException $e) { $this->logger->warning($e); diff --git a/app/common/Command/Watch.php b/app/common/Command/Watch.php index 5bf5e87..397bcbf 100644 --- a/app/common/Command/Watch.php +++ b/app/common/Command/Watch.php @@ -7,6 +7,7 @@ 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 function Safe\shell_exec; #[AsCommand( @@ -15,27 +16,35 @@ use function Safe\shell_exec; )] class Watch extends Command { - public function __construct(protected string $period, string $name = 'watch') + public function __construct(protected string $command, protected string $period, protected LoggerInterface $logger, string $name = 'watch') { parent::__construct($name); } public function execute(InputInterface $input, OutputInterface $output): int { + $io = new SymfonyStyle($input, $output); + $io->title('Watch'); + $period = new DateInterval($this->period); $current = new DateTimeImmutable(); + $io->info('Starting'); while(true) { $now = new DateTimeImmutable(); if ($now->diff($current) === $period) { + $io->info('Running Update'); $this->runUpdate(); $current = $now; } + $wait = (new DateTimeImmutable((new DateTimeImmutable())->format('Y-m-d H:i:0')))->add(new DateInterval('PT1M')); + time_sleep_until($wait->getTimestamp()); } return Command::SUCCESS; } protected function runUpdate(): void { - $command = '/app/bin/console update'; + $command = "{$this->command} update"; + $this->logger->info("Running '{$command}'"); shell_exec($command); } } diff --git a/app/common/Service/Connector.php b/app/common/Service/Connector.php new file mode 100644 index 0000000..7c8c6bc --- /dev/null +++ b/app/common/Service/Connector.php @@ -0,0 +1,41 @@ +connection)) { + $this->logger->debug('Connecting'); + $r = 0; + $exception = null; + while($r < $this->retries) { + try { + $dsn = "mysql:host={$this->host};dbname={$this->name}"; + $this->connection = new PDO($dsn, $this->username, $this->password); + return $this->connection; + } catch (PDOException $e) { + $this->logger->debug('Retrying'); + if ($exception !== null) { + $e = new PDOException($e->getMessage(), $e->getCode(), $exception); + } + $exception = $e; + } + $r ++; + } + throw $exception; + } + return $this->connection; + } +} diff --git a/app/common/Service/Ipify.php b/app/common/Service/Ipify.php index 1ed9d1a..2136521 100644 --- a/app/common/Service/Ipify.php +++ b/app/common/Service/Ipify.php @@ -22,6 +22,7 @@ class Ipify if (!isset($json->ip)) { throw new Exception('Missing `ip` in JSON response'); } + $this->logger->debug("Current IP: {$json->ip}"); return $json->ip; } } diff --git a/app/common/Service/Repository.php b/app/common/Service/Repository.php index f358748..7300a46 100644 --- a/app/common/Service/Repository.php +++ b/app/common/Service/Repository.php @@ -6,28 +6,28 @@ use Psr\Log\LoggerInterface; class Repository { - public function __construct(protected PDO $connection, protected string $table, protected LoggerInterface $logger) {} + public function __construct(protected Connector $connector, protected string $table, protected LoggerInterface $logger) {} public function update(string $ip): void { $this->logger->debug('Updating Database'); $old_ip = $this->getOld(); - $this->logger->debug($old_ip); + $this->logger->debug("Old IP: {$old_ip}"); if ($old_ip === $ip) { $this->logger->debug('No change in IP'); return; } - $this->doUpdate(); + $this->doUpdate($ip); $this->logger->debug('Updated IP'); } protected function getOld(): string { $query = "SELECT `ip` FROM `{$this->table}` WHERE `host` = ?"; - $statement = $this->connection->prepare($query); + $statement = $this->connector->connect()->prepare($query); $statement->execute(['vialdelamaza']); return $statement->fetch()['ip']; @@ -35,7 +35,7 @@ class Repository protected function doUpdate(string $ip): void { $query = "UPDATE `remote_ip` SET `ip` = ?, `updated` = CURRENT_TIMESTAMP() WHERE `host` = ?"; - $statement = $this->connection->prepare($query); + $statement = $this->connector->connect()->prepare($query); $statement->execute([$ip, 'vialdelamaza']); } } diff --git a/app/public/index.php b/app/public/index.php index 0606ece..cd4984c 100644 --- a/app/public/index.php +++ b/app/public/index.php @@ -4,4 +4,10 @@ $app = require_once implode(DIRECTORY_SEPARATOR, [ 'setup', 'app.php' ]); -$app->run(); +try { + $app->run(); +} catch (Exception $e) { + $app->getContainer()->get(Psr\Log\LoggerInterface::class)->warning($e); +} catch (Error $e) { + $app->getContainer()->get(Psr\Log\LoggerInterface::class)->error($e); +} diff --git a/app/setup/middlewares/log.php b/app/setup/middlewares/log.php new file mode 100644 index 0000000..b81e521 --- /dev/null +++ b/app/setup/middlewares/log.php @@ -0,0 +1,2 @@ +getContainer()->get(Psr\Log\LoggerInterface::class)); diff --git a/app/setup/settings/01_env.php b/app/setup/settings/01_env.php index f991737..6ceb90b 100644 --- a/app/setup/settings/01_env.php +++ b/app/setup/settings/01_env.php @@ -10,10 +10,13 @@ return [ 'password' => $_ENV['MYSQL_PASSWORD'] ]); }, - 'table' => 'remote_ip', + 'table' => $_ENV['MYSQL_TABLE'] ?? 'remote_ip', ]); }, - 'uri' => 'https://api64.ipify.org', - 'period' => 'PT20M', - 'retries' => 5 + 'error_logs_file' => $_ENV['ERROR_LOGS_FILE'] ?? '/logs/remote.error.log', + 'debug_logs_file' => $_ENV['DEBUG_LOGS_FILE'] ?? '/logs/remote.debug.log', + 'uri' => $_ENV['IPIFY_URI'] ?? 'https://api64.ipify.org', + 'command' => 'php /app/public/index.php', + 'period' => $_ENV['WATCH_PERIOD'] ?? 'PT20M', + 'retries' => $_ENV['CONNECTION_RETRIES'] ?? 5 ]; diff --git a/app/setup/setups/commands.php b/app/setup/setups/commands.php index d4ee224..d34ca97 100644 --- a/app/setup/setups/commands.php +++ b/app/setup/setups/commands.php @@ -3,6 +3,10 @@ use Psr\Container\ContainerInterface; return [ ProVM\Command\Watch::class => function(ContainerInterface $container) { - return new ProVM\Command\Watch($container->get('period')); - } + return new ProVM\Command\Watch( + $container->get('command'), + $container->get('period'), + $container->get(Psr\Log\LoggerInterface::class) + ); + }, ]; diff --git a/app/setup/setups/services.php b/app/setup/setups/services.php index b7150fd..d4e86a0 100644 --- a/app/setup/setups/services.php +++ b/app/setup/setups/services.php @@ -5,12 +5,12 @@ return [ Psr\Log\LoggerInterface::class => function(ContainerInterface $container) { return new Monolog\Logger('file', [ new Monolog\Handler\FilterHandler( - new Monolog\Handler\RotatingFileHandler('/var/log/remote.debug.log'), + new Monolog\Handler\RotatingFileHandler($container->get('debug_logs_file')), Monolog\Level::Debug, Monolog\Level::Warning ), new Monolog\Handler\FilterHandler( - new Monolog\Handler\RotatingFileHandler('/var/log/remote.error.log'), + new Monolog\Handler\RotatingFileHandler($container->get('error_logs_file')), Monolog\Level::Error ) ], [ @@ -32,28 +32,20 @@ return [ $container->get(Psr\Log\LoggerInterface::class) ); }, - PDO::class => function(ContainerInterface $container) { + ProVM\Service\Connector::class => function(ContainerInterface $container) { $database = $container->get('database'); - $retries = $container->get('retries'); - $r = 0; - $exception = null; - while($r < $retries) { - try { - $dsn = "mysql:host={$database->get('host')};dbname={$database->get('name')}"; - return new PDO($dsn, $database->get('user')->get('name'), $database->get('user')->get('password')); - } catch (PDOException $e) { - if ($exception !== null) { - $e = new PDOException($e->getMessage(), $e->getCode(), $exception); - } - $exception = $e; - $container->get(Psr\Log\LoggerInterface::class)->debug('Retrying Connection'); - } - } - throw $exception; + return new ProVM\Service\Connector( + $database->get('host'), + $database->get('name'), + $database->get('user')->get('name'), + $database->get('user')->get('password'), + $container->get('retries'), + $container->get(Psr\Log\LoggerInterface::class) + ); }, ProVM\Service\Repository::class => function(ContainerInterface $container) { return new ProVM\Service\Repository( - $container->get(PDO::class), + $container->get(ProVM\Service\Connector::class), $container->get('database')->get('table'), $container->get(Psr\Log\LoggerInterface::class) ); diff --git a/docker-compose.yml b/docker-compose.yml index e43fdf3..f6d5a14 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -6,4 +6,5 @@ services: env_file: .env volumes: - ./app:/app - - ./app/crontab:/var/spool/cron/crontabs/root + - ./logs:/logs +# - ./app/crontab:/var/spool/cron/crontabs/root