Add scheduler and logging
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Michel Roux 2021-01-29 21:32:28 +01:00
parent 320207e3b0
commit a3abcbe28d
5 changed files with 36 additions and 24 deletions

View File

@ -1,18 +0,0 @@
import json
import requests
from pynyaata.config import CLOUDPROXY_ENDPOINT
json_session = requests.post(CLOUDPROXY_ENDPOINT, data=json.dumps({
'cmd': 'sessions.list'
}))
response = json.loads(json_session.text)
sessions = response['sessions']
for session in sessions:
requests.post(CLOUDPROXY_ENDPOINT, data=json.dumps({
'cmd': 'sessions.destroy',
'session': session
}))
print('Destroyed %s' % session)

View File

@ -2,10 +2,13 @@ from asyncio import get_event_loop, set_event_loop, SelectorEventLoop
from functools import wraps
from operator import attrgetter, itemgetter
from flask import redirect, render_template, request, url_for, abort
import requests
from flask import redirect, render_template, request, url_for, abort, json
from requests import RequestException
from . import utils
from .config import app, auth, ADMIN_USERNAME, ADMIN_PASSWORD, MYSQL_ENABLED, APP_PORT, IS_DEBUG
from .config import app, auth, logger, scheduler, ADMIN_USERNAME, ADMIN_PASSWORD, MYSQL_ENABLED, APP_PORT, IS_DEBUG, \
CLOUDPROXY_ENDPOINT
from .connectors import get_instance, run_all
from .connectors.core import ConnectorLang, ConnectorReturn
from .forms import SearchForm, DeleteForm, EditForm
@ -191,6 +194,28 @@ def admin_edit(link_id=None):
return render_template('admin/edit.html', search_form=SearchForm(), link=link, folders=folders, action_form=form)
@scheduler.task('interval', id='flaredestroyy', days=1)
def flaredestroyy():
if CLOUDPROXY_ENDPOINT:
try:
json_session = requests.post(CLOUDPROXY_ENDPOINT, data=json.dumps({
'cmd': 'sessions.list'
}))
response = json.loads(json_session.text)
sessions = response['sessions']
for session in sessions:
requests.post(CLOUDPROXY_ENDPOINT, data=json.dumps({
'cmd': 'sessions.destroy',
'session': session
}))
logger.info('Destroyed %s' % session)
except RequestException as e:
logger.exception(e)
def run():
app.config['SQLALCHEMY_ECHO'] = IS_DEBUG
scheduler.start()
flaredestroyy()
app.run('0.0.0.0', APP_PORT, IS_DEBUG)

View File

@ -1,7 +1,9 @@
import logging
from os import environ, urandom
from flask import Flask
from flask.cli import load_dotenv
from flask_apscheduler import APScheduler
from flask_httpauth import HTTPBasicAuth
from flask_sqlalchemy import SQLAlchemy
@ -23,6 +25,9 @@ app.debug = IS_DEBUG
app.secret_key = urandom(24).hex()
app.url_map.strict_slashes = False
auth = HTTPBasicAuth()
scheduler = APScheduler(app=app)
logging.basicConfig(level=(logging.DEBUG if IS_DEBUG else logging.INFO))
logger = logging.getLogger(app.name)
db_host = environ.get('MYSQL_SERVER')
if db_host:
@ -31,7 +36,7 @@ if db_host:
db_password = environ.get('MYSQL_PASSWORD')
db_name = environ.get('MYSQL_DATABASE')
if not db_user or not db_password or not db_name:
print('Missing connection environment variables')
logger.error('Missing connection environment variables')
exit()
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://%s:%s@%s/%s?charset=utf8mb4' % (
db_user, db_password, db_host, db_name

View File

@ -4,13 +4,12 @@ from datetime import datetime
from enum import Enum
from functools import wraps
from json import dumps, loads
from logging import getLogger
from urllib.parse import urlencode
import requests
from requests import RequestException
from ..config import CACHE_TIMEOUT, REQUESTS_TIMEOUT, CLOUDPROXY_ENDPOINT
from ..config import CACHE_TIMEOUT, REQUESTS_TIMEOUT, CLOUDPROXY_ENDPOINT, logger
cloudproxy_session = None
@ -128,7 +127,7 @@ def curl_content(url, params=None, ajax=False, debug=True):
cloudproxy_session = None
except RequestException as e:
if debug:
getLogger().exception(e)
logger.exception(e)
return {'http_code': http_code, 'output': output}

View File

@ -1,4 +1,5 @@
Flask==1.1.2
Flask-APScheduler==1.11.0
Flask-SQLAlchemy==2.4.4
Flask-HTTPAuth==4.2.0
Flask-WTF==0.14.3