blockstream-satellite-api/server/daemon.py
Blockstream Satellite 9d421771e7 Port the satellite API to python
- Preserve the SQLite database and use SQLAlchemy to wrap db
  interactions.
- Use Alembic for database migrations.
- Organize all the python modules on the new server/ directory.
- Use pytest for unit tests and organize test modules at server/tests/.
2021-07-20 12:28:08 -03:00

60 lines
1.5 KiB
Python

import logging
from flask import Flask
import constants
import invoice_helpers
import order_helpers
from database import db
from transmitter import TxEngine
from worker import Worker
ONE_MINUTE = 60
CLEANUP_DUTY_CYCLE = 5 * ONE_MINUTE # five minutes
def cleanup_database(app):
with app.app_context():
(expired_invoices,
expired_orders) = invoice_helpers.expire_unpaid_invoices()
expired_orders.extend(order_helpers.expire_old_pending_orders())
cleaned_up_orders = order_helpers.cleanup_old_message_files()
work = [
len(x)
for x in [expired_invoices, expired_orders, cleaned_up_orders]
]
if (any(work)):
logging.info("Database cleanup: expired {} invoices, "
"{} orders, and removed {} files".format(*work))
def create_app():
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = f'sqlite:///{constants.DB_FILE}'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config["REDIS_URL"] = constants.REDIS_URI
db.init_app(app)
with app.app_context():
db.create_all()
return app
def main():
logging.basicConfig(level=logging.DEBUG, format=constants.LOGGING_FORMAT)
app = create_app()
# Workers
Worker(period=CLEANUP_DUTY_CYCLE,
fcn=cleanup_database,
args=(app, ),
name="database cleaner")
# Main Tx Engine
with app.app_context():
tx_engine = TxEngine()
tx_engine.start()
if __name__ == '__main__':
main()