|
1 # copyright 2003-2014 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
|
2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
|
3 # |
|
4 # This file is part of CubicWeb. |
|
5 # |
|
6 # CubicWeb is free software: you can redistribute it and/or modify it under the |
|
7 # terms of the GNU Lesser General Public License as published by the Free |
|
8 # Software Foundation, either version 2.1 of the License, or (at your option) |
|
9 # any later version. |
|
10 # |
|
11 # CubicWeb is distributed in the hope that it will be useful, but WITHOUT |
|
12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
|
13 # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
|
14 # details. |
|
15 # |
|
16 # You should have received a copy of the GNU Lesser General Public License along |
|
17 # with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
|
18 """core hooks registering some maintainance tasks as server startup time""" |
|
19 |
|
20 __docformat__ = "restructuredtext en" |
|
21 |
|
22 from datetime import timedelta, datetime |
|
23 |
|
24 from cubicweb.server import hook |
|
25 |
|
26 class TransactionsCleanupStartupHook(hook.Hook): |
|
27 """start task to cleanup transaction data""" |
|
28 __regid__ = 'cw.looping-tasks.transactions-cleanup' |
|
29 events = ('server_startup',) |
|
30 |
|
31 def __call__(self): |
|
32 # XXX use named args and inner functions to avoid referencing globals |
|
33 # which may cause reloading pb |
|
34 lifetime = timedelta(days=self.repo.config['keep-transaction-lifetime']) |
|
35 def cleanup_old_transactions(repo=self.repo, lifetime=lifetime): |
|
36 mindate = datetime.utcnow() - lifetime |
|
37 with repo.internal_cnx() as cnx: |
|
38 cnx.system_sql( |
|
39 'DELETE FROM transactions WHERE tx_time < %(time)s', |
|
40 {'time': mindate}) |
|
41 cnx.commit() |
|
42 if self.repo.config['undo-enabled']: |
|
43 self.repo.looping_task(60*60*24, cleanup_old_transactions, |
|
44 self.repo) |
|
45 |
|
46 class UpdateFeedsStartupHook(hook.Hook): |
|
47 """start task to update datafeed based sources""" |
|
48 __regid__ = 'cw.looping-tasks.update-feeds' |
|
49 events = ('server_startup',) |
|
50 |
|
51 def __call__(self): |
|
52 def update_feeds(repo): |
|
53 # take a list to avoid iterating on a dictionary whose size may |
|
54 # change |
|
55 for uri, source in list(repo.sources_by_uri.items()): |
|
56 if (uri == 'system' |
|
57 or not repo.config.source_enabled(source) |
|
58 or not source.config['synchronize']): |
|
59 continue |
|
60 with repo.internal_cnx() as cnx: |
|
61 try: |
|
62 source.pull_data(cnx) |
|
63 except Exception as exc: |
|
64 cnx.exception('while trying to update feed %s', source) |
|
65 self.repo.looping_task(60, update_feeds, self.repo) |
|
66 |
|
67 |
|
68 class DataImportsCleanupStartupHook(hook.Hook): |
|
69 """start task to cleanup old data imports (ie datafeed import logs)""" |
|
70 __regid__ = 'cw.looping-tasks.dataimports-cleanup' |
|
71 events = ('server_startup',) |
|
72 |
|
73 def __call__(self): |
|
74 def expire_dataimports(repo=self.repo): |
|
75 for uri, source in repo.sources_by_uri.items(): |
|
76 if (uri == 'system' |
|
77 or not repo.config.source_enabled(source)): |
|
78 continue |
|
79 with repo.internal_cnx() as cnx: |
|
80 mindate = datetime.utcnow() - timedelta(seconds=source.config['logs-lifetime']) |
|
81 cnx.execute('DELETE CWDataImport X WHERE X start_timestamp < %(time)s', |
|
82 {'time': mindate}) |
|
83 cnx.commit() |
|
84 self.repo.looping_task(60*60*24, expire_dataimports, self.repo) |