forked from pinax/pinax-notifications
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added asynchronous notification sending.
git-svn-id: http://django-notification.googlecode.com/svn/trunk@97 590c3fc9-4838-0410-bb95-17a0c9b37ca9
- Loading branch information
Showing
4 changed files
with
825 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
|
||
import time | ||
import logging | ||
|
||
try: | ||
import cPickle as pickle | ||
except ImportError: | ||
import pickle | ||
|
||
from django.conf import settings | ||
from django.contrib.auth.models import User | ||
|
||
from lockfile import FileLock, AlreadyLocked, LockTimeout | ||
|
||
from notification.models import NoticeQueue | ||
from notification import models as notification | ||
|
||
# lock timeout value. how long to wait for the lock to become available. | ||
# default behavior is to never wait for the lock to be available. | ||
LOCK_WAIT_TIMEOUT = getattr(settings, "NOTIFICATION_LOCK_WAIT_TIMEOUT", -1) | ||
|
||
def send_all(): | ||
lock = FileLock("send_notices") | ||
|
||
logging.debug("acquiring lock...") | ||
try: | ||
lock.acquire(LOCK_WAIT_TIMEOUT) | ||
except AlreadyLocked: | ||
logging.debug("lock already in place. quitting.") | ||
return | ||
except LockTimeout: | ||
logging.debug("waiting for the lock timed out. quitting.") | ||
return | ||
logging.debug("acquired.") | ||
|
||
batches, sent = 0, 0 | ||
start_time = time.time() | ||
|
||
try: | ||
for queued_batch in NoticeQueue.objects.all(): | ||
notices = pickle.loads(str(queued_batch.pickled_data)) | ||
for user, label, extra_context, on_site in notices: | ||
user = User.objects.get(pk=user) | ||
logging.info("emitting notice to %s" % user) | ||
# call this once per user to be atomic and allow for logging to | ||
# accurately show how long each takes. | ||
notification.send([user], label, extra_context, on_site) | ||
sent += 1 | ||
queued_batch.delete() | ||
batches += 1 | ||
finally: | ||
logging.debug("releasing lock...") | ||
lock.release() | ||
logging.debug("released.") | ||
|
||
logging.info("") | ||
logging.info("%s batches, %s sent" % (batches, sent,)) | ||
logging.info("done in %.2f seconds" % (time.time() - start_time)) |
Oops, something went wrong.