Repository: internetarchive/openlibrary

Issue:
Remove msgbroker / eventer
The `msgbroker` module is just a facade/shim for renaming `eventer` methods causing confusion / obscurity because uses have to be searched under both names.

```
send_message = eventer.trigger
subscribe = eventer.bind
unsubscribe = eventer.unbind
```

The event mechanism is only used in a couple of places and always in a one-to-one fashion, so is of limited/no utility for its intended purpose of decoupling. The implementation is also synchronous, so there's no performance benefit. It's just obscuring how the code is actually coupled.

We should convert the event driven code to just directly call the desired methods. Here's an inventory of calling method, event name, and callback method:

From module `openlibrary.core.lending` to module `openlibrary.plugins.upstream.borrow`:
* `create_loan() -> loan-created -> on_loan_update()`
* `sync_loan() -> loan-completed -> on_loan_delete()`

Also the recently deleted `openlibrary.plugins.openlibrary.libraries` which subscribes to the same two events:
* `loan-created ->on_loan_created_statsdb()`
* `loan-completed -> on_loan_completed_statsdb()`

For two other modules, all use is internal:
* `openlibrary.olbase.events` sends/receives `infobase.all` and `infobase.edit` ie all use is confined to the module. 
* `openlibrary.plugins.openlibrary.events` sends & receives `page.edit` but the callback is a noop



Discussion:

Resolving patch:
diff --git a/openlibrary/core/lending.py b/openlibrary/core/lending.py
--- a/openlibrary/core/lending.py
+++ b/openlibrary/core/lending.py
@@ -7,6 +7,8 @@
 import time
 import logging
 import uuid
+
+import eventer
 import requests
 
 from infogami.utils.view import public
@@ -17,7 +19,6 @@
 from openlibrary.utils import dateutil
 
 from . import ia
-from . import msgbroker
 from . import helpers as h
 
 logger = logging.getLogger(__name__)
@@ -530,7 +531,7 @@ def create_loan(identifier, resource_type, user_key, book_key=None):
 
     if ia_loan:
         loan = Loan.from_ia_loan(ia_loan)
-        msgbroker.send_message("loan-created", loan)
+        eventer.trigger("loan-created", loan)
         sync_loan(identifier)
         return loan
 
@@ -598,7 +599,7 @@ def sync_loan(identifier, loan=NOT_INITIALIZED):
     # fire loan-completed event
     if is_loan_completed and ebook.get('loan'):
         _d = dict(ebook['loan'], returned_at=time.time())
-        msgbroker.send_message("loan-completed", _d)
+        eventer.trigger("loan-completed", _d)
     logger.info("END sync_loan %s", identifier)
 
 
@@ -721,7 +722,7 @@ def save(self):
         web.ctx.site.store[self['_key']] = self
 
         # Inform listers that a loan is created/updated
-        msgbroker.send_message("loan-created", self)
+        eventer.trigger("loan-created", self)
 
     def is_expired(self):
         return self['expiry'] and self['expiry'] < datetime.datetime.utcnow().isoformat()
@@ -754,7 +755,7 @@ def delete(self):
 
         sync_loan(self['ocaid'])
         # Inform listers that a loan is completed
-        msgbroker.send_message("loan-completed", loan)
+        eventer.trigger("loan-completed", loan)
 
 def resolve_identifier(identifier):
     """Returns the OL book key for given IA identifier.
diff --git a/openlibrary/core/msgbroker.py b/openlibrary/core/msgbroker.py
deleted file mode 100644
--- a/openlibrary/core/msgbroker.py
+++ /dev/null
@@ -1,16 +0,0 @@
-"""Broker for dispaching messages in Open Library.
-
-Message Broker or the pub/sub messaging pattern allows publishers and
-subscribers of messages to communite without the knowledge of each other. This
-usually leads to a loosely-coupled design.
-
-For examaple, the borrow module can send messages about loan-created and
-loan-completed and the stats module can subscribe to these events and update
-the database used for displaying loan graphs.
-"""
-
-import eventer
-
-send_message = eventer.trigger
-subscribe = eventer.bind
-unsubscribe = eventer.unbind
diff --git a/openlibrary/plugins/openlibrary/borrow_home.py b/openlibrary/plugins/openlibrary/borrow_home.py
--- a/openlibrary/plugins/openlibrary/borrow_home.py
+++ b/openlibrary/plugins/openlibrary/borrow_home.py
@@ -1,9 +1,12 @@
 """Controller for /borrow page.
 """
 
-import json
 import web
+
 import datetime
+import json
+
+import eventer
 
 from infogami.plugins.api.code import jsonapi
 from infogami.utils import delegate
@@ -251,6 +254,5 @@ def _get_loan_key(loan):
 
 
 def setup():
-    from openlibrary.core import msgbroker
-    msgbroker.subscribe("loan-created", on_loan_created_statsdb)
-    msgbroker.subscribe("loan-completed", on_loan_completed_statsdb)
+    eventer.bind("loan-created", on_loan_created_statsdb)
+    eventer.bind("loan-completed", on_loan_completed_statsdb)


Test patch: