diff --git a/compose.yml b/compose.yml index 55efea8..1489d2e 100755 --- a/compose.yml +++ b/compose.yml @@ -33,12 +33,11 @@ services: worker: -# build: -# dockerfile: docker/Dockerfile.base.worker -# context: . -# args: -# FRANKENPHP_TAG: php8.4-alpine - image: torsearch-alpine + build: + dockerfile: docker/Dockerfile.worker + context: . + args: + APP_VERSION: latest restart: unless-stopped volumes: - $PWD:/app @@ -46,22 +45,22 @@ services: tty: true environment: TZ: America/Chicago -# command: php /app/bin/console messenger:consume async --time-limit=3600 -vv - + command: --download 2 --async scheduler: build: - dockerfile: docker/Dockerfile.base.worker + dockerfile: docker/Dockerfile.worker context: . args: - FRANKENPHP_TAG: php8.4-alpine + APP_VERSION: latest restart: unless-stopped volumes: - $PWD:/app + - $PWD/var/download:/var/download + tty: true environment: TZ: America/Chicago - command: php /app/bin/console messenger:consume scheduler_monitor -vv - tty: true + WORKER_MONITOR: 2 redis: diff --git a/docker/Dockerfile.worker b/docker/Dockerfile.worker index 03576c7..4a80979 100644 --- a/docker/Dockerfile.worker +++ b/docker/Dockerfile.worker @@ -34,7 +34,8 @@ RUN apk add --no-cache \ RUN ln -s /usr/bin/php84 /usr/bin/php COPY docker/worker/supervisord.conf /etc/supervisor/conf.d/supervisord.conf +COPY docker/ app/docker # We start supervisord and supervisord starts # respective service in the configuration file. -CMD ["/usr/bin/supervisord", "-c", "/etc/supervisor/conf.d/supervisord.conf"] +ENTRYPOINT ["/app/bin/console", "init:worker"] diff --git a/docker/worker/async.conf b/docker/worker/async.conf new file mode 100644 index 0000000..10c120c --- /dev/null +++ b/docker/worker/async.conf @@ -0,0 +1,10 @@ +[program:torsearch-worker] +command=/app/bin/console messenger:consume async -vv --time-limit 3600 +numprocs=1 +stdout_logfile=/dev/stdout +stdout_logfile_maxbytes=0 +stderr_logfile=/dev/stderr +stderr_logfile_maxbytes=0 +autorestart=true +startretries=3 +process_name=%(program_name)s_%(process_num)02d diff --git a/docker/worker/download.conf b/docker/worker/download.conf new file mode 100644 index 0000000..25e7736 --- /dev/null +++ b/docker/worker/download.conf @@ -0,0 +1,10 @@ +[program:torsearch-downloader] +command=/app/bin/console messenger:consume download -vv --time-limit 3600 +numprocs=1 +stdout_logfile=/dev/stdout +stdout_logfile_maxbytes=0 +stderr_logfile=/dev/stderr +stderr_logfile_maxbytes=0 +autorestart=true +startretries=3 +process_name=%(program_name)s_%(process_num)02d \ No newline at end of file diff --git a/docker/worker/monitor.conf b/docker/worker/monitor.conf new file mode 100644 index 0000000..e9e11c3 --- /dev/null +++ b/docker/worker/monitor.conf @@ -0,0 +1,10 @@ +[program:torsearch-scheduler] +command=/app/bin/console messenger:consume scheduler_monitor monitor -vv --time-limit 21600 +numprocs=1 +stdout_logfile=/dev/stdout +stdout_logfile_maxbytes=0 +stderr_logfile=/dev/stderr +stderr_logfile_maxbytes=0 +autorestart=true +startretries=3 +process_name=%(program_name)s_%(process_num)02d diff --git a/src/Base/Framework/Command/InitWorker.php b/src/Base/Framework/Command/InitWorker.php new file mode 100644 index 0000000..366194e --- /dev/null +++ b/src/Base/Framework/Command/InitWorker.php @@ -0,0 +1,90 @@ +setName('init:worker') + ->addOption('async', null, InputOption::VALUE_OPTIONAL, 'Run the async worker.',false) + ->addOption('download', null, InputOption::VALUE_OPTIONAL, 'Run the download worker.', false) + ->addOption('monitor', null, InputOption::VALUE_OPTIONAL, 'Run the monitor worker.', false) + ; + } + + protected function execute(InputInterface $input, OutputInterface $output): int + { + $configFile = $this->twig->render("config/supervisord.conf.twig", []); + + if ($this->optionExists($input, $output, 'async')) { + $configFile .= $this->twig->render("config/async.conf.twig", [ + 'replicas' => $this->getOptionValue('async', $input), + ]) . "\n\n"; + } + + if ($this->optionExists($input, $output, 'download')) { + $configFile .= $this->twig->render("config/download.conf.twig", [ + 'replicas' => $this->getOptionValue('download', $input), + ]). "\n\n"; + } + + if ($this->optionExists($input, $output, 'monitor')) { + $configFile .= $this->twig->render("config/monitor.conf.twig", [ + 'replicas' => $this->getOptionValue('monitor', $input), + ]). "\n\n"; + } + + if ("" !== $configFile) { + $output->writeln("[init:worker] Writing /etc/supervisor/conf.d/supervisord.conf"); + file_put_contents("/etc/supervisor/conf.d/supervisord.conf", $configFile); + + $output->writeln("[init:worker] Starting supervisord"); + shell_exec("/usr/bin/supervisord -c /etc/supervisor/conf.d/supervisord.conf"); + return Command::SUCCESS; + } + + $output->writeln("[init:worker] No workers selected. Exiting."); + return Command::FAILURE; + } + + private function optionExists(InputInterface $input, OutputInterface $output, string $option): bool + { + if ($input->getOption($option) !== false) { + $value = $input->getOption($option) ?? 1; + $output->writeln("[init:worker] transport: $option // $value // input var");; + return true; + } + + $optionKey = 'WORKER_' . strtoupper($option); + if (array_key_exists($optionKey, $_SERVER) && $_SERVER[$optionKey] !== null && $_SERVER[$optionKey] !== '') { + $output->writeln("[init:worker] transport: $option // $_SERVER[$optionKey] // env var"); + return true; + } + + return false; + } + + private function getOptionValue(string $option, InputInterface $input): int + { + if ($input->getOption($option) !== false) { + return $input->getOption($option) ?? 1; + } + + $optionKey = 'WORKER_' . strtoupper($option); + return $_SERVER[$optionKey]; + } +} diff --git a/templates/config/async.conf.twig b/templates/config/async.conf.twig new file mode 100644 index 0000000..3e21a28 --- /dev/null +++ b/templates/config/async.conf.twig @@ -0,0 +1,10 @@ +[program:torsearch-worker] +command=/app/bin/console messenger:consume async -vv --time-limit 3600 +numprocs={{ replicas|default(1) }} +stdout_logfile=/dev/stdout +stdout_logfile_maxbytes=0 +stderr_logfile=/dev/stderr +stderr_logfile_maxbytes=0 +autorestart=true +startretries=3 +process_name=%(program_name)s_%(process_num)02d diff --git a/templates/config/download.conf.twig b/templates/config/download.conf.twig new file mode 100644 index 0000000..f5d0e44 --- /dev/null +++ b/templates/config/download.conf.twig @@ -0,0 +1,10 @@ +[program:torsearch-downloader] +command=/app/bin/console messenger:consume download -vv --time-limit 3600 +numprocs={{ replicas|default(1) }} +stdout_logfile=/dev/stdout +stdout_logfile_maxbytes=0 +stderr_logfile=/dev/stderr +stderr_logfile_maxbytes=0 +autorestart=true +startretries=3 +process_name=%(program_name)s_%(process_num)02d \ No newline at end of file diff --git a/templates/config/monitor.conf.twig b/templates/config/monitor.conf.twig new file mode 100644 index 0000000..4c7e031 --- /dev/null +++ b/templates/config/monitor.conf.twig @@ -0,0 +1,10 @@ +[program:torsearch-scheduler] +command=/app/bin/console messenger:consume scheduler_monitor monitor -vv --time-limit 21600 +numprocs={{ replicas|default(1) }} +stdout_logfile=/dev/stdout +stdout_logfile_maxbytes=0 +stderr_logfile=/dev/stderr +stderr_logfile_maxbytes=0 +autorestart=true +startretries=3 +process_name=%(program_name)s_%(process_num)02d diff --git a/templates/config/supervisord.conf.twig b/templates/config/supervisord.conf.twig new file mode 100644 index 0000000..bf3a927 --- /dev/null +++ b/templates/config/supervisord.conf.twig @@ -0,0 +1,5 @@ +[supervisord] +nodaemon=true +logfile=/dev/null +logfile_maxbytes=0 +pidfile=/run/supervisord.pid