##// END OF EJS Templates
caches: log properly how we cleanup cache keys
super-admin -
r4850:cbf6ce71 default
parent child Browse files
Show More
@@ -1,53 +1,54 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2
2
3 # Copyright (C) 2015-2020 RhodeCode GmbH
3 # Copyright (C) 2015-2020 RhodeCode GmbH
4 #
4 #
5 # This program is free software: you can redistribute it and/or modify
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU Affero General Public License, version 3
6 # it under the terms of the GNU Affero General Public License, version 3
7 # (only), as published by the Free Software Foundation.
7 # (only), as published by the Free Software Foundation.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU Affero General Public License
14 # You should have received a copy of the GNU Affero General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 #
16 #
17 # This program is dual-licensed. If you wish to learn more about the
17 # This program is dual-licensed. If you wish to learn more about the
18 # RhodeCode Enterprise Edition, including its added features, Support services,
18 # RhodeCode Enterprise Edition, including its added features, Support services,
19 # and proprietary license terms, please see https://rhodecode.com/licenses/
19 # and proprietary license terms, please see https://rhodecode.com/licenses/
20
20
21 import os
21 import os
22 import atexit
22 import atexit
23 import logging
23 import logging
24 import signal
24 import signal
25
25
26 log = logging.getLogger(__name__)
26 log = logging.getLogger(__name__)
27
27
28 cache_keys_by_pid = set()
28 cache_keys_by_pid = set()
29
29
30
30
31 def free_cache_keys(*args):
31 def free_cache_keys(*args):
32 ssh_cmd = os.environ.get('RC_CMD_SSH_WRAPPER')
32 ssh_cmd = os.environ.get('RC_CMD_SSH_WRAPPER')
33 if ssh_cmd:
33 if ssh_cmd:
34 return
34 return
35
35
36 from rhodecode.model.db import Session, CacheKey
36 from rhodecode.model.db import Session, CacheKey
37 log.info('Clearing %s cache keys', len(cache_keys_by_pid))
38
37
39 if cache_keys_by_pid:
38 if cache_keys_by_pid:
40 try:
39 try:
41 for cache_proc in set(cache_keys_by_pid):
40 for cache_proc in set(cache_keys_by_pid):
42 like_expression = '{}%'.format(cache_proc)
41 like_expression = '{}%'.format(cache_proc)
43 CacheKey.query().filter(
42 qry = CacheKey.query().filter(CacheKey.cache_key.like(like_expression))
44 CacheKey.cache_key.like(like_expression)).delete(synchronize_session='fetch')
43 count = qry.count()
44 log.info('Clearing %s: %s cache keys, total: %s', cache_proc, len(cache_keys_by_pid), count)
45 qry.delete(synchronize_session='fetch')
45 cache_keys_by_pid.remove(cache_proc)
46 cache_keys_by_pid.remove(cache_proc)
46 Session().commit()
47 Session().commit()
47 except Exception:
48 except Exception:
48 log.exception('Failed to clear keys, exiting gracefully')
49 log.exception('Failed to clear keys, exiting gracefully')
49
50
50
51
51 atexit.register(free_cache_keys)
52 atexit.register(free_cache_keys)
52 signal.signal(signal.SIGTERM, free_cache_keys)
53 signal.signal(signal.SIGTERM, free_cache_keys)
53 signal.signal(signal.SIGINT, free_cache_keys)
54 signal.signal(signal.SIGINT, free_cache_keys)
General Comments 0
You need to be logged in to leave comments. Login now