@@ -32,54 +32,52 @@ def delete(self, connection: ConnectionType, job_name: str) -> None:
32
32
logger .debug (f"[registry { self ._key } ] Deleting { job_name } " )
33
33
connection .zrem (self ._key , job_name )
34
34
35
+ def exists (self , connection : ConnectionType , job_name : str ) -> bool :
36
+ return connection .zrank (self ._key , job_name ) is not None
37
+
35
38
36
39
class JobNamesRegistry (ZSetModel ):
37
40
_element_key_template : ClassVar [str ] = ":registry:{}"
38
41
39
- def __init__ (self , connection : ConnectionType , name : str ) -> None :
42
+ def __init__ (self , name : str ) -> None :
40
43
super ().__init__ (name = name )
41
- self .connection = connection
42
-
43
- def __len__ (self ) -> int :
44
- return self .count (self .connection )
45
-
46
- def __contains__ (self , item : str ) -> bool :
47
- return self .connection .zrank (self ._key , item ) is not None
48
44
49
- def all (self , start : int = 0 , end : int = - 1 ) -> List [str ]:
45
+ def all (self , connection : ConnectionType , start : int = 0 , end : int = - 1 ) -> List [str ]:
50
46
"""Returns a list of all job names.
51
47
48
+ :param connection: Broker connection
52
49
:param start: Start score/timestamp, default to 0.
53
50
:param end: End score/timestamp, default to -1 (i.e., no max score).
54
51
:returns: Returns a list of all job names with timestamp from start to end
55
52
"""
56
- self .cleanup (self . connection )
57
- res = [as_str (job_name ) for job_name in self . connection .zrange (self ._key , start , end )]
58
- logger .debug (f"Getting jobs for registry { self ._key } : { len (res )} found." )
53
+ self .cleanup (connection )
54
+ res = [as_str (job_name ) for job_name in connection .zrange (self ._key , start , end )]
55
+ logger .debug (f"Getting jobs for registry { self .key } : { len (res )} found." )
59
56
return res
60
57
61
- def all_with_timestamps (self , start : int = 0 , end : int = - 1 ) -> List [Tuple [str , float ]]:
58
+ def all_with_timestamps (self , connection : ConnectionType , start : int = 0 , end : int = - 1 ) -> List [Tuple [str , float ]]:
62
59
"""Returns a list of all job names with their timestamps.
63
60
61
+ :param connection: Broker connection
64
62
:param start: Start score/timestamp, default to 0.
65
63
:param end: End score/timestamp, default to -1 (i.e., no max score).
66
64
:returns: Returns a list of all job names with timestamp from start to end
67
65
"""
68
- self .cleanup (self . connection )
69
- res = self . connection .zrange (self ._key , start , end , withscores = True )
66
+ self .cleanup (connection )
67
+ res = connection .zrange (self ._key , start , end , withscores = True )
70
68
logger .debug (f"Getting jobs for registry { self ._key } : { len (res )} found." )
71
69
return [(as_str (job_name ), timestamp ) for job_name , timestamp in res ]
72
70
73
- def get_first (self ) -> Optional [str ]:
71
+ def get_first (self , connection : ConnectionType ) -> Optional [str ]:
74
72
"""Returns the first job in the registry."""
75
- self .cleanup (self . connection )
76
- first_job = self . connection .zrange (self ._key , 0 , 0 )
73
+ self .cleanup (connection )
74
+ first_job = connection .zrange (self ._key , 0 , 0 )
77
75
return first_job [0 ].decode () if first_job else None
78
76
79
- def get_last_timestamp (self ) -> Optional [int ]:
77
+ def get_last_timestamp (self , connection : ConnectionType ) -> Optional [int ]:
80
78
"""Returns the latest timestamp in the registry."""
81
- self .cleanup (self . connection )
82
- last_timestamp = self . connection .zrange (self ._key , - 1 , - 1 , withscores = True )
79
+ self .cleanup (connection )
80
+ last_timestamp = connection .zrange (self ._key , - 1 , - 1 , withscores = True )
83
81
return int (last_timestamp [0 ][1 ]) if last_timestamp else None
84
82
85
83
@property
@@ -88,7 +86,7 @@ def key(self) -> str:
88
86
89
87
@classmethod
90
88
def pop (
91
- cls , connection : ConnectionType , registries : Sequence [Self ], timeout : Optional [int ]
89
+ cls , connection : ConnectionType , registries : Sequence [Self ], timeout : Optional [int ]
92
90
) -> Tuple [Optional [str ], Optional [str ]]:
93
91
"""Helper method to abstract away from some Redis API details
94
92
0 commit comments