'Get the oldest transaction which has not been sent for this
service.
Args:
service(ApplicationService): The app service to get the oldest txn.
Returns:
A Deferred which resolves to an AppServiceTransaction or
None.'

@defer.inlineCallbacks
def get_oldest_unsent_txn(self, service):

    def _get_oldest_unsent_txn(txn):
        txn.execute('SELECT    *    FROM    application_services_txns    WHERE    as_id=?    ORDER    BY    txn_id    ASC    LIMIT    1', (service.id,))
        rows = self.cursor_to_dict(txn)
        if (not rows):
            return None
        entry = rows[0]
        return entry
    entry = (yield self.runInteraction('get_oldest_unsent_appservice_txn', _get_oldest_unsent_txn))
    if (not entry):
        defer.returnValue(None)
    event_ids = json.loads(entry['event_ids'])
    events = (yield self._get_events(event_ids))
    defer.returnValue(AppServiceTransaction(service=service, id=entry['txn_id'], events=events))
