← Back to team overview

launchpad-reviewers team mailing list archive

[Merge] ~lgp171188/launchpad:tie-celery-services-to-active-flag into launchpad:master

 

Guruprasad has proposed merging ~lgp171188/launchpad:tie-celery-services-to-active-flag into launchpad:master.

Commit message:
charm/launchpad-scripts: Control systemd services using the juju 'active' config

Requested reviews:
  Launchpad code reviewers (launchpad-reviewers)

For more details, see:
https://code.launchpad.net/~lgp171188/launchpad/+git/launchpad/+merge/447924
-- 
Your team Launchpad code reviewers is requested to review the proposed merge of ~lgp171188/launchpad:tie-celery-services-to-active-flag into launchpad:master.
diff --git a/charm/launchpad-scripts/reactive/launchpad-scripts.py b/charm/launchpad-scripts/reactive/launchpad-scripts.py
index ad779aa..15e7f3c 100644
--- a/charm/launchpad-scripts/reactive/launchpad-scripts.py
+++ b/charm/launchpad-scripts/reactive/launchpad-scripts.py
@@ -6,10 +6,20 @@ import subprocess
 import yaml
 from charmhelpers.core import hookenv, host, templating
 from charms.launchpad.base import configure_email, get_service_config
-from charms.launchpad.db import strip_dsn_authentication, update_pgpass
-from charms.launchpad.payload import configure_cron, configure_lazr, home_dir
+from charms.launchpad.db import (
+    lazr_config_files,
+    strip_dsn_authentication,
+    update_pgpass,
+)
+from charms.launchpad.payload import (
+    config_file_path,
+    configure_cron,
+    configure_lazr,
+    home_dir,
+)
 from charms.reactive import (
     endpoint_from_flag,
+    helpers,
     remove_state,
     set_state,
     when,
@@ -19,6 +29,15 @@ from charms.reactive import (
 from ols import base, postgres
 from psycopg2.extensions import parse_dsn
 
+CHARM_CELERY_SERVICES = [
+    "celerybeat_launchpad",
+    "celeryd_launchpad_job",
+    "celeryd_launchpad_job_slow",
+]
+CHARM_SYSTEMD_SERVICES = CHARM_CELERY_SERVICES + [
+    "number-cruncher",
+]
+
 
 def configure_logrotate(config):
     hookenv.log("Writing logrotate configuration.")
@@ -30,41 +49,28 @@ def configure_logrotate(config):
     )
 
 
-@host.restart_on_change(
-    {
-        "/lib/systemd/system/celerybeat_launchpad.service": [
-            "celerybeat_launchpad",
-        ],
-        "/lib/systemd/system/celeryd_launchpad_job.service": [
-            "celeryd_launchpad_job",
-        ],
-        "/lib/systemd/system/celeryd_launchpad_job_slow.service": [
-            "celeryd_launchpad_job_slow",
-        ],
-    }
-)
+def config_files():
+    files = []
+    files.extend(lazr_config_files())
+    files.append(config_file_path("launchpad-scripts/launchpad-lazr.conf"))
+    files.append(
+        config_file_path("launchpad-scripts-secrets-lazr.conf", secret=True)
+    )
+    return files
+
+
 def configure_celery(config):
     hookenv.log("Writing celery systemd service files.")
     destination_dir = "/lib/systemd/system"
-    service_files = (
-        "celerybeat_launchpad.service",
-        "celeryd_launchpad_job.service",
-        "celeryd_launchpad_job_slow.service",
-    )
-    for service_file in service_files:
+    for service in CHARM_CELERY_SERVICES:
         templating.render(
-            f"{service_file}.j2",
-            f"{destination_dir}/{service_file}",
+            f"{service}.service.j2",
+            f"{destination_dir}/{service}.service",
             config,
         )
     subprocess.check_call(["systemctl", "daemon-reload"])
-    for service_file in service_files:
-        subprocess.check_call(["systemctl", "enable", service_file])
 
 
-@host.restart_on_change(
-    {"/lib/systemd/system/number-cruncher.service": ["number-cruncher"]}
-)
 def configure_number_cruncher(config):
     hookenv.log("Writing the number-cruncher systemd service file.")
     templating.render(
@@ -73,7 +79,6 @@ def configure_number_cruncher(config):
         config,
     )
     subprocess.check_call(["systemctl", "daemon-reload"])
-    subprocess.check_call(["systemctl", "enable", "number-cruncher.service"])
 
 
 def configure_librarian_logs_sync(config):
@@ -161,6 +166,25 @@ def configure():
     configure_cron(config, "crontab.j2")
     configure_celery(config)
     configure_number_cruncher(config)
+
+    if config["active"]:
+        for service in CHARM_SYSTEMD_SERVICES:
+            if helpers.any_file_changed(
+                [
+                    base.version_info_path(),
+                    f"/lib/systemd/system/{service}.service",
+                ]
+                + config_files()
+            ):
+                hookenv.log(
+                    f"Config files or payload changed; restarting {service}"
+                )
+                host.service_restart(service)
+            host.service_resume(service)
+    else:
+        for service in CHARM_SYSTEMD_SERVICES:
+            host.service_pause(service)
+
     set_state("service.configured")