generated from nathanwoodburn/python-webserver-template
feat: Add scheduler and fix some types
All checks were successful
Build Docker / BuildImage (push) Successful in 58s
All checks were successful
Build Docker / BuildImage (push) Successful in 58s
This commit is contained in:
@@ -9,7 +9,7 @@ RUN --mount=type=cache,target=/root/.cache/pip \
|
|||||||
COPY . /app
|
COPY . /app
|
||||||
|
|
||||||
# Optionally mount /data to store the data
|
# Optionally mount /data to store the data
|
||||||
# VOLUME /data
|
VOLUME /app/data
|
||||||
|
|
||||||
ENTRYPOINT ["python3"]
|
ENTRYPOINT ["python3"]
|
||||||
CMD ["main.py"]
|
CMD ["main.py"]
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ def handle_alert(domain: str, notification: dict, alert_data: dict):
|
|||||||
print(f"Unknown alert type: {alert_type} for domain: {domain}")
|
print(f"Unknown alert type: {alert_type} for domain: {domain}")
|
||||||
|
|
||||||
|
|
||||||
def discord_webhook(webhook_url: str, domain: str, content: str, alert_blocks: int):
|
def discord_webhook(webhook_url: str, domain: str, content: dict, alert_blocks: int):
|
||||||
"""
|
"""
|
||||||
Send a message to a Discord webhook.
|
Send a message to a Discord webhook.
|
||||||
"""
|
"""
|
||||||
|
|||||||
@@ -140,7 +140,6 @@ def notify_expiries():
|
|||||||
if not domains:
|
if not domains:
|
||||||
print("No domains found.")
|
print("No domains found.")
|
||||||
return
|
return
|
||||||
|
|
||||||
current_block = get_current_block()
|
current_block = get_current_block()
|
||||||
|
|
||||||
|
|
||||||
@@ -154,7 +153,6 @@ def notify_expiries():
|
|||||||
"time": f"{blocks_remaining // 144} days" # Assuming 144 blocks per day
|
"time": f"{blocks_remaining // 144} days" # Assuming 144 blocks per day
|
||||||
}
|
}
|
||||||
for notification in domains[domain]:
|
for notification in domains[domain]:
|
||||||
print(blocks_remaining, notification['blocks'])
|
|
||||||
if notification['blocks'] <= blocks_remaining and notification['blocks'] >= (blocks_remaining - 1): # Just in case there are 2 blocks really close together
|
if notification['blocks'] <= blocks_remaining and notification['blocks'] >= (blocks_remaining - 1): # Just in case there are 2 blocks really close together
|
||||||
# Check if last block notified is more than current block + 5
|
# Check if last block notified is more than current block + 5
|
||||||
if notification.get('last_block_notified', -1) < (current_block - 5):
|
if notification.get('last_block_notified', -1) < (current_block - 5):
|
||||||
|
|||||||
26
main.py
26
main.py
@@ -4,6 +4,9 @@ import server
|
|||||||
from gunicorn.app.base import BaseApplication
|
from gunicorn.app.base import BaseApplication
|
||||||
import os
|
import os
|
||||||
import dotenv
|
import dotenv
|
||||||
|
import threading
|
||||||
|
import time
|
||||||
|
import domains
|
||||||
|
|
||||||
|
|
||||||
class GunicornApp(BaseApplication):
|
class GunicornApp(BaseApplication):
|
||||||
@@ -14,18 +17,35 @@ class GunicornApp(BaseApplication):
|
|||||||
|
|
||||||
def load_config(self):
|
def load_config(self):
|
||||||
for key, value in self.options.items():
|
for key, value in self.options.items():
|
||||||
if key in self.cfg.settings and value is not None:
|
if key in self.cfg.settings and value is not None: # type: ignore
|
||||||
self.cfg.set(key.lower(), value)
|
self.cfg.set(key.lower(), value) # type: ignore
|
||||||
|
|
||||||
def load(self):
|
def load(self):
|
||||||
return self.application
|
return self.application
|
||||||
|
|
||||||
|
def run_expiry_checker():
|
||||||
|
"""
|
||||||
|
Background function to run notify_expiries every 2 minutes.
|
||||||
|
"""
|
||||||
|
while True:
|
||||||
|
try:
|
||||||
|
print("Running expiry check...")
|
||||||
|
domains.notify_expiries()
|
||||||
|
print("Expiry check completed.")
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Error in expiry checker: {e}")
|
||||||
|
|
||||||
|
# Wait 2 minutes (120 seconds)
|
||||||
|
time.sleep(120)
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
dotenv.load_dotenv()
|
dotenv.load_dotenv()
|
||||||
|
|
||||||
|
# Start the background expiry checker
|
||||||
|
expiry_thread = threading.Thread(target=run_expiry_checker, daemon=True)
|
||||||
|
expiry_thread.start()
|
||||||
|
print("Started background expiry checker thread")
|
||||||
|
|
||||||
workers = os.getenv('WORKERS', 1)
|
workers = os.getenv('WORKERS', 1)
|
||||||
threads = os.getenv('THREADS', 2)
|
threads = os.getenv('THREADS', 2)
|
||||||
workers = int(workers)
|
workers = int(workers)
|
||||||
|
|||||||
24
server.py
24
server.py
@@ -15,11 +15,28 @@ import json
|
|||||||
import requests
|
import requests
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
import dotenv
|
import dotenv
|
||||||
|
import threading
|
||||||
|
import time
|
||||||
|
import domains
|
||||||
|
|
||||||
dotenv.load_dotenv()
|
dotenv.load_dotenv()
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
|
|
||||||
|
def run_expiry_checker():
|
||||||
|
"""
|
||||||
|
Background function to run notify_expiries every 2 minutes.
|
||||||
|
"""
|
||||||
|
while True:
|
||||||
|
try:
|
||||||
|
print("Running expiry check...")
|
||||||
|
domains.notify_expiries()
|
||||||
|
print("Expiry check completed.")
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Error in expiry checker: {e}")
|
||||||
|
|
||||||
|
# Wait 2 minutes (120 seconds)
|
||||||
|
time.sleep(120)
|
||||||
|
|
||||||
def find(name, path):
|
def find(name, path):
|
||||||
for root, dirs, files in os.walk(path):
|
for root, dirs, files in os.walk(path):
|
||||||
@@ -111,4 +128,9 @@ def not_found(e):
|
|||||||
|
|
||||||
# endregion
|
# endregion
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
app.run(debug=True, port=5000, host="0.0.0.0")
|
# Start the background expiry checker for development mode
|
||||||
|
expiry_thread = threading.Thread(target=run_expiry_checker, daemon=True)
|
||||||
|
expiry_thread.start()
|
||||||
|
print("Started background expiry checker thread")
|
||||||
|
|
||||||
|
app.run(debug=True, port=5000, host="127.0.0.1")
|
||||||
|
|||||||
Reference in New Issue
Block a user