##// END OF EJS Templates
small changes for scheduler fixes of EE edition
super-admin -
r5138:14e25636 default
parent child Browse files
Show More

The requested changes are too big and content was truncated. Show full diff

@@ -1,139 +1,133 b''
1 1 # Copyright (C) 2010-2023 RhodeCode GmbH
2 2 #
3 3 # This program is free software: you can redistribute it and/or modify
4 4 # it under the terms of the GNU Affero General Public License, version 3
5 5 # (only), as published by the Free Software Foundation.
6 6 #
7 7 # This program is distributed in the hope that it will be useful,
8 8 # but WITHOUT ANY WARRANTY; without even the implied warranty of
9 9 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 10 # GNU General Public License for more details.
11 11 #
12 12 # You should have received a copy of the GNU Affero General Public License
13 13 # along with this program. If not, see <http://www.gnu.org/licenses/>.
14 14 #
15 15 # This program is dual-licensed. If you wish to learn more about the
16 16 # RhodeCode Enterprise Edition, including its added features, Support services,
17 17 # and proprietary license terms, please see https://rhodecode.com/licenses/
18 18
19 19 import logging
20 20 import datetime
21 21 import time
22 22
23 23 from functools import partial
24 24
25 25 import configparser
26 26 from celery.result import AsyncResult
27 27 import celery.loaders.base
28 28 import celery.schedules
29 29
30 30 from rhodecode.lib.ext_json import sjson as json
31 31
32 32 log = logging.getLogger(__name__)
33 33
34 34
35 35 def get_task_id(task):
36 36 task_id = None
37 37 if isinstance(task, AsyncResult):
38 38 task_id = task.task_id
39 39
40 40 return task_id
41 41
42 42
43 43 def crontab(value):
44 44 return celery.schedules.crontab(**value)
45 45
46 46
47 47 def timedelta(value):
48 48 return datetime.timedelta(**value)
49 49
50 50
51 51 def safe_json(get, section, key):
52 52 value = ''
53 53 try:
54 54 value = get(key)
55 55 json_value = json.loads(value)
56 56 except ValueError:
57 msg = 'The %s=%s is not valid json in section %s' % (
58 key, value, section
59 )
57 msg = f'The {key}={value} is not valid json in section {section}'
60 58 raise ValueError(msg)
61 59
62 60 return json_value
63 61
64 62
65 63 def raw_2_schedule(schedule_value, schedule_type):
66 64 schedule_type_map = {
67 65 'crontab': crontab,
68 66 'timedelta': timedelta,
69 67 'integer': int
70 68 }
71 69 scheduler_cls = schedule_type_map.get(schedule_type)
72 70
73 71 if scheduler_cls is None:
74 raise ValueError(
75 'schedule type %s in section is invalid' % (
76 schedule_type,
77 )
78 )
72 raise ValueError(f'schedule type {schedule_type} in section is invalid')
79 73 try:
80 74 schedule = scheduler_cls(schedule_value)
81 75 except TypeError:
82 76 log.exception('Failed to compose a schedule from value: %r', schedule_value)
83 77 schedule = None
84 78 return schedule
85 79
86 80
87 81 def get_beat_config(parser, section):
88 82
89 83 get = partial(parser.get, section)
90 84 has_option = partial(parser.has_option, section)
91 85
92 86 schedule_type = get('type')
93 87 schedule_value = safe_json(get, section, 'schedule')
94 88
95 89 config = {
96 90 'schedule_type': schedule_type,
97 91 'schedule_value': schedule_value,
98 92 'task': get('task'),
99 93 }
100 94 schedule = raw_2_schedule(schedule_value, schedule_type)
101 95 if schedule:
102 96 config['schedule'] = schedule
103 97
104 98 if has_option('args'):
105 99 config['args'] = safe_json(get, section, 'args')
106 100
107 101 if has_option('kwargs'):
108 102 config['kwargs'] = safe_json(get, section, 'kwargs')
109 103
110 104 if has_option('force_update'):
111 105 config['force_update'] = get('force_update')
112 106
113 107 return config
114 108
115 109
116 110 def parse_ini_vars(ini_vars):
117 111 options = {}
118 112 for pairs in ini_vars.split(','):
119 113 key, value = pairs.split('=')
120 114 options[key] = value
121 115 return options
122 116
123 117
124 118 def ping_db():
125 119 from rhodecode.model import meta
126 120 from rhodecode.model.db import DbMigrateVersion
127 121 log.info('Testing DB connection...')
128 122
129 123 for test in range(10):
130 124 try:
131 125 scalar = DbMigrateVersion.query().scalar()
132 126 log.debug('DB PING %s@%s', scalar, scalar.version)
133 127 break
134 128 except Exception:
135 129 retry = 1
136 130 log.debug('DB not ready, next try in %ss', retry)
137 131 time.sleep(retry)
138 132 finally:
139 133 meta.Session.remove()
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
General Comments 0
You need to be logged in to leave comments. Login now