Skip to content

Commit

Permalink
feat: Add an extra_checks param to service registration
Browse files Browse the repository at this point in the history
Without breaking compatibility, an optional extra_checks
list of Checks allows us to register multiple checks during
service registration.

Note that this was still possible through check registration
but requires multiple API calls.
  • Loading branch information
Mathias Brulatout committed Mar 15, 2024
1 parent 261e9e0 commit 664b497
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
5 changes: 4 additions & 1 deletion consul/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -836,6 +836,7 @@ def register(
http=None,
timeout=None,
enable_tag_override=False,
extra_checks=None,
):
"""
Add a new service to the local agent. There is more
Expand Down Expand Up @@ -877,6 +878,8 @@ def register(
https://www.consul.io/docs/agent/services.html
"""

if extra_checks is None:
extra_checks = []
payload = {}

payload["name"] = name
Expand All @@ -893,7 +896,7 @@ def register(
if meta:
payload["meta"] = meta
if check:
payload["check"] = check
payload["checks"] = [check] + extra_checks
if weights:
payload["weights"] = weights

Expand Down
33 changes: 32 additions & 1 deletion tests/test_std.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,35 @@ def verify_check_status(check_id, status, notes=None):
verify_check_status("ttl_check", "critical")
verify_and_dereg_check("ttl_check")

def test_service_multi_check(self, consul_port):
c = consul.Consul(port=consul_port)
http_addr = f"http://127.0.0.1:{consul_port}"
c.agent.service.register(
"foo1",
check=Check.http(http_addr, "10ms"),
extra_checks=[
Check.http(http_addr, "20ms"),
Check.http(http_addr, "30ms"),
],
)

time.sleep(200 / 1000.0)

_index, nodes = c.health.service("foo1")
assert {check["ServiceID"] for node in nodes for check in node["Checks"]} == {"foo1", ""}

assert {check["CheckID"] for node in nodes for check in node["Checks"]} == {
"service:foo1:1",
"service:foo1:2",
"service:foo1:3",
"serfHealth",
}
time.sleep(1)

_index, checks = c.health.checks(service="foo1")
assert [check["CheckID"] for check in checks] == ["service:foo1:1", "service:foo1:2", "service:foo1:3"]
assert [check["Status"] for check in checks] == ["passing", "passing", "passing"]

def test_service_dereg_issue_156(self, consul_port):
# https://github.com/cablehead/python-consul/issues/156
service_name = "app#127.0.0.1#3000"
Expand Down Expand Up @@ -274,15 +303,17 @@ def test_agent_checks_service_id(self, consul_obj):
assert [node["Service"]["ID"] for node in nodes] == ["foo1"]

c.agent.check.register("foo", Check.ttl("100ms"), service_id="foo1")
c.agent.check.register("foo2", Check.ttl("100ms"), service_id="foo1")

time.sleep(40 / 1000.0)

_index, nodes = c.health.service("foo1")
assert {check["ServiceID"] for node in nodes for check in node["Checks"]} == {"foo1", ""}
assert {check["CheckID"] for node in nodes for check in node["Checks"]} == {"foo", "serfHealth"}
assert {check["CheckID"] for node in nodes for check in node["Checks"]} == {"foo", "foo2", "serfHealth"}

# Clean up tasks
assert c.agent.check.deregister("foo") is True
assert c.agent.check.deregister("foo2") is True

time.sleep(40 / 1000.0)

Expand Down

0 comments on commit 664b497

Please sign in to comment.