diff --git a/Jenkinsfile b/Jenkinsfile index 26a5134..0cdc31e 100755 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -4,6 +4,8 @@ pipeline { environment { TS = credentials('jc_turnstile') DISCORD = credentials('jc_discord') + DISCORD_ERR_STAGING('jc_discord_err_staging') + DISCORD_ERR_PROD('jc_discord_err_prod') } stages{ @@ -58,7 +60,7 @@ pipeline { sh "docker pull registry.jakecharman.co.uk/jakecharman.co.uk:latest" sh "docker stop jake || true" sh "docker rm jake || true" - sh "docker run --name jake -e DISCORD_WEBHOOK=$DISCORD -e TURNSTILE_SECRET=$TS --restart always --network containers_default -v /opt/containers/jc/projects/:/var/www/jc/projects/ -d registry.jakecharman.co.uk/jakecharman.co.uk:latest" + sh "docker run --name jake -e DISCORD_ERR_HOOK=$DISCORD_ERR_STAGING -e DISCORD_WEBHOOK=$DISCORD -e TURNSTILE_SECRET=$TS --restart always --network containers_default -v /opt/containers/jc/projects/:/var/www/jc/projects/ -d registry.jakecharman.co.uk/jakecharman.co.uk:latest" } } } @@ -98,7 +100,7 @@ pipeline { sh "docker pull registry.jakecharman.co.uk/jakecharman.co.uk:latest" sh "docker stop jake || true" sh "docker rm jake || true" - sh "docker run --name jake -e DISCORD_WEBHOOK=$DISCORD -e TURNSTILE_SECRET=$TS --restart always --network containers_default -v /opt/containers/jc/projects/:/var/www/jc/projects/ -d registry.jakecharman.co.uk/jakecharman.co.uk:latest" + sh "docker run --name jake -e DISCORD_ERR_HOOK=$DISCORD_ERR_PROD -e DISCORD_WEBHOOK=$DISCORD -e TURNSTILE_SECRET=$TS --restart always --network containers_default -v /opt/containers/jc/projects/:/var/www/jc/projects/ -d registry.jakecharman.co.uk/jakecharman.co.uk:latest" sh "/home/jenkins/clearCFCache/clearCache.py a514fb61e1413b88aabbb19df16b8508" } } diff --git a/run_local.sh b/run_local.sh index 1c0cb72..7a4b6ed 100755 --- a/run_local.sh +++ b/run_local.sh @@ -9,5 +9,4 @@ if [[ -d "../jc-content" ]]; then content_dir="-v $(realpath ../jc-content):/var/www/jc/projects" fi -docker run -v $(pwd)/src/:/var/www/jc $content_dir jc-ng-localtest - +docker run -e DISCORD_ERR_HOOK=dummy $1 -v $(pwd)/src/:/var/www/jc $content_dir jc-ng-localtest diff --git a/src/index.py b/src/index.py index 51a84c8..3f91708 100755 --- a/src/index.py +++ b/src/index.py @@ -1,12 +1,49 @@ #!/usr/bin/python3 from flask import Flask, render_template, Response +import traceback +from os import environ +import threading +from requests import post +import logging app = Flask(__name__) import projects import contact +class DiscordLogger(logging.Handler): + ''' Simple logging handler to send a message to Discord ''' + + level = logging.ERROR + + def __init__(self, webhook): + super().__init__() + self._webhook = webhook + + def send_to_discord(self, msg: logging.LogRecord): + ''' Send the message ''' + if msg.exc_info is not None: + message_to_send = f'{msg.msg}\n\n{"".join(traceback.format_exception(*msg.exc_info))}' + else: + message_to_send = msg.msg + if len(message_to_send) > 2000: + chars_to_lose = len(message_to_send) - 2000 + if msg.exc_info is not None: + message_to_send = f'{msg.msg}\n\n{"".join(traceback.format_exception(*msg.exc_info))[-chars_to_lose:]}' + else: + message_to_send = msg.msg[-chars_to_lose:] + post(self._webhook, data={'content': message_to_send}, timeout=30) + + def emit(self, record: logging.LogRecord) -> None: + ''' Take in the record and start a new thread to send it to Discord ''' + app.logger.info('Sending error to Discord') + dc_thread = threading.Thread(target=self.send_to_discord, args=[record]) + dc_thread.start() + +discord_logger = DiscordLogger(environ['DISCORD_ERR_HOOK']) +app.logger.addHandler(discord_logger) + @app.route('/') def index(): return render_template('index.html')