This PR implements the concept of middleware. Middleware is a function that intercepts all events. You can use it to implement session management for a database, code an extension to streamsync which globally modifies the operation of certain events, ...
middleware facilitates cross-functional operations such as database transaction management. Opening a session at the start of a query is a pattern that improves the use of a database. The session links the transaction to the request. If processing fails, the transaction is rolled back. If it succeeds, the transaction is successful.
import writer as wf
engine = sa.create_engine("postgresql://admin:admin@localhost/Adventureworks", echo=True)
Session = sessionmaker(engine)
@wf.middleware
def manage_sqlalchemy_session():
with Session() as session:
session.begin()
try:
yield
session.commit()
except:
session.rollback()
raise
middleware facilitates cross-functional operations such as exception tracking.
import writer as wf
@wf.middleware
def manage_sentry_capture(state, payload):
with sentry_sdk.configure_scope() as scope:
scope.set_tag("site", state['site'])
try:
yield
except Exception as exception:
sentry_sdk.capture(exception)
raise
finally:
scope.clear()
This PR implements the concept of middleware. Middleware is a function that intercepts all events. You can use it to implement session management for a database, code an extension to streamsync which globally modifies the operation of certain events, ...
Usecase
middleware facilitates cross-functional operations such as database transaction management. Opening a session at the start of a query is a pattern that improves the use of a database. The session links the transaction to the request. If processing fails, the transaction is rolled back. If it succeeds, the transaction is successful.
middleware facilitates cross-functional operations such as exception tracking.