Skip to content

Commit b015c9d

Browse files
authored
Merge pull request #20 from ArcReactorKC/codex/add-buymeacoffee-link-to-webui-ir1167
Start scheduler on first request under gunicorn
2 parents 62cb47b + e6f7056 commit b015c9d

3 files changed

Lines changed: 43 additions & 5 deletions

File tree

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@ ENV DATA_DIR=/data \
1616
DEVICE_DB=/data/devices.json \
1717
BACKUP_OUTPUT_DIR=/backups
1818

19-
CMD ["python", "/app/app/app.py"]
19+
CMD ["gunicorn", "-w", "2", "-k", "gthread", "--threads", "4", "-b", "0.0.0.0:5000", "--access-logfile", "-", "--error-logfile", "-", "--chdir", "/app/app", "app:app"]

app/app.py

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,27 @@
1+
import fcntl
12
import json
23
import os
34
import shutil
45
import stat
56
import tempfile
67
import threading
8+
import warnings
9+
from datetime import datetime, timezone
710
from ftplib import FTP, all_errors as ftp_errors
811
from datetime import datetime, timezone
912
from pathlib import Path
1013
from typing import Optional
1114

15+
from cryptography.utils import CryptographyDeprecationWarning
16+
17+
warnings.filterwarnings(
18+
"ignore",
19+
message=(
20+
r".*TripleDES has been moved to cryptography\.hazmat\.decrepit\.ciphers\.algorithms\.TripleDES.*"
21+
),
22+
category=CryptographyDeprecationWarning,
23+
)
24+
1225
import paramiko
1326
from apscheduler.schedulers.background import BackgroundScheduler
1427
from apscheduler.triggers.interval import IntervalTrigger
@@ -39,6 +52,8 @@
3952
scheduler = BackgroundScheduler()
4053
backup_status_lock = threading.Lock()
4154
backup_status = {}
55+
_scheduler_lock_handle = None
56+
_scheduler_started = False
4257

4358

4459
def load_devices():
@@ -365,6 +380,29 @@ def refresh_schedule():
365380
save_devices(devices_list)
366381

367382

383+
def start_scheduler():
384+
global _scheduler_lock_handle, _scheduler_started
385+
if _scheduler_started:
386+
return
387+
lock_path = DATA_DIR / "scheduler.lock"
388+
lock_path.parent.mkdir(parents=True, exist_ok=True)
389+
lock_file = lock_path.open("w")
390+
try:
391+
fcntl.flock(lock_file.fileno(), fcntl.LOCK_EX | fcntl.LOCK_NB)
392+
except OSError:
393+
lock_file.close()
394+
return
395+
_scheduler_lock_handle = lock_file
396+
refresh_schedule()
397+
scheduler.start()
398+
_scheduler_started = True
399+
400+
401+
@app.before_request
402+
def _start_scheduler_once():
403+
start_scheduler()
404+
405+
368406
@app.route("/")
369407
def index():
370408
return render_template("index.html", intervals=sorted(INTERVAL_SECONDS.keys()))
@@ -508,6 +546,5 @@ def browse():
508546

509547

510548
if __name__ == "__main__":
511-
refresh_schedule()
512-
scheduler.start()
549+
start_scheduler()
513550
app.run(host="0.0.0.0", port=int(os.getenv("PORT", "5000")))

requirements.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
Flask==3.0.2
1+
Flask==3.0.3
22
APScheduler==3.10.4
3-
paramiko==3.4.0
3+
paramiko==3.5.0
4+
gunicorn==22.0.0

0 commit comments

Comments
 (0)