diff --git a/.env.sample b/.env.sample index 7c4f64b..7e3e670 100644 --- a/.env.sample +++ b/.env.sample @@ -2,3 +2,12 @@ MYSQL_HOST=provm.cl MYSQL_DATABASE=incoviba MYSQL_USER=remote_incoviba MYSQL_PASSWORD=43918b603b84dd8bb29fd29a0ea21ba751de5dc90b26f36c +MYSQL_TABLE=remote_ip + +ERROR_LOGS_FILE=php://stderr +DEBUG_LOGS_FILE=php://stdout + +IPIFY_URI=https://api64.ipify.org +COMMAND=/app/bin/console +WATCH_PERIOD=PT20M +CONNECTION_RETRIES=5 diff --git a/Dockerfile b/Dockerfile index 786fbc3..0b4d744 100644 --- a/Dockerfile +++ b/Dockerfile @@ -3,3 +3,8 @@ FROM php:cli RUN docker-php-ext-install pdo_mysql WORKDIR /app +COPY ./app /app +RUN chmod a+x /app/bin/console + +#ENTRYPOINT [ "/app/bin/console" ] +CMD [ "/app/bin/console", "watch" ] diff --git a/Prod.Dockerfile b/Prod.Dockerfile index 70603ea..9cf7926 100644 --- a/Prod.Dockerfile +++ b/Prod.Dockerfile @@ -20,8 +20,6 @@ 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/Watch.php b/app/common/Command/Watch.php index 397bcbf..1c66843 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\StyleInterface; use Symfony\Component\Console\Style\SymfonyStyle; use function Safe\shell_exec; @@ -27,24 +28,37 @@ class Watch extends Command $io->title('Watch'); $period = new DateInterval($this->period); - $current = new DateTimeImmutable(); + $current = $this->updateTime($period); $io->info('Starting'); + $io->info("Start: {$current->format('Y-m-d H:i:s')}"); 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')); + $io->info('Running Update'); + $this->runUpdate($io); + $current = $this->updateTime($period); + $wait = $current->add($period); + $io->text("Waiting until {$wait->format('Y-m-d H:i:s')}"); time_sleep_until($wait->getTimestamp()); } return Command::SUCCESS; } - protected function runUpdate(): void + protected function updateTime(DateInterval $interval): DateTimeImmutable + { + $t0 = new DateTimeImmutable((new DateTimeImmutable())->format('Y-m-d 00:00:00')); + $tf = new DateTimeImmutable((new DateTimeImmutable())->add(new DateInterval('P1D'))->format('Y-m-d 00:00:00')); + $now = new DateTimeImmutable(); + for ($t = $t0; $t < $tf; $t = $t->add($interval)) { + $t1 = $t->add($interval); + if ($t < $now and $now < $t1) { + return $t; + } + } + return $now; + } + protected function runUpdate(StyleInterface $io): void { $command = "{$this->command} update"; $this->logger->info("Running '{$command}'"); - shell_exec($command); + $result = shell_exec($command); + $io->note($result); } } diff --git a/app/setup/settings/01_env.php b/app/setup/settings/01_env.php index 6ceb90b..b87a28b 100644 --- a/app/setup/settings/01_env.php +++ b/app/setup/settings/01_env.php @@ -16,7 +16,7 @@ return [ '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', + 'command' => $_ENV['COMMAND'] ?? 'php /app/public/index.php', 'period' => $_ENV['WATCH_PERIOD'] ?? 'PT20M', 'retries' => $_ENV['CONNECTION_RETRIES'] ?? 5 ]; diff --git a/docker-compose.yml b/docker-compose.yml index f6d5a14..f82ad6c 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -7,4 +7,3 @@ services: volumes: - ./app:/app - ./logs:/logs -# - ./app/crontab:/var/spool/cron/crontabs/root diff --git a/prod.docker-compose.yml b/prod.docker-compose.yml new file mode 100644 index 0000000..7c874ca --- /dev/null +++ b/prod.docker-compose.yml @@ -0,0 +1,6 @@ +services: + remote_ip: + container_name: remote_ip + build: ./Prod.Dockerfile + restart: unless-stopped + env_file: .env