1414from openlibrary .plugins .upstream .edits import process_merge_request
1515from openlibrary .plugins .worksearch .code import top_books_from_author
1616from openlibrary .utils import dicthash , uniq
17+ from openlibrary .utils .retry import MaxRetriesExceeded , RetryStrategy
1718
1819
1920class BasicRedirectEngine :
@@ -366,9 +367,15 @@ def POST(self):
366367 comment = data .get ('comment' , None )
367368 olids = data .get ('olids' , '' )
368369
369- engine = AuthorMergeEngine (AuthorRedirectEngine ())
370- try :
371- result = engine .merge (master , duplicates )
370+ def merge_records () -> Any :
371+ try :
372+ engine = AuthorMergeEngine (AuthorRedirectEngine ())
373+ return engine .merge (master , duplicates )
374+ except ClientException as e :
375+ raise web .badrequest (json .loads (e .json ))
376+
377+ def update_request () -> None :
378+ data = {}
372379 if mrid :
373380 # Update the request
374381 rtype = 'update-request'
@@ -380,9 +387,21 @@ def POST(self):
380387 if comment :
381388 data ['comment' ] = comment
382389 process_merge_request (rtype , data )
383- except ClientException as e :
384- raise web .badrequest (json .loads (e .json ))
385- return delegate .RawText (json .dumps (result ), content_type = "application/json" )
390+
391+ # actually perform merge and save affected records to db
392+ merge_result = merge_records ()
393+ # attempt to update the merge request status with retries
394+ try :
395+ RetryStrategy (
396+ [ClientException ],
397+ max_retries = 5 ,
398+ delay = 2 ,
399+ )(update_request )
400+ except MaxRetriesExceeded as e :
401+ raise web .badrequest (str (e .last_exception ))
402+ return delegate .RawText (
403+ json .dumps (merge_result ), content_type = "application/json"
404+ )
386405
387406
388407def setup ():
0 commit comments