Skip to content

Commit 422159f

Browse files
authored
Merge pull request #12 from micado-scale/develop
Better app clean-up on boot
2 parents 1c4371f + 36368f8 commit 422159f

File tree

4 files changed

+53
-17
lines changed

4 files changed

+53
-17
lines changed

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,4 @@ ENV MICADO_DIR=/etc/micado/
2121
ENV MICADO_VERS=0.12.1
2222
ENV MICADO_SPEC=/etc/eec/micado_spec.yaml
2323

24-
ENTRYPOINT ["gunicorn", "micado_eec.micado:app", "--bind", "0.0.0.0:5000", "--timeout", "1000", "--workers", "5"]
24+
ENTRYPOINT ["gunicorn", "micado_eec.micado:app", "-c", "micado_eec/gunicorn_conf.py"]

micado_eec/gunicorn_conf.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import redis
2+
from micado_eec.handle_micado import HandleMicado, r
3+
4+
bind = '0.0.0.0:5000'
5+
6+
workers = 5
7+
timeout = 30
8+
9+
def on_starting(server):
10+
server.log.info("Cleaning-up uninitialised apps...")
11+
12+
for thread_id in r.keys():
13+
try:
14+
micado = r.hget(thread_id, "micado_id")
15+
updated = r.hget(thread_id, "last_app_refresh")
16+
except redis.exceptions.ResponseError:
17+
raise TypeError("Database corrupt - contains wrong data types.")
18+
19+
if not micado:
20+
server.log.info(f"App {thread_id} has no MiCADO, removing from DB.")
21+
r.delete(thread_id)
22+
continue
23+
24+
if not updated:
25+
server.log.info(f"App {thread_id} has MiCADO, attempting abort.")
26+
r.expire(thread_id, 60)
27+
thread = HandleMicado(thread_id, f"process_{thread_id}")
28+
thread.start()
29+
thread.abort()
30+
31+
server.log.info("App clean-up done.")
32+

micado_eec/handle_micado.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@
33
import base64
44
import threading
55
import time
6-
from datetime import datetime
76
from typing import Optional
87

8+
import redis
99
import ruamel.yaml as yaml
10-
from redis import StrictRedis
1110
from micado import MicadoClient
1211

1312
from .utils import base64_to_yaml, load_yaml_file
@@ -43,7 +42,10 @@
4342
APP_ID_PARAM = "app_id"
4443
APP_PARAMS = "params"
4544

46-
r = StrictRedis("redis", decode_responses=True)
45+
try:
46+
r = redis.StrictRedis("redis", decode_responses=True)
47+
except redis.exceptions.ConnectionError as e:
48+
raise ConnectionError(f"Cannot connect to Redis: {e}")
4749
if not r.ping():
4850
raise ConnectionError("Cannot connect to Redis")
4951

@@ -86,7 +88,7 @@ def __init__(
8688
self.name = name
8789

8890
if not r.hgetall(threadID):
89-
r.hset(threadID, "submit_time", datetime.now().timestamp())
91+
r.hset(threadID, "submit_time", time.time())
9092
self.set_status()
9193

9294
self.artefact_data = artefact_data or {}
@@ -161,6 +163,7 @@ def run(self):
161163

162164
# Wait for abort
163165
while True:
166+
r.hset(self.threadID, "last_app_refresh", time.time())
164167
if self._is_aborted():
165168
self.abort()
166169
break

micado_eec/micado.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,34 @@
11
import json
22
import uuid
33
import tempfile
4+
import time
45
from datetime import datetime
56

67
import redis
78
from flask import jsonify, Flask, request
89
from werkzeug.exceptions import BadRequest, NotFound
910

10-
from .handle_micado import HandleMicado
11+
from .handle_micado import HandleMicado, r
1112
from .utils import base64_to_yaml, is_valid_adt, get_adt_inputs, get_csar_inputs, file_to_json
1213

13-
r = redis.StrictRedis("redis", decode_responses=True)
14-
if not r.ping():
15-
raise ConnectionError("Cannot connect to Redis")
14+
app = Flask(__name__)
15+
app.debug = True
1616

1717
for thread_id in r.keys():
1818
try:
19-
micado_id = r.hget(thread_id, "micado_id")
19+
updated = r.hget(thread_id, "last_app_refresh")
2020
except redis.exceptions.ResponseError:
2121
raise TypeError("Database corrupt - contains wrong data types.")
22-
23-
if not micado_id:
24-
r.delete(thread_id)
22+
23+
if not updated:
2524
continue
26-
thread = HandleMicado(thread_id, f"process_{thread_id}")
27-
thread.start()
25+
26+
age_seconds = time.time() - float(updated)
27+
if age_seconds > 30:
28+
r.hset(thread_id, "last_app_refresh", time.time())
29+
thread = HandleMicado(thread_id, f"process_{thread_id}")
30+
thread.start()
2831

29-
app = Flask(__name__)
30-
app.debug = True
3132

3233
@app.errorhandler(BadRequest)
3334
def handle_generic_bad_request(error):

0 commit comments

Comments
 (0)