engine-core.py 3.32 KB
Newer Older
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/usr/bin/python3.7

#
# Aura Engine
#
# Copyright (C) 2017-2020    
#           David Trattnig <david.trattnig@subsquare.at>
#           Gottfried Gaisbauer <gottfried.gaisbauer@servus.at>

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.

# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.




import os
import sys
import signal
import logging
import unittest
import meta

from flask import Flask
from flask_sqlalchemy import SQLAlchemy
# from sqlalchemy.ext.declarative import declarative_base

from modules.base.logger import AuraLogger
from modules.base.config import AuraConfig
from modules.base.utils  import EngineUtil

# from modules.monitoring.diskspace_watcher import DiskSpaceWatcher

config = AuraConfig()
def configure_flask():
    app.config["SQLALCHEMY_DATABASE_URI"] = config.get_database_uri()
    app.config['BABEL_DEFAULT_LOCALE'] = 'de'
    app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False

# FIXME Instatiate SQLAlchemy without the need for Flask
app = Flask(__name__)
configure_flask()
DB = SQLAlchemy(app)
# Base = declarative_base()

class Aura:
    """
    Aura Class

    The core of Aura Engine.
    """
    logger = None
    config = None
    server = None
    messenger = None
    controller = None
    scheduler = None


    def __init__(self):
        """
        Initializes the Engine Core.
        """
        self.config = config
        AuraLogger(self.config)
        self.logger = logging.getLogger("AuraEngine")


    def startup(self):
        """
        Starts the Engine Core.
        """
        from modules.scheduling.scheduler import AuraScheduler
        from modules.core.engine import SoundSystem
        from modules.communication.redis.adapter import ServerRedisAdapter

        # Check if the database has to be re-created
        if self.config.get("recreate_db") is not None:
            AuraScheduler(self.config, None)

        # Create scheduler and Liquidsoap communicator
        self.soundsystem = SoundSystem(self.config)
        self.scheduler = AuraScheduler(self.config, self.soundsystem)

        # Create the Redis adapter
        self.messenger = ServerRedisAdapter(self.config)
        self.messenger.scheduler = self.scheduler
        self.messenger.soundsystem = self.soundsystem

        # TODO Check if it's working / needed.
        #self.diskspace_watcher = DiskSpaceWatcher(self.config, self.logger, self.soundsystem)
        #self.diskspace_watcher.start()

        # And finally wait for redis message / start listener thread
        self.messenger.start()



#
# START THE ENGINE
#


if __name__ == "__main__":
    aura = Aura()

    if len(sys.argv) >= 2:
        if "--use-test-data" in sys.argv:
            aura.config.set("use_test_data", True)
        if "--recreate-database" in sys.argv:
            aura.config.set("recreate_db", True)

    aura.startup()