aboutsummaryrefslogtreecommitdiffstats
path: root/.kamal/hooks/pre-connect.sample
diff options
context:
space:
mode:
authorMatthew Lemon <y@yulqen.org>2024-11-11 16:19:44 +0000
committerMatthew Lemon <y@yulqen.org>2024-11-11 16:19:44 +0000
commit97201e0bca203b6b303789374ffd7e4228e062a7 (patch)
tree1d70b0c499c514ceccd906b6b6e85dcbacbd3f45 /.kamal/hooks/pre-connect.sample
initial after migration
Diffstat (limited to '.kamal/hooks/pre-connect.sample')
-rwxr-xr-x.kamal/hooks/pre-connect.sample47
1 files changed, 47 insertions, 0 deletions
diff --git a/.kamal/hooks/pre-connect.sample b/.kamal/hooks/pre-connect.sample
new file mode 100755
index 0000000..18e61d7
--- /dev/null
+++ b/.kamal/hooks/pre-connect.sample
@@ -0,0 +1,47 @@
+#!/usr/bin/env ruby
+
+# A sample pre-connect check
+#
+# Warms DNS before connecting to hosts in parallel
+#
+# These environment variables are available:
+# KAMAL_RECORDED_AT
+# KAMAL_PERFORMER
+# KAMAL_VERSION
+# KAMAL_HOSTS
+# KAMAL_ROLE (if set)
+# KAMAL_DESTINATION (if set)
+# KAMAL_RUNTIME
+
+hosts = ENV["KAMAL_HOSTS"].split(",")
+results = nil
+max = 3
+
+elapsed = Benchmark.realtime do
+ results = hosts.map do |host|
+ Thread.new do
+ tries = 1
+
+ begin
+ Socket.getaddrinfo(host, 0, Socket::AF_UNSPEC, Socket::SOCK_STREAM, nil, Socket::AI_CANONNAME)
+ rescue SocketError
+ if tries < max
+ puts "Retrying DNS warmup: #{host}"
+ tries += 1
+ sleep rand
+ retry
+ else
+ puts "DNS warmup failed: #{host}"
+ host
+ end
+ end
+
+ tries
+ end
+ end.map(&:value)
+end
+
+retries = results.sum - hosts.size
+nopes = results.count { |r| r == max }
+
+puts "Prewarmed %d DNS lookups in %.2f sec: %d retries, %d failures" % [ hosts.size, elapsed, retries, nopes ]