Skip to content

Commit

Permalink
Server start time & routine task status
Browse files Browse the repository at this point in the history
  • Loading branch information
sondregronas committed Aug 2, 2024
1 parent d611cb6 commit 6e62b79
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 11 deletions.
4 changes: 3 additions & 1 deletion BookingSystem/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ def context_processor() -> dict:
MAX_DAYS=MAX_DAYS,
MIN_LABELS=MIN_LABELS,
MAX_LABELS=MAX_LABELS,
FQDN=urlparse(flask.request.base_url).hostname, )
FQDN=urlparse(flask.request.base_url).hostname,
server_start_time=Settings.get('last_start_time'))

@app.errorhandler(401)
def unauthorized_401(_) -> tuple[flask.Response, int]:
Expand Down Expand Up @@ -131,6 +132,7 @@ def debug_login() -> flask.Response:
Minify(app, static=False, go=False) # Some static files don't minify well (breaks JS)
init_db()
Settings.verify_settings_exist()
Settings.set('last_start_time', str(datetime.now().timestamp()))
threaded_start_routine(os.getpid())

return app
Expand Down
5 changes: 4 additions & 1 deletion BookingSystem/routes.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import json

import flask

import inventory
Expand Down Expand Up @@ -51,7 +53,8 @@ def logout() -> flask.Response:
def admin_settings() -> str:
return flask.render_template('admin_settings.html',
last_sent=Settings.get('report_last_sent') or 0,
send_reports=Settings.get('send_reports') == '1')
send_reports=Settings.get('send_reports') == '1',
routine_tasks_info=json.loads(Settings.get('routine_tasks') or '{}'))


@app.route('/audits')
Expand Down
30 changes: 21 additions & 9 deletions BookingSystem/routine_tasks.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
# Run cron jobs within this file as python functions

import json
import os
import shutil
import threading
import time
from datetime import datetime

import schedule

from __init__ import logger
from db import DATABASE
from db import DATABASE, Settings
from teams import send_report
from user import prune_inactive

Expand All @@ -22,7 +24,6 @@ def run(cls):
while not cease_continuous_run.is_set():
schedule.run_pending()
time.sleep(interval)
# TODO: Post to API when a task is run? (for debugging)

continuous_thread = ScheduleThread()
continuous_thread.start()
Expand All @@ -40,9 +41,20 @@ def _routine_backup():


def start_routine():
def _task(job_func):
def update_db(fn_name):
data = json.loads(Settings.get('routine_tasks') or '{}')
if not data.get(fn_name):
data[fn_name] = dict({'last_run': None, 'runs': 0})

data[fn_name]['last_run'] = datetime.now().timestamp()
data[fn_name]['runs'] += 1

Settings.set('routine_tasks', json.dumps(data))

def _task(job_func, job_name=''):
try:
job_func()
update_db(job_name)
except Exception as e:
logger.error(e)

Expand All @@ -59,12 +71,12 @@ def run_mon_to_fri_at_time(job_func, at_time):
job_func.__name__ = f'{func_name} (Fredag)'
schedule.every().friday.at(at_time).do(job_func)

__routine_backup = lambda: _task(_routine_backup)
__send_report = lambda: _task(send_report)
__prune_inactive = lambda: _task(prune_inactive)
__routine_backup.__name__ = 'Ukentlig backup'
__send_report.__name__ = 'Send Dagsrapport'
__prune_inactive.__name__ = 'Rydd opp brukere'
__routine_backup = lambda: _task(_routine_backup, 'Ukentlig backup')
__send_report = lambda: _task(send_report, 'Send Dagsrapport')
__prune_inactive = lambda: _task(prune_inactive, 'Rydd opp brukere')
__routine_backup.__name__ = 'Ukentlig backup' # Set name for scheduler
__send_report.__name__ = 'Send Dagsrapport' # Set name for scheduler
__prune_inactive.__name__ = 'Rydd opp brukere' # Set name for scheduler

# TODO: Add a setting to change the time of the day these run from frontend
run_mon_to_fri_at_time(__send_report, "10:00")
Expand Down
26 changes: 26 additions & 0 deletions BookingSystem/templates/admin_settings.html
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,32 @@ <h3>En beskjed på toppen av siden. La feltene stå tomme for å fjerne bulletin
</hgroup>
{% include 'forms/bulletin.html' %}

{% if routine_tasks %}
<hgroup>
<h2>Rutineoppgaver</h2>
<h3>Status på rutineoppgaver (debugging)</h3>
</hgroup>
<table>
<tr>
<th>Oppgave</th>
<th>Sist kjørt</th>
<th>Utføringer</th>
</tr>
{% for task, values in routine_tasks_info.items() %}
<tr>
<td>{{ task }}</td>
<td>{{ values['last_run'] | strfunixtime('%H:%M:%S %d.%m.%Y') }}</td>
<td>{{ values['runs'] }}</td>
</tr>
{% endfor %}
</table>
{% endif %}

<center>
<small>Applikasjonen har kjørt siden
{{ server_start_time | strfunixtime }}</small>
</center>

<script>
let last_sent = "{{last_sent}}";

Expand Down

0 comments on commit 6e62b79

Please sign in to comment.