From e281204913384c9de1749501e3a3b4f56b42402e Mon Sep 17 00:00:00 2001 From: adammck Date: Tue, 21 Apr 2009 19:34:07 -0400 Subject: [PATCH] added message serialization for failed spomc messages when an outgoing message fails, it is written to /tmp/rapidsms-pending. on startup, this file is read, and all of the messages contained are sent out. this is crappy as hell, and only meant for the demo tomorrow, in case spomskyd blows up while people are texting in. --- lib/rapidsms/backends/backend.py | 4 ++-- lib/rapidsms/backends/spomc.py | 18 +++++++++++++++--- lib/rapidsms/router.py | 32 +++++++++++++++++++++++++++++++- lib/spomsky/__init__.py | 3 +++ 4 files changed, 51 insertions(+), 6 deletions(-) diff --git a/lib/rapidsms/backends/backend.py b/lib/rapidsms/backends/backend.py index d319b05a1..4c6087d2b 100644 --- a/lib/rapidsms/backends/backend.py +++ b/lib/rapidsms/backends/backend.py @@ -31,8 +31,8 @@ def run (self): def stop(self): self._running = False - def message(self, connection, text, date=None): - c = Connection(self, connection) + def message(self, identity, text, date=None): + c = Connection(self, identity) return Message(c, text, date) def route(self, msg): diff --git a/lib/rapidsms/backends/spomc.py b/lib/rapidsms/backends/spomc.py index 9e571280b..26e0d57ee 100644 --- a/lib/rapidsms/backends/spomc.py +++ b/lib/rapidsms/backends/spomc.py @@ -5,12 +5,12 @@ from rapidsms.connection import Connection import backend import spomsky -import re +import re, time class Backend(backend.Backend): def configure(self, host="localhost", port=8100): self.client = spomsky.Client(host, port) - + def __callback(self, source, message_text): # drop the "sms://" protocol from the source phone_number = re.compile("[a-z]+://").sub("", source) @@ -23,8 +23,20 @@ def __callback(self, source, message_text): def send(self, message): destination = "%s" % (message.connection.identity) - self.client.send(destination, message.text) + sent = self.client.send(destination, message.text) + # if the message wasn't sent, log it to the super + # low-tech pending file, to be sent at some point + # in the undefined future... + if not sent: + + # TODO: move this to backend.py, and make it sane + str = "%s:%s:%s" % (self.name, destination, message.text) + self.warning("DIDN'T SEND: %s" % (str)) + f = file("/tmp/rapidsms-pending", "a") + f.write(str + "\n") + f.close() + def start(self): self.client.subscribe(self.__callback) backend.Backend.start(self) diff --git a/lib/rapidsms/router.py b/lib/rapidsms/router.py index e90a6d2cb..14f8d054a 100644 --- a/lib/rapidsms/router.py +++ b/lib/rapidsms/router.py @@ -1,7 +1,7 @@ #!/usr/bin/env python # vim: ai ts=4 sts=4 et sw=4 -import time, datetime +import time, datetime, os import threading import traceback @@ -175,6 +175,36 @@ def start (self): self.start_all_backends() self.start_all_apps() + # check for any pending messages + try: + fn = "/tmp/rapidsms-pending" + f = file(fn) + + # trash the file, to prevent + # these messages being re-sent + msgs = f.readlines() + os.unlink(fn) + f.close() + + # iterate the pending messages, + for pending_msg in msgs: + be_name, identity, txt =\ + pending_msg.strip().split(":") + + # find the backend named by the message, + # reconstruct the object, and send it + for backend in self.backends: + if backend.name == be_name: + msg = backend.message(identity, txt) + self.info("Sending pending message: %r" % msg) + msg.send() + + # something went bang. not sure what, and don't + # particularly care. they'll be re-tried the + # next time rapidsms starts up + except: + pass + # wait until we're asked to stop while self.running: try: diff --git a/lib/spomsky/__init__.py b/lib/spomsky/__init__.py index e0a836c3f..e9946cb79 100644 --- a/lib/spomsky/__init__.py +++ b/lib/spomsky/__init__.py @@ -92,6 +92,9 @@ def send(self, destination, body): # so just return false instead except: return False + + # nothing went bang + return True def subscribe(self, callback):