Kiln » Kiln Storage Service Read More
Clone URL:  
tailtracking.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import os import sys import settings from redis import Redis from bugzscout import report_exception def _get_redis(): return Redis(host=settings.SYNC_REDIS_HOST, port=settings.SYNC_REDIS_PORT, db=settings.TAILS_REDIS_DB) def _do_sync(): return getattr(settings, 'HOSTED', False) and getattr(settings, 'DO_SYNCSTATUS', False) def add_tail(repo, redis=None): if _do_sync(): try: if not redis: redis = _get_redis() tail = repo.repo[0].hex() redis.sadd(tail, repo.uuid) return tail except Exception, e: report_exception(e) def random_tail(repo, redis=None): if _do_sync(): try: if not redis: redis = _get_redis() tail = add_tail(repo, redis) if redis.scard(tail) < 2: return None for i in range(5): # Try a few times to make sure we do have another option # before bailing out with None. other = redis.srandmember(tail) if other != repo.uuid: return other return None except Exception, e: # We never want to error out from here since it would break sync. report_exception(e) def main(argv): from api.repositories import Repository if _do_sync(): repos = os.listdir(settings.KILN_REPOSITORY_ROOT) i = 1 redis = _get_redis() redis.flushdb() for folder in repos: r = Repository(folder, suppresshooks=True) if r.exists(): add_tail(r) sys.stdout.write(('Added %d of %d tails (%s)' % (i, len(repos), folder)).ljust(78) + '\r') i += 1 if len(argv) >= 1 and argv[0] == '--relink-all': i = 1 redis = _get_redis() for folder in repos: r = Repository(folder, suppresshooks=True) if r.exists(): r.relink() sys.stdout.write(('Relinking %d of %d repos (%s)' % (i, len(repos), folder)).ljust(78) + '\r') i += 1 sys.stdout.write('Done'.ljust(78) + '\n') else: print "We don't do sync status on this box." if __name__ == '__main__': main(sys.argv[1:])