From 17cbfd71b4bfb155139ae5155a422819e0a1201e Mon Sep 17 00:00:00 2001
From: Patrick Jentsch
Date: Wed, 22 Sep 2021 13:53:39 +0200
Subject: [PATCH] Rename TaskRunner to Daemon (because it essentially is one)
and restructure cli
---
app/cli.py | 25 ++++++++++++++++++-------
app/daemon/__init__.py | 20 ++++++++++++++++++++
app/{tasks => daemon}/corpus_utils.py | 0
app/{tasks => daemon}/job_utils.py | 0
app/tasks/__init__.py | 14 --------------
boot.sh | 12 ++++++------
config.py | 2 ++
7 files changed, 46 insertions(+), 27 deletions(-)
create mode 100644 app/daemon/__init__.py
rename app/{tasks => daemon}/corpus_utils.py (100%)
rename app/{tasks => daemon}/job_utils.py (100%)
delete mode 100644 app/tasks/__init__.py
diff --git a/app/cli.py b/app/cli.py
index 7c0782b2..50e017c9 100644
--- a/app/cli.py
+++ b/app/cli.py
@@ -11,15 +11,26 @@ def register(app):
# create or update user roles
Role.insert_roles()
- @app.cli.command()
- def tasks():
- from app.tasks import TaskRunner
- task_runner = TaskRunner()
- task_runner.run()
+ @app.cli.group()
+ def daemon():
+ """Daemon commands."""
+ pass
- @app.cli.command()
+ @daemon.command()
+ def run():
+ """Run daemon"""
+ from app.daemon import Daemon
+ daemon = Daemon()
+ daemon.run()
+
+ @app.cli.group()
def test():
- """Run the unit tests."""
+ """Test commands."""
+ pass
+
+ @test.command()
+ def run():
+ """Run unit tests."""
import unittest
tests = unittest.TestLoader().discover('tests')
unittest.TextTestRunner(verbosity=2).run(tests)
diff --git a/app/daemon/__init__.py b/app/daemon/__init__.py
new file mode 100644
index 00000000..461e0ca8
--- /dev/null
+++ b/app/daemon/__init__.py
@@ -0,0 +1,20 @@
+from app import db
+from time import sleep
+from .corpus_utils import CheckCorporaMixin
+from .job_utils import CheckJobsMixin
+import docker
+
+
+class Daemon(CheckCorporaMixin, CheckJobsMixin):
+ def __init__(self):
+ self.docker = docker.from_env()
+
+ def run(self):
+ while True:
+ try:
+ self.check_corpora()
+ self.check_jobs()
+ db.session.commit()
+ except:
+ pass
+ sleep(1.5)
diff --git a/app/tasks/corpus_utils.py b/app/daemon/corpus_utils.py
similarity index 100%
rename from app/tasks/corpus_utils.py
rename to app/daemon/corpus_utils.py
diff --git a/app/tasks/job_utils.py b/app/daemon/job_utils.py
similarity index 100%
rename from app/tasks/job_utils.py
rename to app/daemon/job_utils.py
diff --git a/app/tasks/__init__.py b/app/tasks/__init__.py
deleted file mode 100644
index 69096a7b..00000000
--- a/app/tasks/__init__.py
+++ /dev/null
@@ -1,14 +0,0 @@
-from .corpus_utils import CheckCorporaMixin
-from .job_utils import CheckJobsMixin
-from ..import db, socketio
-import docker
-
-
-class TaskRunner(CheckCorporaMixin, CheckJobsMixin):
- def __init__(self):
- self.docker = docker.from_env()
-
- def run(self):
- self.check_corpora()
- self.check_jobs()
- db.session.commit()
diff --git a/boot.sh b/boot.sh
index bbca6652..579d0f06 100755
--- a/boot.sh
+++ b/boot.sh
@@ -1,19 +1,19 @@
#!/bin/bash
source venv/bin/activate
-if [[ "${NOPAQUE_DAEMON_ENABLED:-True}" == "True" ]]; then
- echo "INFO Starting nopaque daemon process..."
- ./nopaque-daemon.sh &
-fi
-
if [[ "${#}" -eq 0 ]]; then
while true; do
+ echo "INFO Run deployment tasks..."
flask deploy
if [[ "${?}" == "0" ]]; then
break
fi
- echo "Deploy command failed, retrying in 5 secs..."
+ echo "WARNING ...Failed, retrying in 5 secs..."
sleep 5
done
+ if [[ "${NOPAQUE_DAEMON_ENABLED:-True}" == "True" ]]; then
+ echo "INFO Start nopaque daemon..."
+ flask daemon run &
+ fi
gunicorn \
--access-logfile - \
--error-logfile - \
diff --git a/config.py b/config.py
index bdd94433..b922cd64 100644
--- a/config.py
+++ b/config.py
@@ -46,6 +46,8 @@ class Config:
''' # nopaque # '''
NOPAQUE_ADMIN = os.environ.get('NOPAQUE_ADMIN')
NOPAQUE_CONTACT = os.environ.get('NOPAQUE_CONTACT')
+ NOPAQUE_DAEMON_ENABLED = \
+ os.environ.get('NOPAQUE_DAEMON_ENABLED', 'true').lower() == 'true'
NOPAQUE_DATA_DIR = os.environ.get('NOPAQUE_DATA_DIR', '/mnt/nopaque')
NOPAQUE_MAIL_SUBJECT_PREFIX = '[nopaque]'
NOPAQUE_SOCKETIO_MESSAGE_QUEUE_URI = \