From fcd5ab17cce52b08d3694eaa6e69aac2b6d50beb Mon Sep 17 00:00:00 2001 From: har0ke Date: Wed, 21 Aug 2019 10:23:07 +0200 Subject: [PATCH] Django command to install systemd service for recalculation thread --- .../commands/recalculate_process.py | 22 ++++++++ .../commands/systemd_recalculate.py | 50 +++++++++++++++++++ .../commands/tally_recalculate.service | 14 ++++++ 3 files changed, 86 insertions(+) create mode 100644 main/management/commands/recalculate_process.py create mode 100644 main/management/commands/systemd_recalculate.py create mode 100644 main/management/commands/tally_recalculate.service diff --git a/main/management/commands/recalculate_process.py b/main/management/commands/recalculate_process.py new file mode 100644 index 0000000..8869657 --- /dev/null +++ b/main/management/commands/recalculate_process.py @@ -0,0 +1,22 @@ +#!/usr/bin/python +# -*- coding: -*- + +from django.contrib.auth.models import User +from django.core.management.base import BaseCommand +from django.db import transaction + +from main.billing import RecalculateThread +from main.models import ProductType + + +class Command(BaseCommand): + + @transaction.atomic + def handle(self, *args, **options): + r_thread = RecalculateThread() + r_thread.start() + try: + r_thread.join() + except KeyboardInterrupt: + r_thread.running = False + r_thread.join() \ No newline at end of file diff --git a/main/management/commands/systemd_recalculate.py b/main/management/commands/systemd_recalculate.py new file mode 100644 index 0000000..e16ba36 --- /dev/null +++ b/main/management/commands/systemd_recalculate.py @@ -0,0 +1,50 @@ +#!/usr/bin/python +# -*- coding: -*- +import os +import shlex + +import pwd +from django.contrib.auth.models import User +from django.core.management.base import BaseCommand +from django.db import transaction + +from main.billing import RecalculateThread +from main.models import ProductType + + +class Command(BaseCommand): + + def add_arguments(self, parser): + # declare file to import from + parser.add_argument("user") + parser.add_argument("environment") + + def handle(self, *args, **options): + with open(os.path.join(os.path.dirname(__file__), "tally_recalculate.service")) as f: + template = f.read() + + options = { + "user": shlex.quote(options["user"]), + "venv": shlex.quote(os.path.abspath(options["environment"])), + "proj": shlex.quote(os.path.abspath(os.path.join(os.path.dirname(__file__), "../../.."))) + } + + if not os.path.exists(os.path.join(options["venv"], "bin", "activate")): + print(os.path.join(options["venv"], "bin", "activate") + " does not exist.") + exit(-1) + + try: + pwd.getpwnam(options["user"]) + except KeyError: + print('User %s does not exist.' % options["user"]) + exit(-1) + + if not os.path.exists(os.path.join(options["venv"], "bin", "activate")): + print(os.path.join(options["venv"], "bin", "activate") + " does not exist.") + + output_file = "/etc/systemd/system/tally-recalculate.service" + + print("Installing service (%s)" % output_file) + os.system("sudo bash -c " + shlex.quote("echo " + shlex.quote(template.format(**options)) + " > " + output_file)) + print("Enabling service") + os.system("sudo systemctl enable tally-recalculate") \ No newline at end of file diff --git a/main/management/commands/tally_recalculate.service b/main/management/commands/tally_recalculate.service new file mode 100644 index 0000000..31356c4 --- /dev/null +++ b/main/management/commands/tally_recalculate.service @@ -0,0 +1,14 @@ +[Unit] +Description=Tallybill Recalculate Service +After=network.target +StartLimitIntervalSec=0 + +[Service] +Type=simple +Restart=always +RestartSec=1 +User={user} +ExecStart=bash {proj}/scripts/tally_recalculate.sh {venv} {proj} + +[Install] +WantedBy=multi-user.target \ No newline at end of file