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