From 0c1c68a718691451322a86ff16508da950d4156c Mon Sep 17 00:00:00 2001 From: simon Date: Mon, 6 Jun 2022 15:19:23 +0700 Subject: [PATCH] add scheduled backup of docker stats to pg --- env/tubearchivist.sample.env | 3 +++ tubearchivist/web/requirements.txt | 3 ++- tubearchivist/web/src/api_docker.py | 7 +++++++ tubearchivist/web/views.py | 15 +++++++++++++++ 4 files changed, 27 insertions(+), 1 deletion(-) diff --git a/env/tubearchivist.sample.env b/env/tubearchivist.sample.env index 84c49ac..ce3afae 100644 --- a/env/tubearchivist.sample.env +++ b/env/tubearchivist.sample.env @@ -1,6 +1,9 @@ +TZ=America/New_York REDDIT_HOOK_URL=https://discordapp.com/api/webhooks/000000000000000000/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ROADMAP_HOOK_URL=https://discordapp.com/api/webhooks/000000000000000000/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa DOCKER_UNSTABLE_HOOK_URL=https://discordapp.com/api/webhooks/000000000000000000/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +GITHUB_RELEASE_HOOK_URL=https://discord.com/api/webhooks/000000000000000000/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa NOTIFICATION_TEST_HOOK_URL=https://discord.com/api/webhooks/000000000000000000/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + GH_HOOK_SECRET=xxxxxxxxxxxxxxxxxxxxxxxx DOCKER_HOOK_SECRET=yyyyyyyyyyyyyyyyyyyyyyyy \ No newline at end of file diff --git a/tubearchivist/web/requirements.txt b/tubearchivist/web/requirements.txt index b02856e..e1a048f 100644 --- a/tubearchivist/web/requirements.txt +++ b/tubearchivist/web/requirements.txt @@ -1,7 +1,8 @@ +APScheduler==3.9.1 flask==2.1.2 ipython==8.4.0 markdown==3.3.7 psycopg2==2.9.3 -redis==4.3.1 +redis==4.3.3 requests==2.27.1 uWSGI==2.0.20 diff --git a/tubearchivist/web/src/api_docker.py b/tubearchivist/web/src/api_docker.py index 982d133..ef5c415 100644 --- a/tubearchivist/web/src/api_docker.py +++ b/tubearchivist/web/src/api_docker.py @@ -19,9 +19,11 @@ class DockerBackup: def run_backup(self): """public method to run""" + print("start hourly docker stats backup") self._get_image_stats() self._build_query() self._insert_line() + print("completed hourly docker stats backup") def _get_image_stats(self): """return dict for image""" @@ -58,3 +60,8 @@ class DockerBackup: handler = DatabaseConnect() handler.db_execute(self.query) handler.db_close() + + +def run_docker_backup(): + """hourly task to store docker stats""" + DockerBackup().run_backup() diff --git a/tubearchivist/web/views.py b/tubearchivist/web/views.py index 8aaf195..92b8756 100644 --- a/tubearchivist/web/views.py +++ b/tubearchivist/web/views.py @@ -1,12 +1,27 @@ """holds all views and api endpoints""" +from os import environ + +from apscheduler.schedulers.background import BackgroundScheduler from flask import Flask, render_template, jsonify, request +from src.api_docker import run_docker_backup from src.webhook_docker import DockerHook from src.webhook_github import GithubBackup, GithubHook import markdown app = Flask(__name__) +scheduler = BackgroundScheduler(timezone=environ.get("TZ")) +scheduler.add_job( + run_docker_backup, + trigger="cron", + day="*", + hour="*", + minute="2", + name="docker_backup", +) +scheduler.start() + @app.route("/") def home():