##// END OF EJS Templates
core: synced vendor/ext_json with ce for better compatability
super-admin -
r1250:2c57bb5b default
parent child Browse files
Show More
@@ -3,7 +3,6 b' This library is provided to allow standa'
3 to output log data as JSON formatted strings
3 to output log data as JSON formatted strings
4 '''
4 '''
5 import logging
5 import logging
6 import json
7 import re
6 import re
8 from datetime import date, datetime, time, tzinfo, timedelta
7 from datetime import date, datetime, time, tzinfo, timedelta
9 import traceback
8 import traceback
@@ -13,13 +12,8 b' from inspect import istraceback'
13
12
14 from collections import OrderedDict
13 from collections import OrderedDict
15
14
16
15 from ...logging_formatter import _inject_req_id, ExceptionAwareFormatter
17 def _inject_req_id(record, *args, **kwargs):
16 from ...ext_json import sjson as json
18 return record
19
20
21 ExceptionAwareFormatter = logging.Formatter
22
23
17
24 ZERO = timedelta(0)
18 ZERO = timedelta(0)
25 HOUR = timedelta(hours=1)
19 HOUR = timedelta(hours=1)
@@ -111,11 +111,12 b' class Lock:'
111 extend_script = None
111 extend_script = None
112 reset_script = None
112 reset_script = None
113 reset_all_script = None
113 reset_all_script = None
114 blocking = None
114
115
115 _lock_renewal_interval: float
116 _lock_renewal_interval: float
116 _lock_renewal_thread: Union[threading.Thread, None]
117 _lock_renewal_thread: Union[threading.Thread, None]
117
118
118 def __init__(self, redis_client, name, expire=None, id=None, auto_renewal=False, strict=True, signal_expire=1000):
119 def __init__(self, redis_client, name, expire=None, id=None, auto_renewal=False, strict=True, signal_expire=1000, blocking=True):
119 """
120 """
120 :param redis_client:
121 :param redis_client:
121 An instance of :class:`~StrictRedis`.
122 An instance of :class:`~StrictRedis`.
@@ -143,6 +144,9 b' class Lock:'
143 If set ``True`` then the ``redis_client`` needs to be an instance of ``redis.StrictRedis``.
144 If set ``True`` then the ``redis_client`` needs to be an instance of ``redis.StrictRedis``.
144 :param signal_expire:
145 :param signal_expire:
145 Advanced option to override signal list expiration in milliseconds. Increase it for very slow clients. Default: ``1000``.
146 Advanced option to override signal list expiration in milliseconds. Increase it for very slow clients. Default: ``1000``.
147 :param blocking:
148 Boolean value specifying whether lock should be blocking or not.
149 Used in `__enter__` method.
146 """
150 """
147 if strict and not isinstance(redis_client, StrictRedis):
151 if strict and not isinstance(redis_client, StrictRedis):
148 raise ValueError("redis_client must be instance of StrictRedis. "
152 raise ValueError("redis_client must be instance of StrictRedis. "
@@ -179,6 +183,8 b' class Lock:'
179 else None)
183 else None)
180 self._lock_renewal_thread = None
184 self._lock_renewal_thread = None
181
185
186 self.blocking = blocking
187
182 self.register_scripts(redis_client)
188 self.register_scripts(redis_client)
183
189
184 @classmethod
190 @classmethod
@@ -342,9 +348,11 b' class Lock:'
342 loggers["refresh.exit"].debug("Renewal thread for Lock(%r) exited.", self._name)
348 loggers["refresh.exit"].debug("Renewal thread for Lock(%r) exited.", self._name)
343
349
344 def __enter__(self):
350 def __enter__(self):
345 acquired = self.acquire(blocking=True)
351 acquired = self.acquire(blocking=self.blocking)
346 if not acquired:
352 if not acquired:
347 raise AssertionError(f"Lock({self._name}) wasn't acquired, but blocking=True was used!")
353 if self.blocking:
354 raise AssertionError(f"Lock({self._name}) wasn't acquired, but blocking=True was used!")
355 raise NotAcquired(f"Lock({self._name}) is not acquired or it already expired.")
348 return self
356 return self
349
357
350 def __exit__(self, exc_type=None, exc_value=None, traceback=None):
358 def __exit__(self, exc_type=None, exc_value=None, traceback=None):
@@ -14,7 +14,7 b" log = logging.getLogger('rhodecode.stats"
14
14
15 def statsd_config(config, prefix='statsd.'):
15 def statsd_config(config, prefix='statsd.'):
16 _config = {}
16 _config = {}
17 for key in config.keys():
17 for key in list(config.keys()):
18 if key.startswith(prefix):
18 if key.startswith(prefix):
19 _config[key[len(prefix):]] = config[key]
19 _config[key[len(prefix):]] = config[key]
20 return _config
20 return _config
@@ -24,9 +24,10 b' def client_from_config(configuration, pr'
24 from pyramid.settings import asbool
24 from pyramid.settings import asbool
25
25
26 _config = statsd_config(configuration, prefix)
26 _config = statsd_config(configuration, prefix)
27 statsd_flag = _config.get('enabled')
27 statsd_enabled = asbool(_config.pop('enabled', False))
28 statsd_enabled = asbool(_config.pop('enabled', False))
28 if not statsd_enabled:
29 if not statsd_enabled:
29 log.debug('statsd client not enabled by statsd.enabled = flag, skipping...')
30 log.debug('statsd client not enabled by statsd.enabled = %s flag, skipping...', statsd_flag)
30 return
31 return
31
32
32 host = _config.pop('statsd_host', HOST)
33 host = _config.pop('statsd_host', HOST)
@@ -1,2 +1,14 b''
1 # use orjson by default
1 import json as stdlib_json
2 import orjson as json
2
3 try:
4 # we keep simplejson for having dump functionality still
5 # orjson doesn't support it
6 import simplejson as sjson
7 except ImportError:
8 sjson = stdlib_json
9
10 try:
11 import orjson
12 import orjson as json
13 except ImportError:
14 json = stdlib_json
@@ -37,11 +37,19 b' COLORS = {'
37 }
37 }
38
38
39
39
40 def _inject_req_id(record, *args, **kwargs):
41 return record
42
43
44 class ExceptionAwareFormatter(logging.Formatter):
45 pass
46
47
40 class ColorFormatter(logging.Formatter):
48 class ColorFormatter(logging.Formatter):
41
49
42 def format(self, record):
50 def format(self, record):
43 """
51 """
44 Change record's levelname to use with COLORS enum
52 Changes record's levelname to use with COLORS enum
45 """
53 """
46 def_record = super().format(record)
54 def_record = super().format(record)
47
55
@@ -51,3 +59,5 b' class ColorFormatter(logging.Formatter):'
51
59
52 colored_record = ''.join([start, def_record, end])
60 colored_record = ''.join([start, def_record, end])
53 return colored_record
61 return colored_record
62
63
General Comments 0
You need to be logged in to leave comments. Login now