From 6620d0c916665d7fd03fe1d7c33a522443cf7f2b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Pineau?= Date: Mon, 26 Aug 2024 10:54:57 +0200 Subject: [PATCH] do not use profiles to control workers I does not work with depends on --- .castor/docker.php | 64 ++++++++++++++++++- .../docker/docker-compose.worker.yml | 7 +- 2 files changed, 66 insertions(+), 5 deletions(-) diff --git a/.castor/docker.php b/.castor/docker.php index a34880a..e7a7c1e 100644 --- a/.castor/docker.php +++ b/.castor/docker.php @@ -284,7 +284,24 @@ function workers_start(): void { io()->title('Starting workers'); - up(profiles: ['worker']); + $workers = get_workers(); + + if (!$workers) { + return; + } + + run([ + 'docker', + 'update', + '--restart=unless-stopped', + ...$workers, + ], quiet: true); + + run([ + 'docker', + 'start', + ...$workers, + ], quiet: true); } #[AsTask(description: 'Stops the workers', namespace: 'docker:worker', name: 'stop', aliases: ['stop-workers'])] @@ -292,7 +309,24 @@ function workers_stop(): void { io()->title('Stopping workers'); - stop(profiles: ['worker']); + $workers = get_workers(); + + if (!$workers) { + return; + } + + run([ + 'docker', + 'update', + '--restart=no', + ...$workers, + ]); + + run([ + 'docker', + 'stop', + ...$workers, + ]); } #[AsContext(default: true)] @@ -495,3 +529,29 @@ function run_in_docker_or_locally_for_mac(string $command, ?Context $c = null): docker_compose_run($command, c: $c); } } + + +/** + * Find worker containers for the current project. + * + * @return array + */ +function get_workers(): array +{ + $command = [ + 'docker', + 'ps', + '-a', + '--filter', 'label=docker-starter.worker.' . variable('project_name'), + '--quiet', + ]; + $out = capture($command); + + if (!$out) { + return []; + } + + $workers = explode("\n", $out); + + return array_map('trim', $workers); +} diff --git a/infrastructure/docker/docker-compose.worker.yml b/infrastructure/docker/docker-compose.worker.yml index a95c4b3..e4a1115 100644 --- a/infrastructure/docker/docker-compose.worker.yml +++ b/infrastructure/docker/docker-compose.worker.yml @@ -4,13 +4,14 @@ x-services-templates: build: context: services/php target: worker - # Don't use depends_on, it does not work well when stopping containers - # with docker compose profiles + # depends_on: + # - postgres volumes: - "../..:/var/www:cached" + labels: + - "docker-starter.worker.${PROJECT_NAME}=true" profiles: - default - - worker # services: # worker_messenger: