add scheduled backup of docker stats to pg

This commit is contained in:
simon 2022-06-06 15:19:23 +07:00
parent 7fdb73a452
commit 0c1c68a718
No known key found for this signature in database
GPG Key ID: 2C15AA5E89985DD4
4 changed files with 27 additions and 1 deletions

View File

@ -1,6 +1,9 @@
TZ=America/New_York
REDDIT_HOOK_URL=https://discordapp.com/api/webhooks/000000000000000000/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa REDDIT_HOOK_URL=https://discordapp.com/api/webhooks/000000000000000000/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
ROADMAP_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 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 NOTIFICATION_TEST_HOOK_URL=https://discord.com/api/webhooks/000000000000000000/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
GH_HOOK_SECRET=xxxxxxxxxxxxxxxxxxxxxxxx GH_HOOK_SECRET=xxxxxxxxxxxxxxxxxxxxxxxx
DOCKER_HOOK_SECRET=yyyyyyyyyyyyyyyyyyyyyyyy DOCKER_HOOK_SECRET=yyyyyyyyyyyyyyyyyyyyyyyy

View File

@ -1,7 +1,8 @@
APScheduler==3.9.1
flask==2.1.2 flask==2.1.2
ipython==8.4.0 ipython==8.4.0
markdown==3.3.7 markdown==3.3.7
psycopg2==2.9.3 psycopg2==2.9.3
redis==4.3.1 redis==4.3.3
requests==2.27.1 requests==2.27.1
uWSGI==2.0.20 uWSGI==2.0.20

View File

@ -19,9 +19,11 @@ class DockerBackup:
def run_backup(self): def run_backup(self):
"""public method to run""" """public method to run"""
print("start hourly docker stats backup")
self._get_image_stats() self._get_image_stats()
self._build_query() self._build_query()
self._insert_line() self._insert_line()
print("completed hourly docker stats backup")
def _get_image_stats(self): def _get_image_stats(self):
"""return dict for image""" """return dict for image"""
@ -58,3 +60,8 @@ class DockerBackup:
handler = DatabaseConnect() handler = DatabaseConnect()
handler.db_execute(self.query) handler.db_execute(self.query)
handler.db_close() handler.db_close()
def run_docker_backup():
"""hourly task to store docker stats"""
DockerBackup().run_backup()

View File

@ -1,12 +1,27 @@
"""holds all views and api endpoints""" """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 flask import Flask, render_template, jsonify, request
from src.api_docker import run_docker_backup
from src.webhook_docker import DockerHook from src.webhook_docker import DockerHook
from src.webhook_github import GithubBackup, GithubHook from src.webhook_github import GithubBackup, GithubHook
import markdown import markdown
app = Flask(__name__) 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("/") @app.route("/")
def home(): def home():