allura
Révision | 5f1d5e17934804045a8ab0b9f8e8b76ae44b783e (tree) |
---|---|
l'heure | 2011-04-01 02:54:06 |
Auteur | Rick Copeland <rcopeland@geek...> |
Commiter | Rick Copeland |
[#1849] Make sure our reindex only tries to load 1024 project records at a time
Signed-off-by: Rick Copeland <rcopeland@geek.net>
@@ -7,6 +7,7 @@ from ming.orm import MappedClass, mapper, ThreadLocalORMSession, session, state | ||
7 | 7 | |
8 | 8 | import allura.tasks.index_tasks |
9 | 9 | from allura.lib.exceptions import CompoundError |
10 | +from allura.lib import utils | |
10 | 11 | from . import base |
11 | 12 | |
12 | 13 | class ShowModelsCommand(base.Command): |
@@ -39,37 +40,38 @@ class ReindexCommand(base.Command): | ||
39 | 40 | self.basic_setup() |
40 | 41 | graph = build_model_inheritance_graph() |
41 | 42 | if self.options.project: |
42 | - projects = [ M.Project.query.get(shortname=self.options.project) ] | |
43 | + q_project = M.Project.query.find(dict(shortname=self.options.project)) | |
43 | 44 | elif self.options.neighborhood: |
44 | 45 | neighborhood_id = M.Neighborhood.query.get( |
45 | 46 | url_prefix='/%s/' % self.options.neighborhood)._id |
46 | - projects = M.Project.query.find(dict(neighborhood_id=neighborhood_id)) | |
47 | + q_project = M.Project.query.find(dict(neighborhood_id=neighborhood_id)) | |
47 | 48 | else: |
48 | - projects = M.Project.query.find() | |
49 | + q_project = M.Project.query.find() | |
49 | 50 | seen_dbs = set() |
50 | - for p in projects: | |
51 | - if p.database_uri in seen_dbs: continue | |
52 | - seen_dbs.add(p.database_uri) | |
53 | - base.log.info('Reindex project %s', p.shortname) | |
54 | - c.project = p | |
55 | - for _, a_cls in dfs(M.Artifact, graph): | |
56 | - base.log.info(' %s', a_cls) | |
57 | - ref_ids = [] | |
58 | - for a in a_cls.query.find(): | |
51 | + for projects in utils.chunked_iterator(q_project): | |
52 | + for p in projects: | |
53 | + if p.database_uri in seen_dbs: continue | |
54 | + seen_dbs.add(p.database_uri) | |
55 | + base.log.info('Reindex project %s', p.shortname) | |
56 | + c.project = p | |
57 | + for _, a_cls in dfs(M.Artifact, graph): | |
58 | + base.log.info(' %s', a_cls) | |
59 | + ref_ids = [] | |
60 | + for a in a_cls.query.find(): | |
61 | + try: | |
62 | + M.ArtifactReference.from_artifact(a) | |
63 | + except: | |
64 | + base.log.exception('Making ArtifactReference from %s', a) | |
65 | + continue | |
66 | + ref_ids.append(a.index_id()) | |
67 | + M.artifact_orm_session.clear() | |
59 | 68 | try: |
60 | - M.ArtifactReference.from_artifact(a) | |
61 | - except: | |
62 | - base.log.exception('Making ArtifactReference from %s', a) | |
63 | - continue | |
64 | - ref_ids.append(a.index_id()) | |
65 | - M.artifact_orm_session.clear() | |
66 | - try: | |
67 | - allura.tasks.index_tasks.add_artifacts(ref_ids) | |
68 | - except CompoundError, err: | |
69 | - base.log.exception('Error indexing artifacts:\n%r', err) | |
70 | - base.log.error('%s', err.format_error()) | |
71 | - M.main_orm_session.flush() | |
72 | - M.main_orm_session.clear() | |
69 | + allura.tasks.index_tasks.add_artifacts(ref_ids) | |
70 | + except CompoundError, err: | |
71 | + base.log.exception('Error indexing artifacts:\n%r', err) | |
72 | + base.log.error('%s', err.format_error()) | |
73 | + M.main_orm_session.flush() | |
74 | + M.main_orm_session.clear() | |
73 | 75 | |
74 | 76 | class EnsureIndexCommand(base.Command): |
75 | 77 | min_args=0 |
@@ -87,3 +87,15 @@ class StatsHandler(WatchedFileHandler): | ||
87 | 87 | if v is not None) |
88 | 88 | record.exc_info = None # Never put tracebacks in the rtstats log |
89 | 89 | WatchedFileHandler.emit(self, record) |
90 | + | |
91 | +def chunked_iterator(query, pagesize=1024): | |
92 | + page = 0 | |
93 | + while True: | |
94 | + results = ( | |
95 | + query | |
96 | + .skip(pagesize*page) | |
97 | + .limit(pagesize) | |
98 | + .all()) | |
99 | + if not results: break | |
100 | + yield results | |
101 | + page += 1 |