Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request consul in prestart hook #278

Merged
merged 1 commit into from
Oct 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 86 additions & 29 deletions jobs/fastcgi.nomad
Original file line number Diff line number Diff line change
Expand Up @@ -22,44 +22,101 @@ job "fastcgi" {
datacenters = ["dc1"]

group "fastcgi" {

# Init Task Lifecycle
# Reference: https://www.nomadproject.io/docs/job-specification/lifecycle#init-task-pattern
task "wait-for-mysql" {
lifecycle {
hook = "prestart"
sidecar = false
dynamic "task" {
for_each = local.main ? [{}] : []
labels = ["wait-for-mysql"]
content {
lifecycle {
hook = "prestart"
sidecar = false
}

driver = "exec"
config {
command = "sh"
args = [
"-c",
"while ! ncat --send-only 127.0.0.1 3306 < /dev/null; do sleep 1; done",
]
}
}
}
# Inter-job dependencies with init tasks
# https://developer.hashicorp.com/nomad/tutorials/task-deps/task-dependencies-interjob
dynamic "task" {
for_each = var.test ? [{}] : []
labels = ["await-mysql"]
content {
lifecycle {
hook = "prestart"
sidecar = false
}

driver = "exec"
config {
command = "sh"
args = [
"-c",
format(
"while ! ncat --send-only %s %s < /dev/null; do sleep 1; done",
local.main ? "127.0.0.1" : NOMAD_UPSTREAM_IP_mysql,
local.main ? "3306" : NOMAD_UPSTREAM_PORT_mysql
),
]
driver = "docker"
config {
image = "busybox:1.28"
command = "sh"
network_mode = "host"
args = [
"-c",
true ? "echo -n 'Waiting for service'; until nc -z ${var.main_nomad_addr} 3306 < /dev/null; do echo '.'; sleep 2; done"
: "echo -n 'Waiting for service'; until nslookup mysql.service.consul 127.0.0.1:8600 2>&1 >/dev/null; do echo '.'; sleep 2; done",
]
}

resources {
cpu = 100
memory = 100
}
}
}

task "wait-for-memcached" {
lifecycle {
hook = "prestart"
sidecar = false
dynamic "task" {
for_each = local.main ? [{}] : []
labels = ["wait-for-memcached"]
content {
lifecycle {
hook = "prestart"
sidecar = false
}

driver = "exec"
config {
command = "sh"
args = [
"-c",
"while ! ncat --send-only 127.0.0.1 11211 < /dev/null; do sleep 1; done",
]
}
}
}
dynamic "task" {
for_each = var.test ? [{}] : []
labels = ["await-memcached"]
content {
lifecycle {
hook = "prestart"
sidecar = false
}

driver = "exec"
config {
command = "sh"
args = [
"-c",
format("while ! ncat --send-only %s %s < /dev/null; do sleep 1; done",
local.main ? "127.0.0.1" : NOMAD_UPSTREAM_IP_memcached,
local.main ? "11211" : NOMAD_UPSTREAM_PORT_memcached
),
]
driver = "docker"
config {
image = "busybox:1.28"
command = "sh"
network_mode = "host"
args = [
"-c",
"echo -n 'Waiting for service'; until nslookup memcached.service.consul 127.0.0.1:8600 2>&1 >/dev/null; do echo '.'; sleep 2; done",
]
}

resources {
cpu = 100
memory = 100
}
}
}

Expand Down