Django command to install systemd service for recalculation thread
This commit is contained in:
parent
cf3b812549
commit
fcd5ab17cc
22
main/management/commands/recalculate_process.py
Normal file
22
main/management/commands/recalculate_process.py
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
#!/usr/bin/python
|
||||||
|
# -*- coding: <utf-8> -*-
|
||||||
|
|
||||||
|
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()
|
50
main/management/commands/systemd_recalculate.py
Normal file
50
main/management/commands/systemd_recalculate.py
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
#!/usr/bin/python
|
||||||
|
# -*- coding: <utf-8> -*-
|
||||||
|
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")
|
14
main/management/commands/tally_recalculate.service
Normal file
14
main/management/commands/tally_recalculate.service
Normal file
@ -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
|
Loading…
x
Reference in New Issue
Block a user