This repository has been archived by the owner on May 7, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ofw.py
169 lines (131 loc) · 4.46 KB
/
ofw.py
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import getpass
import subprocess
import threading
import multiprocessing
from optimization.pyroServerManagement import PyroServerManagement
from utils_intern.messageLogger import MessageLogger
from swagger_server.wsgi import StandaloneApplication
"""
Created by Gustavo Aragón on 14.03.2018
"""
import configparser
import os
import signal
import sys
import shutil
# import swagger_server.__main__ as webserver
import time
import swagger_server.wsgi as webserver
from IO.ZMQClient import ForwarderDevice
from config.configUpdater import ConfigUpdater
"""
Get the address of the data.dat
"""
class GracefulKiller:
kill_now = False
signals = {
signal.SIGINT: 'SIGINT',
signal.SIGTERM: 'SIGTERM'
}
def __init__(self):
signal.signal(signal.SIGINT, self.exit_gracefully)
signal.signal(signal.SIGTERM, self.exit_gracefully)
def exit_gracefully(self, signum, frame):
print("\nReceived {} signal".format(self.signals[signum]))
print("Cleaning up resources. End of the program")
from IO.redisDB import RedisDB
redisDB = RedisDB()
redisDB.set("End ofw", "True")
time.sleep(6)
self.kill_now = True
"""def sigterm(x, y):
print('SIGTERM received, time to leave for OFW')
from IO.redisDB import RedisDB
redisDB = RedisDB()
redisDB.set("End ofw", "True")
# Register the signal to the handler
signal.signal(signal.SIGTERM, sigterm) # Used by this script"""
def startOfw(options):
# code to start a daemon
init = 0
def parseArgs():
mandatoryArgs = 0
def main():
global OPTIONS
logger, redisDB, config = setup()
logger.debug("###################################")
logger.info("OFW started")
logger.debug("###################################")
logger.debug("Starting name server and dispatch server")
number_of_workers = config.getint("IO", "number.of.gunicorn.workers", fallback=-1)
p = multiprocessing.Process(target=StandaloneApplication.main, args=(number_of_workers,))
p.start()
logger.info("Starting webserver")
killer = GracefulKiller()
#webserver.main()
while not killer.kill_now:
time.sleep(1)
p.join(2)
# while True:
# results=opt.start()
# print(results)
# time.sleep(5)
"""def signal_handler(sig, frame):
print('You pressed Ctrl+C!')
print(sig)
if zmqForwarder:
print("stopping zmq forwarder")
zmqForwarder.Stop()
sys.exit(0)"""
zmqForwarder = None
def setup():
config_path = "/usr/src/app/optimization/resources/ConfigFile.properties"
config_path_default = "/usr/src/app/config/ConfigFile.properties"
config, logger = ConfigUpdater.get_config_and_logger("ofw", config_path_default, config_path)
redisDB = clear_redis(logger)
redisDB.set("End ofw", "False")
copy_models()
copy_pv_files()
copy_env_varibles()
#logger.debug("env = "+str(os.environ))
zmqHost = config.get("IO", "zmq.host")
pubPort = config.get("IO", "zmq.pub.port")
subPort = config.get("IO", "zmq.sub.port")
zmqForwarder = ForwarderDevice(zmqHost, pubPort, subPort)
zmqForwarder.start()
return (logger, redisDB, config)
def copy_pv_files():
pv_path = "/usr/src/app/utils/pv_data"
if os.path.exists(pv_path):
for file in os.listdir(pv_path):
file_path = os.path.join(pv_path, file)
if os.path.isfile(file_path) and ".txt" in file:
shutil.copyfile(file_path, os.path.join("/usr/src/app/prediction/resources", file))
def copy_models():
models_path = "/usr/src/app/optimization/resources/models"
if os.path.exists(models_path):
for file in os.listdir(models_path):
file_path = os.path.join(models_path, file)
if os.path.isfile(file_path) and ".py" in file:
shutil.copyfile(file_path, os.path.join("/usr/src/app/optimization/models", file))
def clear_redis(logger):
logger.info("reset redis")
from IO.redisDB import RedisDB
redisDB = RedisDB()
redisDB.reset()
redisDB.set("time", time.time())
redisDB.set("End ofw", "False")
return redisDB
def copy_env_varibles():
with open("/usr/src/app/utils_intern/env_var.txt", "r") as f:
rows = f.readlines()
for row in rows:
if len(row) > 0:
row = row.replace("\n","")
s = row.split("=")
if len(s) == 1:
s.append("")
os.environ[s[0]] = str(s[1])
if __name__ == "__main__":
# execute only if run as a script
main()