Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Made the database connections work, introduced pool recycling and sorted out the timezone issues by using utc #92

Closed
wants to merge 18 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions runnumber-rest/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
import flask
from flask_restful import Api, Resource
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy import func
from sqlalchemy import func, event
import re

__all__ = ["app", "api", "db"]

Expand All @@ -32,6 +33,7 @@
DEPLOYMENT_ENV=os.environ.get("DEPLOYMENT_ENV", "DEV"),
RUN_START=int(os.getenv("RUN_START", "1000")),
SQLALCHEMY_ECHO=False,
SQLALCHEMY_ENGINE_OPTIONS={"pool_pre_ping": True, "pool_recycle": 3600},
)

uri = app.config["SQLALCHEMY_DATABASE_URI"]
Expand All @@ -46,6 +48,14 @@
PARSED_URI = urlparse(app.config["SQLALCHEMY_DATABASE_URI"])
DB_TYPE = PARSED_URI.scheme

@app.before_first_request
def register_event_handlers():
@event.listens_for(db.engine, "handle_error")
def handle_exception(context):
if not context.is_disconnect and re.match(
r"^(?:DPI-1001|DPI-4011)", str(context.original_exception)
):
context.is_disconnect = True

# $ curl -u fooUsr:barPass -X GET np04-srv-021:30016//runnumber/get
@api.resource("/runnumber/get")
Expand Down Expand Up @@ -93,7 +103,8 @@ def get(self):
run = RunNumber(rn=current_max_run)
db.session.add(run)
print(f"getNewtRunNumber: result {[current_max_run]}")
return flask.make_response(flask.jsonify([[[current_max_run]]]))
cnu = [name.upper() for name in column_names]
return flask.make_response(flask.jsonify(cnu, [[*result]]))
except Exception as err_obj:
print(f"Exception:{err_obj}")
return flask.make_response(flask.jsonify({"Exception": f"{err_obj}"}))
Expand Down
22 changes: 17 additions & 5 deletions runregistry-rest/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
from flask_caching import Cache
from flask_restful import Api, Resource
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy import desc, func
from sqlalchemy import func, event
import re

__all__ = ["app", "api", "db"]

Expand All @@ -37,6 +38,7 @@
DEPLOYMENT_ENV=os.environ.get("DEPLOYMENT_ENV", "DEV"),
RUN_START=int(os.getenv("RUN_START", "1000")),
SQLALCHEMY_ECHO=False,
SQLALCHEMY_ENGINE_OPTIONS={"pool_pre_ping": True, "pool_recycle": 3600},
)

cache = Cache(app)
Expand All @@ -53,6 +55,14 @@
PARSED_URI = urlparse(app.config["SQLALCHEMY_DATABASE_URI"])
DB_TYPE = PARSED_URI.scheme

@app.before_first_request
def register_event_handlers():
@event.listens_for(db.engine, "handle_error")
def handle_exception(context):
if not context.is_disconnect and re.match(
r"^(?:DPI-1001|DPI-4011)", str(context.original_exception)
):
context.is_disconnect = True

def cache_key():
args = flask.request.args
Expand Down Expand Up @@ -93,7 +103,8 @@ def get(self, runNum):
result = list(result)
column_names = RunRegistryMeta.__table__.columns.keys()
column_names.remove('filename') #Don't like this but only way to stay consistent with Oracle
return flask.make_response(flask.jsonify(column_names, [[*result]]))
cnu = [name.upper() for name in column_names]
return flask.make_response(flask.jsonify(cnu, [[*result]]))
except Exception as err_obj:
print(f"Exception:{err_obj}")
return flask.make_response(flask.jsonify({"Exception": f"{err_obj}"}))
Expand Down Expand Up @@ -127,7 +138,8 @@ def get(self, amount):
result = [list(row) for row in result]
column_names = RunRegistryMeta.__table__.columns.keys()
column_names.remove('filename') #Don't like this but only way to stay consistent with Oracle
return flask.make_response(flask.jsonify(column_names, [*result]))
cnu = [name.upper() for name in column_names]
return flask.make_response(flask.jsonify(cnu, [[*result]]))
except Exception as err_obj:
return flask.make_response(flask.jsonify({"Exception": f"{err_obj}"}))

Expand Down Expand Up @@ -167,7 +179,7 @@ def get(self, runNum):
print(f"Exception:{err_obj}")
return flask.make_response(flask.jsonify({"Exception": f"{err_obj}"}))

# $ curl -u fooUsr:barPass -F "run_number=1000" -F "det_id=foo" -F "run_type=bar" -F "software_version=dunedaq-vX.Y.Z" -F "file=@sspconf.tar.gz" -X POST np04-srv-017:30015/runregistry/insertRun/
# $ curl -u fooUsr:barPass -F "run_num=1000" -F "det_id=foo" -F "run_type=bar" -F "software_version=dunedaq-vX.Y.Z" -F "file=@sspconf.tar.gz" -X POST np04-srv-017:30015/runregistry/insertRun/
@api.resource("/runregistry/insertRun/")
class insertRun(Resource):
"""
Expand All @@ -181,7 +193,7 @@ def post(self):
local_file_name = None
try:
# Ensure form fields
run_number = flask.request.form.get("run_number")
run_number = flask.request.form.get("run_num")
det_id = flask.request.form.get("det_id")
run_type = flask.request.form.get("run_type")
software_version = flask.request.form.get("software_version")
Expand Down
3 changes: 2 additions & 1 deletion runregistry-rest/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@ class RunRegistryMeta(db.Model):
software_version = db.Column(
'software_version', db.String(40)
)
configs = db.relationship('RunRegistryConfigs', backref='meta')

class RunRegistryConfigs(db.Model):
run_number = db.Column(
'run_number', db.Integer, primary_key=True, autoincrement=True, nullable=False
'run_number', db.Integer, db.ForeignKey(RunRegistryMeta.run_number), primary_key=True, nullable=False
)
configuration = db.Column(
'configuration', db.LargeBinary, nullable=False
Expand Down