Sorry, I fired a little too soon… You might want to look at the current state of the art in our sqlalchemy cookie cutter:
https://github.com/Pylons/pyramid-cookiecutter-alchemy <https://github.com/Pylons/pyramid-cookiecutter-alchemy> Request finalized callbacks are explicitly outside of tweens, and outside of any encapsulation. You’ll want to look at pyramid_tm or build a tween yourself that handles things in a similar fashion. > On Apr 28, 2017, at 00:20, Theron Luhn <[email protected]> wrote: > > I have a pretty standard setup of Pyramid and SQLAlchemy, based off > http://docs.pylonsproject.org/projects/pyramid_cookbook/en/latest/database/sqlalchemy.html > > However, this has a problem: The `cleanup` callback executes *after* the > tween has returned. I use tweens for both error logging (Rollbar) and > request logging (output via python-fluent-loggerd). This means that if the > commit and associated flush fail, neither my request logger nor error logging > catch it. > > For example, if my tween looked like this: > > > class Tween: > def __init__(self, handler, register): > self.handler = handler > > def __call__(self, request): > start = time() > response = self.handler(request) > duration = time() - start > log(duration, response.status_code) # This shows a success > return response # session.commit() fails sometime after this > > > The log call would report an HTTP 200, even the end result may be a HTTP 500 > due to a failure in session.commt() > > The obvious solution is to move logging to the WSGI middleware layer, but > then I lose all the Pyramid-specific details, which I am loath to do. I > could also manually add a try/except to handle errors in the cleanup and > report to Rollbar, but that still means my request logs show a success. > > Is there a way I can force SQLAlchemy to commit/rollback (or at least flush) > before the passing the baton back to the tweens? > > -- > You received this message because you are subscribed to the Google Groups > "pylons-discuss" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected] > <mailto:[email protected]>. > To post to this group, send email to [email protected] > <mailto:[email protected]>. > To view this discussion on the web visit > https://groups.google.com/d/msgid/pylons-discuss/a98bf35f-d120-40b4-8168-fe4fc2f09f84%40googlegroups.com > > <https://groups.google.com/d/msgid/pylons-discuss/a98bf35f-d120-40b4-8168-fe4fc2f09f84%40googlegroups.com?utm_medium=email&utm_source=footer>. > For more options, visit https://groups.google.com/d/optout > <https://groups.google.com/d/optout>. -- You received this message because you are subscribed to the Google Groups "pylons-discuss" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/pylons-discuss/B511E99D-97ED-4088-AC83-230157C0E798%400x58.com. For more options, visit https://groups.google.com/d/optout.
