@@ -108,7 +108,7 @@ class ChannelJob:
108
108
job that are necessary to prioritise them.
109
109
110
110
Channel jobs are comparable according to the following rules:
111
- * jobs with an eta come before all other jobs
111
+ * jobs with an eta cannot be compared with jobs without
112
112
* then jobs with a smaller eta come first
113
113
* then jobs with a smaller priority come first
114
114
* then jobs with a smaller creation time come first
@@ -135,14 +135,18 @@ class ChannelJob:
135
135
>>> j3 < j1
136
136
True
137
137
138
- j4 and j5 comes even before j3, because they have an eta
138
+ j4 and j5 have an eta, they cannot be compared with j3
139
139
140
140
>>> j4 = ChannelJob(None, None, 4,
141
141
... seq=0, date_created=4, priority=9, eta=9)
142
142
>>> j5 = ChannelJob(None, None, 5,
143
143
... seq=0, date_created=5, priority=9, eta=9)
144
- >>> j4 < j5 < j3
144
+ >>> j4 < j5
145
145
True
146
+ >>> j4 < j3
147
+ Traceback (most recent call last):
148
+ ...
149
+ TypeError: '<' not supported between instances of 'int' and 'NoneType'
146
150
147
151
j6 has same date_created and priority as j5 but a smaller eta
148
152
@@ -153,7 +157,7 @@ class ChannelJob:
153
157
154
158
Here is the complete suite:
155
159
156
- >>> j6 < j4 < j5 < j3 < j1 < j2
160
+ >>> j6 < j4 < j5 and j3 < j1 < j2
157
161
True
158
162
159
163
j0 has the same properties as j1 but they are not considered
@@ -173,16 +177,13 @@ class ChannelJob:
173
177
174
178
"""
175
179
176
- __slots__ = ("db_name" , "channel" , "uuid" , "seq" , "date_created" , "priority" , "eta " , "__weakref__" )
180
+ __slots__ = ("db_name" , "channel" , "uuid" , "_sorting_key " , "__weakref__" )
177
181
178
182
def __init__ (self , db_name , channel , uuid , seq , date_created , priority , eta ):
179
183
self .db_name = db_name
180
184
self .channel = channel
181
185
self .uuid = uuid
182
- self .seq = seq
183
- self .date_created = date_created
184
- self .priority = priority
185
- self .eta = eta
186
+ self ._sorting_key = (eta , priority , date_created , seq )
186
187
187
188
def __repr__ (self ):
188
189
return "<ChannelJob %s>" % self .uuid
@@ -193,18 +194,36 @@ def __eq__(self, other):
193
194
def __hash__ (self ):
194
195
return id (self )
195
196
197
+ def set_no_eta (self ):
198
+ self ._sorting_key = (None ,) + self ._sorting_key [1 :]
199
+
200
+ @property
201
+ def seq (self ):
202
+ return self ._sorting_key [3 ]
203
+
204
+ @property
205
+ def date_created (self ):
206
+ return self ._sorting_key [2 ]
207
+
208
+ @property
209
+ def priority (self ):
210
+ return self ._sorting_key [1 ]
211
+
212
+ @property
213
+ def eta (self ):
214
+ return self ._sorting_key [0 ]
215
+
196
216
def sorting_key (self ):
197
- return self .eta , self .priority , self .date_created , self .seq
217
+ # DEPRECATED
218
+ return self ._sorting_key
198
219
199
220
def sorting_key_ignoring_eta (self ):
200
- return self .priority , self . date_created , self . seq
221
+ return self ._sorting_key [ 1 :]
201
222
202
223
def __lt__ (self , other ):
203
- if self .eta and not other .eta :
204
- return True
205
- elif not self .eta and other .eta :
206
- return False
207
- return self .sorting_key () < other .sorting_key ()
224
+ # Do not compare job where ETA is set with job where it is not
225
+ # If one job 'eta' is set, and the other is None, it raises TypeError
226
+ return self ._sorting_key < other ._sorting_key
208
227
209
228
210
229
class ChannelQueue :
@@ -314,7 +333,7 @@ def remove(self, job):
314
333
def pop (self , now ):
315
334
while self ._eta_queue and self ._eta_queue [0 ].eta <= now :
316
335
eta_job = self ._eta_queue .pop ()
317
- eta_job .eta = None
336
+ eta_job .set_no_eta ()
318
337
self ._queue .add (eta_job )
319
338
if self .sequential and self ._eta_queue and self ._queue :
320
339
eta_job = self ._eta_queue [0 ]
0 commit comments