Skip to content

Commit 5d93733

Browse files
authored
when no worker is defined, do not stop the complete stack (#398)
1 parent 17fccc3 commit 5d93733

1 file changed

Lines changed: 21 additions & 21 deletions

File tree

.castor/docker.php

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -346,26 +346,15 @@ function workers_start(): void
346346
function workers_stop(): void
347347
{
348348
io()->title('Stopping workers');
349+
$workers = get_service_names(profile: 'worker');
349350

350-
// Docker compose cannot stop a single service in a profile, if it depends
351-
// on another service in another profile. To make it work, we need to select
352-
// both profiles, and so stop both services
351+
if ([] === $workers) {
352+
io()->error('No worker service found.');
353353

354-
// So we find all services, in all profiles, and manually filter the one
355-
// that has the "worker" profile, then we stop it
356-
$command = ['stop'];
357-
358-
foreach (get_services() as $name => $service) {
359-
foreach ($service['profiles'] ?? [] as $profile) {
360-
if ('worker' === $profile) {
361-
$command[] = $name;
362-
363-
continue 2;
364-
}
365-
}
354+
return;
366355
}
367356

368-
docker_compose($command, profiles: ['*']);
357+
docker_compose(['stop', ...$workers], profiles: ['*']);
369358
}
370359

371360
/**
@@ -588,22 +577,33 @@ function push(bool $dryRun = false): void
588577
/**
589578
* @return array<string, array{profiles?: list<string>, build: array{context: string, dockerfile?: string, cache_from?: list<string>, target?: string}}>
590579
*/
591-
function get_services(): array
580+
function get_services(?string $profile = null): array
592581
{
593-
return json_decode(
582+
$services = json_decode(
594583
docker_compose(
595584
['config', '--format', 'json'],
596585
context()->withQuiet(),
597586
profiles: ['*'],
598587
)->getOutput(),
599588
true,
589+
flags: JSON_THROW_ON_ERROR,
600590
)['services'];
591+
592+
if (null === $profile) {
593+
return $services;
594+
}
595+
596+
// Docker compose cannot get the services config for a given profile if one of
597+
// these services depends on another service in another profile.
598+
// So we find all services, in all profiles, and manually filter the one
599+
// that has the given profile, then we stop it
600+
return array_filter($services, static fn ($service) => \in_array($profile, $service['profiles'] ?? [], true));
601601
}
602602

603603
/**
604-
* @return string[]
604+
* @return list<string>
605605
*/
606-
function get_service_names(): array
606+
function get_service_names(?string $profile = null): array
607607
{
608-
return array_keys(get_services());
608+
return array_keys(get_services($profile));
609609
}

0 commit comments

Comments
 (0)