Show More
@@ -3,7 +3,6 b' This library is provided to allow standa' | |||
|
3 | 3 | to output log data as JSON formatted strings |
|
4 | 4 | ''' |
|
5 | 5 | import logging |
|
6 | import json | |
|
7 | 6 | import re |
|
8 | 7 | from datetime import date, datetime, time, tzinfo, timedelta |
|
9 | 8 | import traceback |
@@ -13,13 +12,8 b' from inspect import istraceback' | |||
|
13 | 12 | |
|
14 | 13 | from collections import OrderedDict |
|
15 | 14 | |
|
16 | ||
|
17 | def _inject_req_id(record, *args, **kwargs): | |
|
18 | return record | |
|
19 | ||
|
20 | ||
|
21 | ExceptionAwareFormatter = logging.Formatter | |
|
22 | ||
|
15 | from ...logging_formatter import _inject_req_id, ExceptionAwareFormatter | |
|
16 | from ...ext_json import sjson as json | |
|
23 | 17 | |
|
24 | 18 | ZERO = timedelta(0) |
|
25 | 19 | HOUR = timedelta(hours=1) |
@@ -111,11 +111,12 b' class Lock:' | |||
|
111 | 111 | extend_script = None |
|
112 | 112 | reset_script = None |
|
113 | 113 | reset_all_script = None |
|
114 | blocking = None | |
|
114 | 115 | |
|
115 | 116 | _lock_renewal_interval: float |
|
116 | 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 | 121 | :param redis_client: |
|
121 | 122 | An instance of :class:`~StrictRedis`. |
@@ -143,6 +144,9 b' class Lock:' | |||
|
143 | 144 | If set ``True`` then the ``redis_client`` needs to be an instance of ``redis.StrictRedis``. |
|
144 | 145 | :param signal_expire: |
|
145 | 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 | 151 | if strict and not isinstance(redis_client, StrictRedis): |
|
148 | 152 | raise ValueError("redis_client must be instance of StrictRedis. " |
@@ -179,6 +183,8 b' class Lock:' | |||
|
179 | 183 | else None) |
|
180 | 184 | self._lock_renewal_thread = None |
|
181 | 185 | |
|
186 | self.blocking = blocking | |
|
187 | ||
|
182 | 188 | self.register_scripts(redis_client) |
|
183 | 189 | |
|
184 | 190 | @classmethod |
@@ -342,9 +348,11 b' class Lock:' | |||
|
342 | 348 | loggers["refresh.exit"].debug("Renewal thread for Lock(%r) exited.", self._name) |
|
343 | 349 | |
|
344 | 350 | def __enter__(self): |
|
345 |
acquired = self.acquire(blocking= |
|
|
351 | acquired = self.acquire(blocking=self.blocking) | |
|
346 | 352 | if not acquired: |
|
353 | if self.blocking: | |
|
347 | 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 | 356 | return self |
|
349 | 357 | |
|
350 | 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 | 15 | def statsd_config(config, prefix='statsd.'): |
|
16 | 16 | _config = {} |
|
17 | for key in config.keys(): | |
|
17 | for key in list(config.keys()): | |
|
18 | 18 | if key.startswith(prefix): |
|
19 | 19 | _config[key[len(prefix):]] = config[key] |
|
20 | 20 | return _config |
@@ -24,9 +24,10 b' def client_from_config(configuration, pr' | |||
|
24 | 24 | from pyramid.settings import asbool |
|
25 | 25 | |
|
26 | 26 | _config = statsd_config(configuration, prefix) |
|
27 | statsd_flag = _config.get('enabled') | |
|
27 | 28 | statsd_enabled = asbool(_config.pop('enabled', False)) |
|
28 | 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 | 31 | return |
|
31 | 32 | |
|
32 | 33 | host = _config.pop('statsd_host', HOST) |
@@ -1,2 +1,14 b'' | |||
|
1 | # use orjson by default | |
|
1 | import json as stdlib_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 | |
|
2 | 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 | 48 | class ColorFormatter(logging.Formatter): |
|
41 | 49 | |
|
42 | 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 | 54 | def_record = super().format(record) |
|
47 | 55 | |
@@ -51,3 +59,5 b' class ColorFormatter(logging.Formatter):' | |||
|
51 | 59 | |
|
52 | 60 | colored_record = ''.join([start, def_record, end]) |
|
53 | 61 | return colored_record |
|
62 | ||
|
63 |
General Comments 0
You need to be logged in to leave comments.
Login now