Skip to content

Commit

Permalink
added message serialization for failed spomc messages
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
adammck committed Apr 21, 2009
1 parent b22af8f commit e281204
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 6 deletions.
4 changes: 2 additions & 2 deletions lib/rapidsms/backends/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
18 changes: 15 additions & 3 deletions lib/rapidsms/backends/spomc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
32 changes: 31 additions & 1 deletion lib/rapidsms/router.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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:
Expand Down
3 changes: 3 additions & 0 deletions lib/spomsky/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit e281204

Please sign in to comment.