diff --git a/services/postgresql.spec b/services/postgresql.spec index 204b194..0f681ab 100644 --- a/services/postgresql.spec +++ b/services/postgresql.spec @@ -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 diff --git a/src/service/service.cr b/src/service/service.cr index 90274e2..8d69629 100644 --- a/src/service/service.cr +++ b/src/service/service.cr @@ -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 isn’t 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, let’s 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: diff --git a/src/service/service_definition.cr b/src/service/service_definition.cr index 9b8b095..b5a7ac2 100644 --- a/src/service/service_definition.cr +++ b/src/service/service_definition.cr @@ -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