Skip to content

Commit b9e5ce7

Browse files
committed
Ensure primary_role app hosts are sorted first
When booting non-primary role hosts we will always wait for a primary role host to boor first. So when booting in groups, if there are no primary role hosts in the first batch, then booting will stall. Sort primary role app_hosts first to avoid this. Fixes: #1553
1 parent f62c1a5 commit b9e5ce7

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

lib/kamal/commander/specifics.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ def initialize(config, specific_hosts, specific_roles)
1111
@primary_role = primary_or_first_role(roles_on(primary_host))
1212

1313
stable_sort!(roles) { |role| role == primary_role ? 0 : 1 }
14-
stable_sort!(hosts) { |host| roles_on(host).any? { |role| role == primary_role } ? 0 : 1 }
14+
sort_primary_role_hosts_first!(hosts)
1515
end
1616

1717
def roles_on(host)
1818
roles.select { |role| role.hosts.include?(host.to_s) }
1919
end
2020

2121
def app_hosts
22-
config.app_hosts & specified_hosts
22+
@app_hosts ||= sort_primary_role_hosts_first!(config.app_hosts & specified_hosts)
2323
end
2424

2525
def proxy_hosts
@@ -55,4 +55,8 @@ def specified_hosts
5555
specified_hosts
5656
end
5757
end
58+
59+
def sort_primary_role_hosts_first!(hosts)
60+
stable_sort!(hosts) { |host| roles_on(host).any? { |role| role == primary_role } ? 0 : 1 }
61+
end
5862
end

test/commander_test.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,12 @@ class CommanderTest < ActiveSupport::TestCase
149149
assert_equal [], @kamal.accessory_hosts
150150
end
151151

152+
test "primary role hosts are first" do
153+
configure_with(:deploy_with_roles_workers_primary)
154+
assert_equal [ "1.1.1.1", "1.1.1.2", "1.1.1.3", "1.1.1.4" ], @kamal.hosts
155+
assert_equal [ "1.1.1.1", "1.1.1.2", "1.1.1.3", "1.1.1.4" ], @kamal.app_hosts
156+
end
157+
152158
private
153159
def configure_with(variant)
154160
@kamal = Kamal::Commander.new.tap do |kamal|
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
service: app
2+
image: dhh/app
3+
servers:
4+
workers:
5+
- 1.1.1.1
6+
- 1.1.1.2
7+
web:
8+
- 1.1.1.3
9+
- 1.1.1.4
10+
env:
11+
REDIS_URL: redis://x/y
12+
registry:
13+
server: registry.digitalocean.com
14+
username: user
15+
password: pw
16+
builder:
17+
arch: amd64
18+
deploy_timeout: 1
19+
primary_role: workers

0 commit comments

Comments
 (0)