changeset 3cb9a88ff443 in modules/account_payment_stripe:4.8
details: 
https://hg.tryton.org/modules/account_payment_stripe?cmd=changeset;node=3cb9a88ff443
description:
        Lock records when processing

        issue8012
        review60511002
        (grafted from d404a49a74d2d906ce2b2d3bf14978b5d5c51427)
diffstat:

 payment.py |  21 +++++++++++++++++++++
 1 files changed, 21 insertions(+), 0 deletions(-)

diffs (45 lines):

diff -r 20a3fad82b75 -r 3cb9a88ff443 payment.py
--- a/payment.py        Sat Jan 26 01:12:50 2019 +0100
+++ b/payment.py        Wed Feb 06 22:51:18 2019 +0100
@@ -375,6 +375,7 @@
                     ('stripe_charge_id', '=', None),
                     ])
         for payment in payments:
+            cls.__lock([payment])
             try:
                 charge = stripe.Charge.create(**payment._charge_parameters())
             except (stripe.error.RateLimitError,
@@ -437,6 +438,7 @@
                     or payment.stripe_captured
                     or payment.state != 'processing'):
                 continue
+            cls.__lock([payment])
             try:
                 charge = stripe.Charge.retrieve(
                     payment.stripe_charge_id,
@@ -474,6 +476,25 @@
             'idempotency_key': idempotency_key,
             }
 
+    @classmethod
+    def __lock(cls, records):
+        from trytond.tools import grouped_slice, reduce_ids
+        from sql import Literal, For
+        transaction = Transaction()
+        database = transaction.database
+        connection = transaction.connection
+        table = cls.__table__()
+
+        if database.has_select_for():
+            for sub_records in grouped_slice(records):
+                where = reduce_ids(table.id, sub_records)
+                query = table.select(
+                    Literal(1), where=where, for_=For('UPDATE', nowait=True))
+                with connection.cursor() as cursor:
+                    cursor.execute(*query)
+        else:
+            database.lock(connection, cls._table)
+
 
 class Account(ModelSQL, ModelView):
     "Stripe Account"

Reply via email to