Skip to content

Commit

Permalink
Threaded routine tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
sondregronas committed Aug 1, 2024
1 parent fb52887 commit fe55d4e
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 37 deletions.
5 changes: 2 additions & 3 deletions BookingSystem/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import user
from __init__ import logger, REGEX_ID, REGEX_ITEM, MIN_DAYS, MAX_DAYS, MIN_LABELS, MAX_LABELS, DEBUG, MOCK_DATA
from db import init_db, Settings
from routine_tasks import start_routine
from routine_tasks import threaded_start_routine


def create_app() -> flask.Flask:
Expand Down Expand Up @@ -126,10 +126,9 @@ def debug_login() -> flask.Response:
# Compress & minify
Compress(app)
Minify(app, static=False, go=False) # Some static files don't minify well (breaks JS)

init_db()
Settings.verify_settings_exist()
start_routine() # Will not run if TESTING is set to True in the environment variables
threaded_start_routine(os.getpid())

return app

Expand Down
4 changes: 1 addition & 3 deletions BookingSystem/routes.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import flask

import inventory
import routine_tasks
from __init__ import KIOSK_FQDN, LABEL_SERVER
from db import add_admin, Settings
from sanitizer import handle_api_exception
Expand Down Expand Up @@ -52,8 +51,7 @@ 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',
routine_tasks=routine_tasks.get_job_status())
send_reports=Settings.get('send_reports') == '1')


@app.route('/audits')
Expand Down
31 changes: 22 additions & 9 deletions BookingSystem/routine_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ 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 Down Expand Up @@ -82,12 +83,24 @@ def run_mon_to_fri_at_time(job_func, at_time):
return run_continuously()


def get_job_status():
return {
'jobs': {
job.job_func.__name__: {'last_run': job.last_run.isoformat() if job.last_run else '',
'next_run': job.next_run.isoformat(),
'last_run_dt': job.last_run,
'next_run_dt': job.next_run}
for job in schedule.get_jobs()}
}
def threaded_start_routine(main_pid: int) -> None:
# NOTE: This (nested threads) shouldn't be necessary, but I could not get the atexit.register to work properly
# with the main gunicorn process. This is a workaround to ensure that the routine tasks are stopped when the main
# process exits.
import atexit

t = threading.Thread(target=start_routine, args=(), daemon=True)
t.start()

@atexit.register
def cleanup():
# Cleanup on exit
logger.info(f"Cleaning up {os.getpid()}... (Parent PID: {main_pid})")
if main_pid == os.getpid():
# Exit routine tasks process (only if main process is exiting)
t.join()
logger.info("Stopping routine tasks...")
else:
# Exit child processes
logger.info(f"Exiting child process with PID {os.getpid()}")
os._exit(0) # Exit without calling cleanup
22 changes: 0 additions & 22 deletions BookingSystem/templates/admin_settings.html
Original file line number Diff line number Diff line change
Expand Up @@ -43,28 +43,6 @@ <h3>En beskjed på toppen av siden. La feltene stå tomme for å fjerne bulletin
</hgroup>
{% include 'forms/bulletin.html' %}

<hgroup>
<h2>Rutinestatus</h2>
<h3>Informasjon om rutineoppgaver (kjøres automagisk)</h3>
</hgroup>

<table>
<thead>
<tr>
<th>Oppgave</th>
<th>Sist kjørt</th>
<th>Neste kjøring</th>
</tr>
</thead>
{% for job_name, job in routine_tasks.jobs.items() %}
<tr>
<td>{{ job_name.replace('_',' ').strip() | capitalize }}</td>
<td>{{ job.last_run }}</td>
<td>{{ job.next_run }}</td>
</tr>
{% endfor %}
</table>

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

Expand Down

0 comments on commit fe55d4e

Please sign in to comment.