Merge lp:~milo/linaro-patchmetrics/cached-user-check into lp:linaro-patchmetrics

Proposed by Milo Casagrande
Status: Merged
Merged at revision: 402
Proposed branch: lp:~milo/linaro-patchmetrics/cached-user-check
Merge into: lp:linaro-patchmetrics
Diff against target: 63 lines (+25/-3)
2 files modified
apps/patchwork/utils.py (+23/-3)
apps/settings.py (+2/-0)
To merge this branch: bzr merge lp:~milo/linaro-patchmetrics/cached-user-check
Reviewer Review Type Date Requested Status
Linaro Automation & Validation Pending
Review via email: mp+197944@code.launchpad.net

Description of the change

MP adds expiry check for cached users in the DB. Default value is set to 30 days.

To post a comment you must log in.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'apps/patchwork/utils.py'
2--- apps/patchwork/utils.py 2013-10-16 13:25:16 +0000
3+++ apps/patchwork/utils.py 2013-12-05 18:34:32 +0000
4@@ -50,6 +50,9 @@
5 DEVNULL = open(os.devnull, "w")
6 atexit.register(close_fo, DEVNULL)
7
8+# Default date format.
9+DATE_FORMAT = '%Y-%m-%d %H:%M:%S.%f'
10+
11
12 class GitException(Exception):
13 """General class for git exceptions."""
14@@ -501,9 +504,7 @@
15
16 is_valid = False
17 if cached_user:
18- # TODO: we need to set a timeout and check again even if the user
19- # is cached, it might have been removed from Crowd.
20- is_valid |= cached_user[1]
21+ is_valid |= check_cached_user_validity(cached_user, db, crowd)
22 else:
23 # XXX: The first time this runs, it might hit the Crowd server hard,
24 # since none of the users will be cached.
25@@ -514,6 +515,25 @@
26 return is_valid
27
28
29+def check_cached_user_validity(cached_user, db, crowd):
30+ """Checks the user timestamp, and in case re-checks user validity.
31+
32+ :param chached_user: The user from the local cache.
33+ :param db: A PatchworkDB instance for the cache backend.
34+ :param crowd: A Crowd instance to update user data if necessary.
35+ """
36+ date_inserted = datetime.datetime.strptime(cached_user[2], DATE_FORMAT)
37+ today = datetime.datetime.now()
38+ delta = today - date_inserted
39+
40+ valid_user = cached_user[1]
41+ if delta.days >= settings.CROWD_USERS_DB_EXPIRY:
42+ valid_user = crowd.is_valid_user(cached_user[0])
43+ db.update_user(cached_user[0], valid_user, today)
44+
45+ return valid_user
46+
47+
48 def get_patches_matching_commit(project, commit_diff, commit_msg,
49 commit_author):
50 """Return the Patch objects that are likely matches to the given commit.
51
52=== modified file 'apps/settings.py'
53--- apps/settings.py 2013-12-03 09:38:31 +0000
54+++ apps/settings.py 2013-12-05 18:34:32 +0000
55@@ -150,6 +150,8 @@
56 # Full path to a DB file.
57 # This is used to have a cache of which users exists in the Crowd database.
58 CROWD_USERS_DB_FILE = None
59+# Number of days before checking again if a user exists in the Crowd db.
60+CROWD_USERS_DB_EXPIRY = 30
61
62 # Where to store the git repos that we scan to identify which patches have
63 # been committed.

Subscribers

People subscribed via source and target branches