-
Notifications
You must be signed in to change notification settings - Fork 30
feat: add user record cleanup script #677
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import time | ||
|
||
import click | ||
|
||
from autopush.db import ( | ||
get_router_table, | ||
Router, | ||
) | ||
from autopush.metrics import SinkMetrics | ||
|
||
|
||
@click.command() | ||
@click.option('--router_table_name', help="Name of the router table.") | ||
@click.option('--months-ago', default=2, help="Months ago to remove.") | ||
@click.option('--batch_size', default=25, | ||
help="Deletes to run before pausing.") | ||
@click.option('--pause_time', default=1, | ||
help="Seconds to pause between batches.") | ||
def drop_users(router_table_name, months_ago, batch_size, pause_time): | ||
router_table = get_router_table(router_table_name) | ||
router = Router(router_table, SinkMetrics()) | ||
|
||
click.echo("Deleting users with a last_connect %s months ago." | ||
% months_ago) | ||
|
||
count = 0 | ||
for deletes in router.drop_old_users(months_ago): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. shouldn't batch_size be an argument to drop_old_users? it would work more as advertised and simplify this loop too There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I mainly wanted a way to rope in how frequently the pause happens, but wanted to still maximize the actual delete batch commands to the max supported by AWS. So that the approximate speed is controlled, a little slop is ok. It's entirely likely that there will be no pause at all specified since latency is probably going to reduce the max speed anyways. |
||
click.echo("") | ||
count += deletes | ||
if count >= batch_size: | ||
click.echo("Deleted %s user records, pausing for %s seconds." | ||
% pause_time) | ||
time.sleep(pause_time) | ||
count = 0 | ||
click.echo("Finished old user purge.") | ||
|
||
|
||
if __name__ == '__main__': # pragma: nocover | ||
drop_users() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
import unittest | ||
import uuid | ||
|
||
from autopush.exceptions import AutopushException | ||
from autopush.websocket import ms_time | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FWIW: moved this to |
||
from boto.dynamodb2.exceptions import ( | ||
ConditionalCheckFailedException, | ||
ProvisionedThroughputExceededException, | ||
|
@@ -23,7 +23,9 @@ | |
Storage, | ||
Message, | ||
Router, | ||
generate_last_connect, | ||
) | ||
from autopush.exceptions import AutopushException | ||
from autopush.metrics import SinkMetrics | ||
from autopush.utils import WebPushNotification | ||
|
||
|
@@ -361,6 +363,25 @@ def setUp(self): | |
def tearDown(self): | ||
self.real_table.connection = self.real_connection | ||
|
||
def _create_minimal_record(self): | ||
data = { | ||
"uaid": str(uuid.uuid4()), | ||
"router_type": "webupsh", | ||
"last_connect": generate_last_connect(), | ||
"connected_at": ms_time(), | ||
} | ||
return data | ||
|
||
def test_drop_old_users(self): | ||
# First create a bunch of users | ||
r = get_router_table() | ||
router = Router(r, SinkMetrics()) | ||
for _ in range(0, 53): | ||
router.register_user(self._create_minimal_record()) | ||
|
||
results = router.drop_old_users(months_ago=0) | ||
eq_(list(results), [25, 25, 3]) | ||
|
||
def test_custom_tablename(self): | ||
db = DynamoDBConnection() | ||
db_name = "router_%s" % uuid.uuid4() | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm guessing this partially fixes #653?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nope, its how they index key is constructed.