1
1
import asyncio
2
2
import functools
3
3
import logging
4
+ import ssl
4
5
from dataclasses import dataclass
5
6
from datetime import datetime , timedelta
6
7
from operator import attrgetter
7
- from ssl import SSLContext
8
- from typing import Any , Callable , List , Optional , Tuple , Union
8
+ from typing import Any , Callable , Generator , List , Optional , Tuple , Union
9
+ from urllib . parse import urlparse
9
10
from uuid import uuid4
10
11
11
12
import aioredis
12
13
from aioredis import MultiExecError , Redis
14
+ from pydantic .validators import make_arbitrary_type_validator
13
15
14
16
from .constants import default_queue_name , job_key_prefix , result_key_prefix
15
17
from .jobs import Deserializer , Job , JobDef , JobResult , Serializer , deserialize_job , serialize_job
18
20
logger = logging .getLogger ('arq.connections' )
19
21
20
22
23
+ class SSLContext (ssl .SSLContext ):
24
+ """
25
+ Required to avoid problems with
26
+ """
27
+
28
+ @classmethod
29
+ def __get_validators__ (cls ) -> Generator [Callable [..., Any ], None , None ]:
30
+ yield make_arbitrary_type_validator (ssl .SSLContext )
31
+
32
+
21
33
@dataclass
22
34
class RedisSettings :
23
35
"""
@@ -38,8 +50,20 @@ class RedisSettings:
38
50
sentinel : bool = False
39
51
sentinel_master : str = 'mymaster'
40
52
53
+ @classmethod
54
+ def from_dsn (cls , dsn : str ) -> 'RedisSettings' :
55
+ conf = urlparse (dsn )
56
+ assert conf .scheme in {'redis' , 'rediss' }, 'invalid DSN scheme'
57
+ return RedisSettings (
58
+ host = conf .hostname or 'localhost' ,
59
+ port = conf .port or 6379 ,
60
+ ssl = conf .scheme == 'rediss' ,
61
+ password = conf .password ,
62
+ database = int ((conf .path or '0' ).strip ('/' )),
63
+ )
64
+
41
65
def __repr__ (self ) -> str :
42
- return '< RedisSettings {}> ' .format (' ' .join (f'{ k } ={ v } ' for k , v in self .__dict__ .items ()))
66
+ return 'RedisSettings({}) ' .format (', ' .join (f'{ k } ={ v !r } ' for k , v in self .__dict__ .items ()))
43
67
44
68
45
69
# extra time after the job is expected to start when the job key should expire, 1 day in ms
0 commit comments