Show More
@@ -1,54 +1,70 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 sys | |||
22 | import atexit |
|
23 | import atexit | |
23 | import logging |
|
24 | import logging | |
24 | import signal |
|
25 | import signal | |
|
26 | import rhodecode | |||
25 |
|
27 | |||
26 | log = logging.getLogger(__name__) |
|
28 | log = logging.getLogger(__name__) | |
27 |
|
29 | |||
28 | cache_keys_by_pid = set() |
|
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 | def free_cache_keys(*args): |
|
43 | def free_cache_keys(*args): | |
|
44 | from rhodecode.model.db import Session, CacheKey | |||
|
45 | ||||
|
46 | if rhodecode.is_test: | |||
|
47 | return | |||
|
48 | ||||
32 | ssh_cmd = os.environ.get('RC_CMD_SSH_WRAPPER') |
|
49 | ssh_cmd = os.environ.get('RC_CMD_SSH_WRAPPER') | |
33 | if ssh_cmd: |
|
50 | if ssh_cmd: | |
34 | return |
|
51 | return | |
35 |
|
52 | |||
36 | from rhodecode.model.db import Session, CacheKey |
|
|||
37 |
|
||||
38 | if cache_keys_by_pid: |
|
53 | if cache_keys_by_pid: | |
39 | try: |
|
54 | try: | |
40 | for cache_proc in set(cache_keys_by_pid): |
|
55 | for cache_proc in set(cache_keys_by_pid): | |
41 | like_expression = '{}%'.format(cache_proc) |
|
56 | like_expression = '{}%'.format(cache_proc) | |
42 | qry = CacheKey.query().filter(CacheKey.cache_key.like(like_expression)) |
|
57 | qry = CacheKey.query().filter(CacheKey.cache_key.like(like_expression)) | |
43 | count = qry.count() |
|
58 | count = qry.count() | |
44 | log.info('Clearing %s: %s cache keys, total: %s', cache_proc, len(cache_keys_by_pid), count) |
|
59 | log.info('Clearing %s: %s cache keys, total: %s', cache_proc, len(cache_keys_by_pid), count) | |
45 | qry.delete(synchronize_session='fetch') |
|
60 | qry.delete(synchronize_session='fetch') | |
46 | cache_keys_by_pid.remove(cache_proc) |
|
61 | cache_keys_by_pid.remove(cache_proc) | |
47 | Session().commit() |
|
62 | Session().commit() | |
48 | except Exception: |
|
63 | except Exception: | |
49 | log.exception('Failed to clear keys, exiting gracefully') |
|
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) |
|
68 | signal.signal(signal.SIGTERM, sigHandler) | |
53 |
signal.signal(signal.SIGT |
|
69 | signal.signal(signal.SIGINT, sigHandler) | |
54 | signal.signal(signal.SIGINT, free_cache_keys) |
|
70 |
General Comments 0
You need to be logged in to leave comments.
Login now