feat: Add some background server for health checks
This commit is contained in:
parent
51cd23ed84
commit
f8c364e874
29
main.py
29
main.py
@ -9,6 +9,8 @@ import asyncio
|
||||
import signal
|
||||
import dotenv
|
||||
import os
|
||||
from flask import Flask, jsonify
|
||||
import threading
|
||||
|
||||
dotenv.load_dotenv()
|
||||
|
||||
@ -69,7 +71,6 @@ def saveTransaction(txData,blockHeight):
|
||||
cursor.execute("SELECT * FROM transactions WHERE hash = %s", (txData["hash"],))
|
||||
txExists = cursor.fetchone()
|
||||
if txExists:
|
||||
print(f"\nTransaction {txData['hash']} already exists in database.")
|
||||
return
|
||||
|
||||
cursor.execute("INSERT INTO transactions (hash, witnessHash, fee, rate, mtime, block, `index`, version, inputs, outputs, locktime, hex) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)", (txData["hash"], txData["witnessHash"], txData["fee"], txData["rate"], txData["mtime"], blockHeight, txData["index"], txData["version"], json.dumps(txData["inputs"]), json.dumps(txData["outputs"]), txData["locktime"], txData["hex"]))
|
||||
@ -87,7 +88,6 @@ def saveBlock(blockData):
|
||||
cursor.execute("SELECT * FROM blocks WHERE height = %s", (blockData["height"],))
|
||||
blockExists = cursor.fetchone()
|
||||
if blockExists:
|
||||
print(f"Block {blockData['height']} already exists in database.")
|
||||
return
|
||||
|
||||
cursor.execute("INSERT INTO blocks (hash, height, depth, version, prevBlock, merkleRoot, witnessRoot, treeRoot, reservedRoot, time, bits, nonce, extraNonce, mask, txs) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)", (blockData["hash"], blockData["height"], blockData["depth"], blockData["version"], blockData["prevBlock"], blockData["merkleRoot"], blockData["witnessRoot"], blockData["treeRoot"], blockData["reservedRoot"], blockData["time"], blockData["bits"], blockData["nonce"], blockData["extraNonce"], blockData["mask"], json.dumps(hashes)))
|
||||
@ -255,12 +255,14 @@ class CatchUp:
|
||||
self.targetHeight = targetHeight
|
||||
self.running = True
|
||||
self.closing = False
|
||||
self.interupted = False
|
||||
|
||||
async def catchUp(self):
|
||||
print(f"Catching up from {self.currentHeight} to {self.targetHeight}")
|
||||
def signal_handler(sig, frame):
|
||||
self.interupted = True
|
||||
self.stop()
|
||||
print("Caught Ctrl+C")
|
||||
print("\n\nCaught Ctrl+C\n")
|
||||
|
||||
signal.signal(signal.SIGINT, signal_handler)
|
||||
asyncio.create_task(self.loop())
|
||||
@ -294,7 +296,26 @@ class CatchUp:
|
||||
|
||||
|
||||
|
||||
# region Server
|
||||
app = Flask(__name__)
|
||||
|
||||
@app.route("/")
|
||||
def ping():
|
||||
return jsonify({"status": "OK"})
|
||||
|
||||
def run_server():
|
||||
app.run(host='0.0.0.0', port=3000)
|
||||
|
||||
def start_flask_in_thread():
|
||||
"""Starts the Flask server in a background thread."""
|
||||
flask_thread = threading.Thread(target=run_server)
|
||||
flask_thread.daemon = True # Ensures that the Flask thread will exit when the main program exits
|
||||
flask_thread.start()
|
||||
# endregion
|
||||
|
||||
if __name__ == "__main__":
|
||||
# Webserver in background
|
||||
start_flask_in_thread()
|
||||
setupDB()
|
||||
# Check if DB needs to catch up
|
||||
newestBlock = getFirstMissingBlock()
|
||||
@ -311,6 +332,8 @@ if __name__ == "__main__":
|
||||
print(f"Database is out of sync. Catching up from {newestBlock} to {NodeHeight}")
|
||||
catchUpper = CatchUp(newestBlock, NodeHeight)
|
||||
asyncio.run(catchUpper.catchUp())
|
||||
if catchUpper.interupted:
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
|
||||
|
@ -1,3 +1,4 @@
|
||||
mysql-connector-python
|
||||
requests
|
||||
python-dotenv
|
||||
python-dotenv
|
||||
flask
|
Loading…
Reference in New Issue
Block a user