Skip to content

Commit

Permalink
do not use profiles to control workers
Browse files Browse the repository at this point in the history
I does not work with depends on
  • Loading branch information
lyrixx committed Aug 26, 2024
1 parent f089b27 commit 6620d0c
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 5 deletions.
64 changes: 62 additions & 2 deletions .castor/docker.php
Original file line number Diff line number Diff line change
Expand Up @@ -284,15 +284,49 @@ 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'])]
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)]
Expand Down Expand Up @@ -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<string>
*/
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);
}
7 changes: 4 additions & 3 deletions infrastructure/docker/docker-compose.worker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 6620d0c

Please sign in to comment.