-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp_setup.py
More file actions
68 lines (55 loc) · 2.33 KB
/
app_setup.py
File metadata and controls
68 lines (55 loc) · 2.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import logging
import sys
import time
import psycopg
import redis
from magplex import Locale
from magplex.database.database import PostgresConnection, PostgresPool, RedisPool
from magplex.database.migrations import migrations
from magplex.utilities import logs
from magplex.utilities.scheduler import TaskManager, wake_scheduler
from magplex.utilities.variables import Environment
from version import version
def initialize():
# Initialize logging.
logs.initialize()
logging.info(f"MagPlex v{version} by LegendaryFire")
# Check & make sure all environment variables are provided.
if not Environment.valid():
logging.error("Missing environment variables.")
sys.exit()
# Test Redis cache connection.
cache_conn = RedisPool.get_connection()
try:
cache_conn.ping()
logging.info(f"Connected to Redis database at {Environment.REDIS_HOST}:{Environment.REDIS_PORT}.")
except redis.exceptions.RedisError:
logging.error(f"Unable to connect to Redis server at {Environment.REDIS_HOST}:{Environment.REDIS_PORT}.")
time.sleep(30)
sys.exit()
# Test Postgres database connection.
try:
conn = PostgresConnection().get_connection(use_pool=False)
with conn.cursor() as cursor:
cursor.execute("SELECT 1")
cursor.fetchone()
conn.close()
logging.info(f"Connected to Postgres database at {Environment.POSTGRES_HOST}:{Environment.POSTGRES_PORT}.")
except psycopg.Error:
logging.error(f"Unable to connect to Postgres server at {Environment.POSTGRES_HOST}:{Environment.POSTGRES_PORT}.")
time.sleep(30)
sys.exit()
# Create database if it doesn't already exist.
logging.info("Creating Postgres database schema if it doesn't already exist.")
migrations.create_database()
migrations.run_missing_migrations()
# TODO: Initialize all devices on startup, and add their background tasks to the queue.
PostgresPool.close_pool()
def run_scheduler():
# Start background task scheduler.
scheduler = TaskManager.get_scheduler()
scheduler.remove_all_jobs()
scheduler.add_job(wake_scheduler, 'interval', id="wake_scheduler", seconds=5, replace_existing=True)
logging.info(Locale.TASK_JOB_ADDED_SUCCESSFULLY(job="wake_scheduler"))
if not scheduler.running:
scheduler.start()