readiness-check-command: implemented.

The 'postgresql' service is a good example of how this is used in
practice.
master
Luka Vandervelden 2019-11-11 16:52:40 +01:00
parent c43eb7af42
commit f2507e2dd0
3 changed files with 32 additions and 0 deletions

View File

@ -2,6 +2,7 @@ name: postgresql
user: postgres
command: postgres -D ${SERVICE_ROOT}/db -k /tmp/postgresql-${ENVIRONMENT}
#stop-command: kill -HUP ${PID}
readiness-check-command: pg_isready -p ${POSTGRESQL_PORT}
environment-variables:
- PGROOT=${SERVICE_ROOT}
provides: postgresql

View File

@ -179,6 +179,9 @@ class Service
def reload_command
@reference.reload_command
end
def readiness_check_command
@reference.readiness_check_command
end
def provides
@reference.provides
end
@ -233,6 +236,12 @@ class Service
env["SERVICE_TOKENS"] = @providers.to_a.map(&.[0]).join ':'
@ports.each do |name, number|
name = name.upcase
env["PORT_#{name}"] = number.to_s
end
# FIXME: Parsing should probably be done… when parsing the file.
# FIXME: Parsing is probably a bit primitive. Maybe this isnt the right way of defining this.
@reference.environment_variables.each do |string|
@ -341,6 +350,26 @@ class Service
end
self.save_pid context.pid_directory, process.pid
# FIXME: At this point, lets wait a few seconds for the service to be ready.
if command = readiness_check_command
context.info "Waiting for service to become ready…"
start_time = Time.local
now = Time.local
while (now - start_time).seconds < 5
r = Process.run "sh", ["-c", command]
if r.success?
break
end
sleep 0.05
now = Time.local
end
end
end
# TODO:

View File

@ -74,6 +74,7 @@ class ServiceDefinition
getter command : String
getter stop_command : String?
getter reload_command : String?
getter readiness_check_command : String?
getter directory : String?
getter user : String?
getter provides : String?
@ -93,6 +94,7 @@ class ServiceDefinition
@non_runnable = (@command == "none")
@stop_command = specs["stop-command"]?.try &.as_s
@reload_command = specs["reload-command"]?.try &.as_s
@readiness_check_command = specs["readiness-check-command"]?.try &.as_s
@directory = specs["directory"]?.try &.as_s
@user = specs["user"]?.try &.as_s
@provides = specs["provides"]?.try &.as_a_or_s.map { |x| Provides.new x } || Array(Provides).new