Skip to content
This repository has been archived by the owner on Jun 12, 2018. It is now read-only.

Commit

Permalink
Add command for reconciling message caches (and maybe later for other…
Browse files Browse the repository at this point in the history
… things).
  • Loading branch information
hodgestar committed Feb 18, 2014
1 parent ed86f0c commit 998078e
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 0 deletions.
68 changes: 68 additions & 0 deletions go/base/management/commands/go_manage_message_cache.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
from optparse import make_option

from django.core.management.base import CommandError

from go.base.command_utils import (
BaseGoCommand, make_command_option,
user_details_as_string)


class Command(BaseGoCommand):
help = "Manage the message cache."

option_list = BaseGoCommand.option_list + (
make_command_option('reconcile', help='Reconcile the message cache.'),
make_option(
'--email-address', dest='email_address',
help="Act on the given user's batches."),
make_option('--conversation-key',
dest='conversation_key',
help='Act on the given conversation.'),
make_option('--active-conversations',
dest='active_conversations',
action='store_true', default=False,
help='Act on all active conversations.'),
make_option('--dry-run',
dest='dry_run',
action='store_true', default=False,
help='Just pretend to act.'),
)

def _get_user_apis(self):
if "email_address" in self.options:
return [self.mk_user_api(self.options["email_address"])]
return self.mk_all_user_apis()

def _get_batches(self, user_api):
batches = set()
if 'conversation_key' in self.options:
conv = user_api.get_conversation(self.options['conversation_key'])
batches.add(conv.batch.key)
if self.options.get('active_conversations'):
batches.update(
conv.batch.key for conv in user_api.active_conversations())
return list(batches)

def _apply_to_batches(self, func, dry_run=None):
if dry_run is None:
dry_run = self.options.get('dry_run')

for user, user_api in self._get_user_apis():
batches = self._get_batches(user_api)
if not batches:
continue
self.stdout.write(
"Processing account %s ...\n" % user_details_as_string(user))
for batch_id in sorted(batches):
self.stdout.write(
" Preforming %s on batch %s ...\n"
% (func.__name__, batch_id))
if not dry_run:
func(user_api, batch_id)
self.stdout.write("done.\n")

def handle_command_reconcile(self, *args, **options):
def reconcile(user_api, batch_id):
user_api.api.mdb.reconcile_cache(batch_id)

self._apply_to_batches(reconcile)
40 changes: 40 additions & 0 deletions go/base/tests/test_go_manage_message_cache.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from go.base.management.commands import go_manage_message_cache
from go.base.tests.helpers import GoCommandTestCase


class TestGoManageMessageCache(GoCommandTestCase):

def setUp(self):
self.setup_command(go_manage_message_cache.Command)

def test_reconcile_conversation(self):
conv = self.user_helper.create_conversation(u"http_api")
expected_output = "\n".join([
u'Processing account Test User'
u' <user@domain.com> [test-0-user] ...',
u' Preforming reconcile on'
u' batch %s ...' % conv.batch.key,
u'done.',
u''
])
self.assert_command_output(
expected_output, 'reconcile',
email_address=self.user_email, conversation_key=conv.key)

def test_reconcile_active_conversations(self):
conv1 = self.user_helper.create_conversation(u"http_api")
conv2 = self.user_helper.create_conversation(u"http_api")
batch_ids = sorted([conv1.batch.key, conv2.batch.key])
expected_output = "\n".join([
u'Processing account Test User'
u' <user@domain.com> [test-0-user] ...',
u' Preforming reconcile on'
u' batch %s ...' % batch_ids[0],
u' Preforming reconcile on'
u' batch %s ...' % batch_ids[1],
u'done.',
u''
])
self.assert_command_output(
expected_output, 'reconcile',
email_address=self.user_email, active_conversations=True)

0 comments on commit 998078e

Please sign in to comment.