aura.py 3.65 KB
Newer Older
1
#!/usr/bin/python3.5
Gottfried Gaisbauer's avatar
Gottfried Gaisbauer committed
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
#
#  engine
#
#  Playout Daemon for autoradio project
#
#
#  Copyright (C) 2017-2018 Gottfried Gaisbauer <gottfried.gaisbauer@servus.at>
#
#  This file is part of engine.
#
#  engine is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  any later version.
#
#  engine 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 General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with engine. If not, see <http://www.gnu.org/licenses/>.
#

27
import os
Gottfried Gaisbauer's avatar
Gottfried Gaisbauer committed
28
29
30
import sys
import signal
import unittest
31

32
33
from modules.scheduling.scheduler import AuraScheduler
from modules.communication.liquidsoap.communicator import LiquidSoapCommunicator
34
from modules.communication.redis.adapter import ServerRedisAdapter
35
from modules.web.routes import Routes
36
from modules.monitoring.diskspace_watcher import DiskSpaceWatcher
37
from libraries.base.logger import AuraLogger
38

Gottfried Gaisbauer's avatar
Gottfried Gaisbauer committed
39
40
41
from testing.test import TestConfig, TestLogger

class Aura(AuraLogger):
42
    server = None
43
    messenger = None
44
    controller = None
45

46
    # ------------------------------------------------------------------------------------------ #
47
    def __init__(self):
48
        super(Aura, self).__init__()
49

Gottfried Gaisbauer's avatar
Gottfried Gaisbauer committed
50
51
52
53
54
55
56
57
58
59
    def test_yourself(self):
        self.run_test(TestConfig)
        self.run_test(TestLogger)

    def run_test(self, testingClass):
        runner = unittest.TextTestRunner()
        result = runner.run(unittest.makeSuite(testingClass))

    def startup(self):

60
61
        server = object

Gottfried Gaisbauer's avatar
Gottfried Gaisbauer committed
62
        #        self.controller = AuraController(self.config)
63
64
65
        # create scheduler and ls_communicator
        self.liquidsoapcommunicator = LiquidSoapCommunicator(self.config)
        self.scheduler = AuraScheduler(self.config)
66

67
68
69
70
71
        # give both a reference of each other
        self.liquidsoapcommunicator.scheduler = self.scheduler
        self.scheduler.liquidsoapcommunicator = self.liquidsoapcommunicator

        # create the redis adapter
72
        self.messenger = ServerRedisAdapter()
73
74
75
        self.messenger.config = self.config
        self.messenger.scheduler = self.scheduler
        self.messenger.liquidsoapcommunicator = self.liquidsoapcommunicator
76

77
78
79
        self.diskspace_watcher = DiskSpaceWatcher(self.config, self.logger, self.liquidsoapcommunicator)
        self.diskspace_watcher.run()

80
81
82
83
84
85
        def receive_signal(signum, stack):
            print("received signal")
            server.reload()

        signal.signal(signal.SIGUSR1, receive_signal)

86
        # wait for redis message
Gottfried Gaisbauer's avatar
Gottfried Gaisbauer committed
87
        self.join_comm()
88

89
        # start the web service
90
        self.start_web_service()
Gottfried Gaisbauer's avatar
Gottfried Gaisbauer committed
91

92
    def join_comm(self):
93
        # start listener thread
94
        self.messenger.start()
95

96
    def start_web_service(self):
97
        try:
98
            self.logger.info("Listening on Port 5000 for API or Webcalls")
99
            Routes(self.scheduler, self.liquidsoapcommunicator, self.messenger)
100
101
102
        except OSError as e:
            self.messenger.halt()
            self.logger.critical("AuraEngine already running? Exception: " + e.strerror + ". Exiting...")
103
            os._exit(0)
104

105

106
107
108
109
110
# # ## ## ## ## ## # #
# # ENTRY FUNCTION # #
# # ## ## ## ## ## # #
def main():
    aura = Aura()
Gottfried Gaisbauer's avatar
Gottfried Gaisbauer committed
111
112
113
114
115
116
117

    if len(sys.argv) >= 2 and sys.argv[1] == "--na-gspiast-di-?":
        aura.test_yourself()
    else:
        aura.startup()


118
119
120
121
# # ## ## ## ## ## ## # #
# # End ENTRY FUNCTION # #
# # ## ## ## ## ## ## # #

122

123
if __name__ == "__main__":
Gottfried Gaisbauer's avatar
Gottfried Gaisbauer committed
124
    main()