Add stub ApplicationServiceTransactionStore. Bootstrap Recoverers. Fill in stub Transaction functions.

This commit is contained in:
Kegan Dougal 2015-03-06 14:53:35 +00:00
parent 0fbfe1b08a
commit 141ec04d19
2 changed files with 71 additions and 7 deletions

View File

@ -71,11 +71,13 @@ class AppServiceScheduler(object):
clock, store, as_api, self.event_grouper, create_recoverer clock, store, as_api, self.event_grouper, create_recoverer
) )
@defer.inlineCallbacks
def start(self): def start(self):
# check for any DOWN ASes and start recoverers for them. # check for any DOWN ASes and start recoverers for them.
_Recoverer.start( recoverers = yield _Recoverer.start(
self.clock, self.store, self.as_api, self.txn_ctrl.on_recovered self.clock, self.store, self.as_api, self.txn_ctrl.on_recovered
) )
self.txn_ctrl.add_recoverers(recoverers)
self.txn_ctrl.start_polling() self.txn_ctrl.start_polling()
def submit_event_for_as(self, service, event): def submit_event_for_as(self, service, event):
@ -91,12 +93,34 @@ class AppServiceTransaction(object):
self.events = events self.events = events
def send(self, as_api): def send(self, as_api):
# TODO sends this transaction using this as_api """Sends this transaction using the provided AS API interface.
pass
Args:
as_api(ApplicationServiceApi): The API to use to send.
Returns:
A Deferred which resolves to True if the transaction was sent.
"""
return as_api.push_bulk(
service=self.service,
events=self.events,
txn_id=self.id
)
def complete(self, store): def complete(self, store):
# TODO increment txn id on AS and nuke txn contents from db """Completes this transaction as successful.
pass
Marks this transaction ID on the application service and removes the
transaction contents from the database.
Args:
store: The database store to operate on.
Returns:
A Deferred which resolves to True if the transaction was completed.
"""
return store.complete_appservice_txn(
service=self.service,
txn_id=self.id
)
class _EventGrouper(object): class _EventGrouper(object):
@ -125,6 +149,8 @@ class _TransactionController(object):
self.as_api = as_api self.as_api = as_api
self.event_grouper = event_grouper self.event_grouper = event_grouper
self.recoverer_fn = recoverer_fn self.recoverer_fn = recoverer_fn
# keep track of how many recoverers there are
self.recoverers = []
def start_polling(self): def start_polling(self):
groups = self.event_grouper.drain_groups() groups = self.event_grouper.drain_groups()
@ -144,6 +170,10 @@ class _TransactionController(object):
# TODO mark AS as UP # TODO mark AS as UP
pass pass
def add_recoverers(self, recoverers):
for r in recoverers:
self.recoverers.append(r)
def _start_recoverer(self, service): def _start_recoverer(self, service):
recoverer = self.recoverer_fn(service, self.on_recovered) recoverer = self.recoverer_fn(service, self.on_recovered)
recoverer.recover() recoverer.recover()
@ -161,9 +191,15 @@ class _TransactionController(object):
class _Recoverer(object): class _Recoverer(object):
@staticmethod @staticmethod
@defer.inlineCallbacks
def start(clock, store, as_api, callback): def start(clock, store, as_api, callback):
# TODO check for DOWN ASes and init recoverers services = yield store.get_failing_appservices()
pass recoverers = [
_Recoverer(clock, store, as_api, s, callback) for s in services
]
for r in recoverers:
r.recover()
defer.returnValue(recoverers)
def __init__(self, clock, store, as_api, service, callback): def __init__(self, clock, store, as_api, service, callback):
self.clock = clock self.clock = clock

View File

@ -336,3 +336,31 @@ class ApplicationServiceStore(SQLBaseStore):
hs_token=service["hs_token"], hs_token=service["hs_token"],
sender=service["sender"] sender=service["sender"]
)) ))
class ApplicationServiceTransactionStore(SQLBaseStore):
def __init__(self, hs):
super(ApplicationServiceTransactionStore, self).__init__(hs)
def get_failing_appservices(self):
"""Get a list of application services which are down.
Returns:
A Deferred which resolves to a list of ApplicationServices, which
may be empty.
"""
pass
def complete_appservice_txn(self, txn_id, service):
"""Completes an application service transaction.
Args:
txn_id(str): The transaction ID being completed.
service(ApplicationService): The application service which was sent
this transaction.
Returns:
A Deferred which resolves to True if this transaction was completed
successfully.
"""
pass