Repository: internetarchive/openlibrary

Issue:
Remove calls to sync update_waitinglist 
This is a cleanup for #3402, it is tentatively determined that loan_sync from openlibrary.php within petabox is not required on borrow and the right logic should get called anyway when the join/leave waitlist logic is invoked within petabox lending internals.


Discussion:

Resolving patch:
diff --git a/openlibrary/core/waitinglist.py b/openlibrary/core/waitinglist.py
--- a/openlibrary/core/waitinglist.py
+++ b/openlibrary/core/waitinglist.py
@@ -210,7 +210,6 @@ def is_user_waiting_for(user_key, book_key):
     if book and book.ocaid:
         return WaitingLoan.find(user_key, book.ocaid) is not None
 
-
 def join_waitinglist(user_key, book_key, itemname=None):
     """Adds a user to the waiting list of given book.
 
@@ -221,7 +220,6 @@ def join_waitinglist(user_key, book_key, itemname=None):
         WaitingLoan.new(user_key=user_key,
                         identifier=book.ocaid,
                         itemname=itemname)
-        update_waitinglist(book.ocaid)
 
 def leave_waitinglist(user_key, book_key, itemname=None):
     """Removes the given user from the waiting list of the given book.
@@ -232,76 +230,6 @@ def leave_waitinglist(user_key, book_key, itemname=None):
                              itemname=itemname)
         if w:
             w.delete()
-            update_waitinglist(book.ocaid)
-
-def update_waitinglist(identifier):
-    """Updates the status of the waiting list.
-
-    It does the following things:
-
-    * marks the first one in the waiting-list as active if the book is available to borrow
-    * updates the waiting list size in the ebook document (this is used by solr to index wl size)
-    * If the person who borrowed the book is in the waiting list, removed it (should never happen)
-
-    This function should be called on the following events:
-    * When a book is checked out or returned
-    * When a person joins or leaves the waiting list
-    """
-    return None
-    # For books with many active loans, calls to loan.sync can be very slow / 504.
-    # It looks like, these two functions are handled on the IA side, and don't actually
-    # need to be called from Open Library. Disabling as a patch deploy for now; can
-    # likely remove remove in the near future.
-    _wl_api.request("loan.sync", identifier=identifier)
-    return on_waitinglist_update(identifier)
-
-    book = _get_book(identifier)
-    book_key = book.key
-
-    logger.info("BEGIN updating %r", book_key)
-
-    checkedout = lending.is_loaned_out(identifier)
-
-    if checkedout:
-        loans = book.get_loans()
-        # Delete from waiting list if a user has already borrowed this book
-        for loan in loans:
-            w = WaitingLoan.find(loan['user'], book.ocaid)
-            if w:
-                w.delete()
-
-    wl = get_waitinglist_for_book(book_key)
-
-    # Delete the first entry if it is expired
-    if wl and wl[0].is_expired():
-        wl[0].delete()
-        wl = wl[1:]
-
-    # Mark the first entry in the waiting-list as available if the book
-    # is not checked out.
-    if not checkedout and wl and wl[0]['status'] != 'available':
-        expiry = datetime.datetime.utcnow() + datetime.timedelta(days=1)
-        wl[0].update(status='available', expiry=expiry.isoformat())
-
-    ebook_key = "ebooks" + book_key
-    ebook = web.ctx.site.store.get(ebook_key) or {}
-
-    # for the end user, a book is not available if it is either
-    # checked out or someone is waiting.
-    not_available = bool(checkedout or wl)
-
-    update_ebook('ebooks' + book_key,
-        book_key=book_key,
-        borrowed=str(not_available).lower(), # store as string "true" or "false"
-        wl_size=len(wl))
-
-    # Start storing ebooks/$identifier so that we can handle multiple editions
-    # with same ocaid more effectively.
-    update_ebook('ebooks/' + identifier,
-        borrowed=str(not_available).lower(), # store as string "true" or "false"
-        wl_size=len(wl))
-
-    logger.info("END updating %r", book_key)
 
 def on_waitinglist_update(identifier):
     """Triggered when a waiting list is updated.
@@ -361,10 +289,6 @@ def prune_expired_waitingloans():
     24 hours after his waiting loan becomes "available".
     """
     return
-    expired = WaitingLoan.prune_expired()
-    # Update the checkedout status and position in the WL for each entry
-    for r in expired:
-        update_waitinglist(r['identifier'])
 
 def update_all_waitinglists():
     rows = WaitingLoan.query(limit=10000)
@@ -372,7 +296,6 @@ def update_all_waitinglists():
     for identifier in identifiers:
         try:
             _wl_api.request("loan.sync", identifier=identifier)
-            update_waitinglist(identifier)
         except Exception:
             logger.error("failed to update waitinglist for %s", identifier, exc_info=True)
 
diff --git a/openlibrary/plugins/upstream/borrow.py b/openlibrary/plugins/upstream/borrow.py
--- a/openlibrary/plugins/upstream/borrow.py
+++ b/openlibrary/plugins/upstream/borrow.py
@@ -19,11 +19,9 @@
 from infogami.infobase.utils import parse_datetime
 
 from openlibrary.core import stats
-from openlibrary.core import msgbroker
 from openlibrary.core import lending
 from openlibrary.core import vendors
 from openlibrary.core import waitinglist
-from openlibrary.core import ab
 from openlibrary.accounts.model import OpenLibraryAccount
 from openlibrary import accounts
 from openlibrary.utils import dateutil
@@ -147,15 +145,15 @@ def POST(self, key):
             raise web.seeother(redirect_url)
 
         if action == 'return':
-            loan_resp = lending.s3_loan_api(edition.ocaid, s3_keys, action='return_loan')
+            lending.s3_loan_api(edition.ocaid, s3_keys, action='return_loan')
             stats.increment('ol.loans.return')
             raise web.seeother(edition.url())
         elif action == 'join-waitinglist':
-            loan_resp = lending.s3_loan_api(edition.ocaid, s3_keys, action='join_waitlist')
+            lending.s3_loan_api(edition.ocaid, s3_keys, action='join_waitlist')
             stats.increment('ol.loans.joinWaitlist')
             raise web.redirect(edition.url())
         elif action == 'leave-waitinglist':
-            loan_resp = lending.s3_loan_api(edition.ocaid, s3_keys, action='leave_waitlist')
+            lending.s3_loan_api(edition.ocaid, s3_keys, action='leave_waitlist')
             stats.increment('ol.loans.leaveWaitlist')
             raise web.redirect(edition.url())
 
@@ -173,7 +171,7 @@ def POST(self, key):
             if not (s3_keys or borrow_access):
                 raise web.seeother(error_redirect)
 
-            loan_resp = lending.s3_loan_api(edition.ocaid, s3_keys, action='%s_book' % borrow_access)
+            lending.s3_loan_api(edition.ocaid, s3_keys, action='%s_book' % borrow_access)
             stats.increment('ol.loans.bookreader')
             stats.increment('ol.loans.%s' % borrow_access)
             action = 'read'
@@ -282,8 +280,6 @@ def POST(self, key):
             loan = lending.get_loan(edition.ocaid)
             if loan and loan['_key'] == i.loan_key:
                 loan.delete()
-        elif i.action == 'update_loan_info':
-            waitinglist.update_waitinglist(edition.ocaid)
         raise web.seeother(web.ctx.path + '/borrow_admin')
 
 class borrow_admin_no_update(delegate.page):
@@ -381,7 +377,6 @@ class ia_auth(delegate.page):
     def GET(self, item_id):
         i = web.input(_method='GET', callback=None, loan=None, token=None)
 
-        resource_id = 'bookreader:%s' % item_id
         content_type = "application/json"
 
         # check that identifier is valid
@@ -411,7 +406,7 @@ def GET(self):
     def POST(self):
         data = web.data()
         try:
-            notify_xml = etree.fromstring(data)
+            etree.fromstring(data)
             output = json.dumps({'success': True})
         except Exception as e:
             output = json.dumps({'success': False, 'error': str(e)})
@@ -650,7 +645,6 @@ def _update_loan_status(loan_key, loan, bss_status = None):
         if loan['expiry'] and loan['expiry'] < datetime.datetime.utcnow().isoformat():
             logger.info("%s: loan expired. deleting...", loan_key)
             web.ctx.site.store.delete(loan_key)
-            on_loan_delete(loan)
         return
 
     # Load status from book status server
@@ -679,7 +673,6 @@ def update_loan_from_bss_status(loan_key, loan, status):
         # Was returned, expired, or timed out
         web.ctx.site.store.delete(loan_key)
         logger.info("%s: loan returned or expired or timedout, deleting...", loan_key)
-        on_loan_delete(loan)
         return
 
     # Book has non-returned status
@@ -688,7 +681,6 @@ def update_loan_from_bss_status(loan_key, loan, status):
         loan['expiry'] = status['until']
         web.ctx.site.store[loan_key] = loan
         logger.info("%s: updated expiry to %s", loan_key, loan['expiry'])
-        on_loan_update(loan)
 
 def update_all_loan_status():
     """Update the status of all loans known to Open Library by cross-checking with the book status server.
@@ -720,6 +712,7 @@ def resource_uses_bss(resource_id):
                 return True
     return False
 
+
 def user_can_borrow_edition(user, edition):
     """Returns the type of borrow for which patron is eligible, favoring
     "browse" over "borrow" where available, otherwise return False if
@@ -736,7 +729,7 @@ def user_can_borrow_edition(user, edition):
         if lending_st.get('available_to_browse'):
             return 'browse'
         if lending_st.get('available_to_borrow') or (
-            book_is_waitlistable and is_users_turn_to_borrow(user, edition)):
+                book_is_waitlistable and is_users_turn_to_borrow(user, edition)):
             return 'borrow'
     return False
 
@@ -923,15 +916,5 @@ def make_bookreader_auth_link(loan_key, item_id, book_path, ol_host, ia_userid=N
     }
     return auth_link + urllib.parse.urlencode(params)
 
-def on_loan_update(loan):
-    # update the waiting list and ebook document.
-    waitinglist.update_waitinglist(loan['ocaid'])
-
-def on_loan_delete(loan):
-    # update the waiting list and ebook document.
-    waitinglist.update_waitinglist(loan['ocaid'])
-
-msgbroker.subscribe("loan-created", on_loan_update)
-msgbroker.subscribe("loan-completed", on_loan_delete)
 lending.setup(config)
 vendors.setup(config)


Test patch: