engine-core.py 4.07 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
#!/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
28
import meta
29
30
31
import signal
import logging
import unittest
32
import subprocess
33

34
35
from flask               import Flask
from flask_sqlalchemy    import SQLAlchemy
36
37
38

from modules.base.logger import AuraLogger
from modules.base.config import AuraConfig
39
from modules.base.utils  import SimpleUtil
40
41
42
43
44
45
46
47
48
49
50
51
52


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)

53
54

class AuraEngine:
55
    """
56
57
58
59
60
    AuraEngine does the following:

    1. Initialize the soundsystem and scheduler
    2. Initialize Redis
    3. Start Liquidsoap in a separate thread which connects to the engine
61
62
63
64
65
66
67
68

    """
    logger = None
    config = None
    server = None
    messenger = None
    controller = None
    scheduler = None
69
    lqs = None
70
71
72
73


    def __init__(self):
        """
74
        Initializes Engine Core.
75
76
77
78
79
80
81
82
        """
        self.config = config
        AuraLogger(self.config)
        self.logger = logging.getLogger("AuraEngine")


    def startup(self):
        """
83
        Starts Engine Core.
84
85
86
87
88
89
90
        """
        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:
91
            AuraScheduler(self.config, None, None)
92
93
94

        # Create scheduler and Liquidsoap communicator
        self.soundsystem = SoundSystem(self.config)
95
        self.scheduler = AuraScheduler(self.config, self.soundsystem, self.on_initialized)
96
97
98
99
100
101
102
103
104
105
106

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

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



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
    def on_initialized(self):
        """
        Called when the engine is initialized, before the Liquidsoap connection is established."
        """
        self.logger.info(SimpleUtil.green("Engine Core initialized - Waiting for Liquidsoap connection ..."))
        cwd = os.getcwd()
        cwd += "/modules/liquidsoap/" 
        command = "/home/david/.opam/4.08.0/bin/liquidsoap"
        self.lqs = subprocess.Popen([command, "engine.liq"], cwd=cwd, stdout=subprocess.PIPE, shell=False)



    def exit_gracefully(self, signum, frame):
        """
        Shutdown of the engine. Also terminates the liquidsoap thread.
        """
        self.lqs.terminate()
        self.logger.info("Gracefully terminated Aura Engine!" + str(self.lqs))



128
129
130
131
132
#
# START THE ENGINE
#


133
134
135
136
137
if __name__ == "__main__":        
    engine = AuraEngine()

    signal.signal(signal.SIGINT, engine.exit_gracefully)
    signal.signal(signal.SIGTERM, engine.exit_gracefully)
138
139
140

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

145
    engine.startup()