run.py 3.07 KB
Newer Older
1
#!/bin/sh
2
3
''''which python3.9 >/dev/null 2>&1 && exec python3.9 "$0" "$@"     # '''
''''which python3.8  >/dev/null 2>&1 && exec python3.8  "$0" "$@"   # '''
4
''''exec echo "Error: Snaaakey Python, where are you?"                # '''
5
6

#
David Trattnig's avatar
David Trattnig committed
7
8
9
# Aura Engine (https://gitlab.servus.at/aura/engine)
#
# Copyright (C) 2017-2020 - The Aura Engine Team.
10
11
12
13
14
#
# 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.
David Trattnig's avatar
David Trattnig committed
15
#
16
17
18
19
# 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.
David Trattnig's avatar
David Trattnig committed
20
#
21
22
23
24
25
26
27
28
29
# 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

30
31
from flask               import Flask
from flask_sqlalchemy    import SQLAlchemy
32

33
34
35
from src.base.logger import AuraLogger
from src.base.config import AuraConfig
from src.base.utils  import SimpleUtil as SU
36
37
38
39
40
41
42
43


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

Lars Kruse's avatar
Lars Kruse committed
44
# FIXME Instantiate SQLAlchemy without the need for Flask
45
46
47
48
app = Flask(__name__)
configure_flask()
DB = SQLAlchemy(app)

49

50
class EngineRunner:
51
    """
52
    EngineRunner is in charge of starting the engine.
53
54
55
    """
    logger = None
    config = None
David Trattnig's avatar
David Trattnig committed
56
    engine = None
57
    lqs = None
58
59
60
61


    def __init__(self):
        """
62
        Constructor
63
64
        """
        self.config = config
David Trattnig's avatar
David Trattnig committed
65
66
67
        AuraLogger(self.config)
        self.logger = logging.getLogger("AuraEngine")
        
68

69
    def run(self):
70
        """
71
        Starts Engine Core.
72
        """                        
David Trattnig's avatar
David Trattnig committed
73
        from src.engine import Engine
74
        self.engine = Engine()
75

76

77
78
79
80
81
82
83
84
    def recreate_db(self):
        """
        Initializes the database and deletes any existing content.
        """
        from src.scheduling.models import AuraDatabaseModel
        AuraDatabaseModel.recreate_db()


85
86
    def exit_gracefully(self, signum, frame):
        """
87
        Shutdown of the engine. Also terminates the Liquidsoap thread.
88
        """
89
90
91
        if self.engine:
            self.engine.terminate()

92
93
94
95
        if self.lqs:
            self.lqs.terminate()
            self.logger.info("Terminated Liquidsoap")

David Trattnig's avatar
David Trattnig committed
96
        self.logger.info("Gracefully terminated Aura Engine! (signum:%s, frame:%s)" % (signum, frame))
97
        sys.exit(0)        
98
99
100



101
102
103
104
105
#
# START THE ENGINE
#


106
if __name__ == "__main__":        
107
108
109
    runner = EngineRunner()
    signal.signal(signal.SIGINT, runner.exit_gracefully)
    signal.signal(signal.SIGTERM, runner.exit_gracefully)
110
111
112

    if len(sys.argv) >= 2:
        if "--recreate-database" in sys.argv:
113
114
            runner.recreate_db()
            sys.exit(0)
115
116
         
    runner.run()