How to retry transactions on command line scripts

I have some command line scripts -- the kind you run with "bin/instance run script.py" -- and sometimes when I run them I have conflict errors while trying to commit the transaction. It occurs because there are other users managing content on the site simultaneously.

My question is: how to implement the same retry mechanism that Plone uses when handling the regular requests. I want my script to behave as if the code is being executed in a view, for example.

You could use dm.zodb.asynchronous. Its transactional module contains a transactional decorator which handles transactions similar to the way of Zope/Plone (i.e. with retrieal in case of ConflictError). It comes with a simple example.

2 Likes

It seems to work. Thank you!