Skip to content

Commit

Permalink
Add a feature for customized service start command. (#68)
Browse files Browse the repository at this point in the history
  • Loading branch information
shk3 authored Mar 29, 2024
1 parent 3eb2af7 commit 176ad2d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
1 change: 1 addition & 0 deletions kafka_roller/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ Options:
-h INT, --healthcheck-retries=INT health check retries before aborting
-p STRING, --pre-stop=STRING command to run on each host before stopping the service
-r STRING, --pre-start=STRING command to run on each host before starting the service
-s STRING, --start-cmd=STRING customized command for starting the service
-z STRING, --zk=STRING zookeeper used by kafka
```

Expand Down
15 changes: 10 additions & 5 deletions kafka_roller/kafka_roller/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"conf": "kafka admin config overrides in a json file",
"pre_stop": "command to run on each host before stopping the service",
"pre_start": "command to run on each host before starting the service",
"start_cmd": "customized command for starting the service",
}
)
def restart(
Expand All @@ -41,6 +42,7 @@ def restart(
conf=None,
pre_stop=None,
pre_start=None,
start_cmd=None,
):
"""Gracefully restart an instance."""
admin = _admin_client(bootstrap, conf)
Expand All @@ -57,20 +59,20 @@ def restart(
logger.info("(%s/%s) Restarting %s", index + 1, len(hosts), hostname(b.host))
start = datetime.now()
_instance_restart(
b, admin, zk, healthcheck_retries, healthcheck_wait, pre_stop, pre_start
b, admin, zk, healthcheck_retries, healthcheck_wait, pre_stop, pre_start, start_cmd
)
end = datetime.now()
logger.info("Instance restart took: %s", end - start)


def _instance_restart(c, admin, zk, retries, retry_wait, pre_stop, pre_start):
def _instance_restart(c, admin, zk, retries, retry_wait, pre_stop, pre_start, start_cmd):
"""Gracefully restart an instance."""
with ZK(zk) as zk_conn:
if not is_cluster_healthy(admin, zk_conn, retries, retry_wait):
logger.critical("Cluster is not healthy, aborting!")
sys.exit(1)
stop(c, pre_stop)
start(c, pre_start)
start(c, pre_start, start_cmd)


def _admin_client(bootstrap, conf=None):
Expand Down Expand Up @@ -103,11 +105,14 @@ def stop(c, pre_stop=None):
# introduce & execute post stop here if needed..


def start(c, pre_start=None):
def start(c, pre_start=None, start_cmd=None):
"""Gracefully start an instance."""
if pre_start is not None:
_run_safe(c, "Pre-start", pre_start)
service_start(c, "kafka")
if start_cmd is not None:
_run_safe(c, "Start", start_cmd)
else:
service_start(c, "kafka")
# introduce & execute post start here if needed..


Expand Down

0 comments on commit 176ad2d

Please sign in to comment.