equal
deleted
inserted
replaced
1 # copyright 2003-2012 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
1 # copyright 2003-2014 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
3 # |
3 # |
4 # This file is part of CubicWeb. |
4 # This file is part of CubicWeb. |
5 # |
5 # |
6 # CubicWeb is free software: you can redistribute it and/or modify it under the |
6 # CubicWeb is free software: you can redistribute it and/or modify it under the |
32 # XXX use named args and inner functions to avoid referencing globals |
32 # XXX use named args and inner functions to avoid referencing globals |
33 # which may cause reloading pb |
33 # which may cause reloading pb |
34 lifetime = timedelta(days=self.repo.config['keep-transaction-lifetime']) |
34 lifetime = timedelta(days=self.repo.config['keep-transaction-lifetime']) |
35 def cleanup_old_transactions(repo=self.repo, lifetime=lifetime): |
35 def cleanup_old_transactions(repo=self.repo, lifetime=lifetime): |
36 mindate = datetime.now() - lifetime |
36 mindate = datetime.now() - lifetime |
37 session = repo.internal_session() |
37 with repo.internal_cnx() as cnx: |
38 try: |
38 cnx.system_sql( |
39 session.system_sql( |
|
40 'DELETE FROM transactions WHERE tx_time < %(time)s', |
39 'DELETE FROM transactions WHERE tx_time < %(time)s', |
41 {'time': mindate}) |
40 {'time': mindate}) |
42 session.commit() |
41 cnx.commit() |
43 finally: |
|
44 session.close() |
|
45 if self.repo.config['undo-enabled']: |
42 if self.repo.config['undo-enabled']: |
46 self.repo.looping_task(60*60*24, cleanup_old_transactions, |
43 self.repo.looping_task(60*60*24, cleanup_old_transactions, |
47 self.repo) |
44 self.repo) |
48 |
45 |
49 class UpdateFeedsStartupHook(hook.Hook): |
46 class UpdateFeedsStartupHook(hook.Hook): |
58 for uri, source in list(repo.sources_by_uri.iteritems()): |
55 for uri, source in list(repo.sources_by_uri.iteritems()): |
59 if (uri == 'system' |
56 if (uri == 'system' |
60 or not repo.config.source_enabled(source) |
57 or not repo.config.source_enabled(source) |
61 or not source.config['synchronize']): |
58 or not source.config['synchronize']): |
62 continue |
59 continue |
63 session = repo.internal_session(safe=True) |
60 with repo.internal_cnx() as cnx: |
64 try: |
61 try: |
65 source.pull_data(session) |
62 source.pull_data(cnx) |
66 except Exception as exc: |
63 except Exception as exc: |
67 session.exception('while trying to update feed %s', source) |
64 cnx.exception('while trying to update feed %s', source) |
68 finally: |
|
69 session.close() |
|
70 self.repo.looping_task(60, update_feeds, self.repo) |
65 self.repo.looping_task(60, update_feeds, self.repo) |
71 |
66 |
72 |
67 |
73 class DataImportsCleanupStartupHook(hook.Hook): |
68 class DataImportsCleanupStartupHook(hook.Hook): |
74 """start task to cleanup old data imports (ie datafeed import logs)""" |
69 """start task to cleanup old data imports (ie datafeed import logs)""" |
79 def expire_dataimports(repo=self.repo): |
74 def expire_dataimports(repo=self.repo): |
80 for uri, source in repo.sources_by_uri.iteritems(): |
75 for uri, source in repo.sources_by_uri.iteritems(): |
81 if (uri == 'system' |
76 if (uri == 'system' |
82 or not repo.config.source_enabled(source)): |
77 or not repo.config.source_enabled(source)): |
83 continue |
78 continue |
84 session = repo.internal_session() |
79 with repo.internal_cnx() as cnx: |
85 try: |
|
86 mindate = datetime.now() - timedelta(seconds=source.config['logs-lifetime']) |
80 mindate = datetime.now() - timedelta(seconds=source.config['logs-lifetime']) |
87 session.execute('DELETE CWDataImport X WHERE X start_timestamp < %(time)s', |
81 cnx.execute('DELETE CWDataImport X WHERE X start_timestamp < %(time)s', |
88 {'time': mindate}) |
82 {'time': mindate}) |
89 session.commit() |
83 cnx.commit() |
90 finally: |
|
91 session.close() |
|
92 self.repo.looping_task(60*60*24, expire_dataimports, self.repo) |
84 self.repo.looping_task(60*60*24, expire_dataimports, self.repo) |