Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F60883260
sausage-api
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Fri, May 3, 03:46
Size
2 KB
Mime Type
text/x-python
Expires
Sun, May 5, 03:46 (1 d, 23 h)
Engine
blob
Format
Raw Data
Handle
17392998
Attached To
R11586 sausage-api
sausage-api
View Options
#!/usr/bin/python3
# © All rights reserved. ECOLE POLYTECHNIQUE FEDERALE DE LAUSANNE,
# Switzerland
# SCITAS - Scientific IT and Application Support, 2021
# See the LICENSE.txt file for more details.
import falcon
import gunicorn.app.base
import multiprocessing
import time
import threading
import requests
import logging
from sys import exit
from os import getpid
from sausage.middleware import Sausage
from sausage.readconf import ReadConf
class UpdateDb(object):
def __init__(self):
server_opt = ReadConf()
self.server = server_opt.bind_addr + ":" + server_opt.bind_port
self.reponse = {}
# create logger
self.logger = logging.getLogger('UpdateDb')
self.logger.setLevel(logging.DEBUG)
# create console handler and set level to debug
lconf = logging.StreamHandler()
lconf.setLevel(logging.DEBUG)
formatter = logging.Formatter(
'[%(asctime)s] [%(name)s] [%(levelname)s] %(message)s',
datefmt='%Y-%m-%d %H:%M:%S')
lconf.setFormatter(formatter)
self.logger.addHandler(lconf)
self.exe = threading.Thread(target=self.infinite)
self.exe.daemon = True
self.exe.start()
def infinite(self):
init = 0
pid = getpid()
try:
while True:
if init == 1:
timeout = 180
else:
timeout = 3
init = 1
# Hack to catch sigint in the self thread
try:
time.sleep(timeout)
except (KeyboardInterrupt, SystemExit):
exit(0)
# End of hack
self.response = requests.get(
'http://' + self.server + '/database/update')
req_state = self.response.json()
if req_state['return'] == 'Database connection error':
self.logger.warning(req_state['return'])
else:
self.logger.info('Update Database with pid ' + str(pid))
except Exception:
self.logger.error('Error updating records in database')
exit(1)
class GunicornApp(gunicorn.app.base.BaseApplication):
def __init__(self, app, options=None):
self.options = options or {}
self.application = app
super().__init__()
def load_config(self):
config = {key: value for key, value in self.options.items()
if key in self.cfg.settings and value is not None}
for key, value in config.items():
self.cfg.set(key.lower(), value)
def load(self):
return self.application
if __name__ == '__main__':
sausage_api = Sausage()
server_opt = ReadConf()
options = {
'bind': '%s:%s' % (server_opt.bind_addr, server_opt.bind_port),
'workers': multiprocessing.cpu_count(),
}
updtdb = UpdateDb()
GunicornApp(sausage_api, options).run()
Event Timeline
Log In to Comment