| Total Complexity | 42 |
| Total Lines | 157 |
| Duplicated Lines | 12.1 % |
| Changes | 0 | ||
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like TaskQueue often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | #!/usr/bin/env python |
||
| 120 | class TaskQueue(object): |
||
| 121 | ''' |
||
| 122 | task queue for scheduler, have a priority queue and a time queue for delayed tasks |
||
| 123 | ''' |
||
| 124 | processing_timeout = 10 * 60 |
||
| 125 | |||
| 126 | def __init__(self, rate=0, burst=0): |
||
| 127 | self.mutex = threading.RLock() |
||
| 128 | self.priority_queue = PriorityTaskQueue() |
||
| 129 | self.time_queue = PriorityTaskQueue() |
||
| 130 | self.processing = PriorityTaskQueue() |
||
| 131 | self.bucket = Bucket(rate=rate, burst=burst) |
||
| 132 | |||
| 133 | @property |
||
| 134 | def rate(self): |
||
| 135 | return self.bucket.rate |
||
| 136 | |||
| 137 | @rate.setter |
||
| 138 | def rate(self, value): |
||
| 139 | self.bucket.rate = value |
||
| 140 | |||
| 141 | @property |
||
| 142 | def burst(self): |
||
| 143 | return self.bucket.burst |
||
| 144 | |||
| 145 | @burst.setter |
||
| 146 | def burst(self, value): |
||
| 147 | self.bucket.burst = value |
||
| 148 | |||
| 149 | def check_update(self): |
||
| 150 | ''' |
||
| 151 | Check time queue and processing queue |
||
| 152 | |||
| 153 | put tasks to priority queue when execute time arrived or process timeout |
||
| 154 | ''' |
||
| 155 | self._check_time_queue() |
||
| 156 | self._check_processing() |
||
| 157 | |||
| 158 | View Code Duplication | def _check_time_queue(self): |
|
|
|
|||
| 159 | now = time.time() |
||
| 160 | self.mutex.acquire() |
||
| 161 | while self.time_queue.qsize() and self.time_queue.top and self.time_queue.top.exetime < now: |
||
| 162 | task = self.time_queue.get_nowait() |
||
| 163 | task.exetime = 0 |
||
| 164 | self.priority_queue.put(task) |
||
| 165 | self.mutex.release() |
||
| 166 | |||
| 167 | View Code Duplication | def _check_processing(self): |
|
| 168 | now = time.time() |
||
| 169 | self.mutex.acquire() |
||
| 170 | while self.processing.qsize() and self.processing.top and self.processing.top.exetime < now: |
||
| 171 | task = self.processing.get_nowait() |
||
| 172 | if task.taskid is None: |
||
| 173 | continue |
||
| 174 | task.exetime = 0 |
||
| 175 | self.priority_queue.put(task) |
||
| 176 | logger.info("processing: retry %s", task.taskid) |
||
| 177 | self.mutex.release() |
||
| 178 | |||
| 179 | def put(self, taskid, priority=0, exetime=0): |
||
| 180 | """ |
||
| 181 | Put a task into task queue |
||
| 182 | |||
| 183 | when use heap sort, if we put tasks(with the same priority and exetime=0) into queue, |
||
| 184 | the queue is not a strict FIFO queue, but more like a FILO stack. |
||
| 185 | |||
| 186 | It is very possible that when there are continuous big flow, the speed of select is |
||
| 187 | slower than request, resulting in priority-queue accumulation in short time. |
||
| 188 | |||
| 189 | In this scenario, the tasks more earlier entering the priority-queue will not get |
||
| 190 | processed until the request flow becomes small. |
||
| 191 | |||
| 192 | """ |
||
| 193 | now = time.time() |
||
| 194 | |||
| 195 | # give exetime to time.time() by default. So if two or more tasks |
||
| 196 | # have the same priority, we can still keep tasks in time order |
||
| 197 | # as much as possible. |
||
| 198 | task = InQueueTask(taskid, priority, exetime if exetime > 0 else now) |
||
| 199 | |||
| 200 | self.mutex.acquire() |
||
| 201 | if taskid in self.priority_queue: |
||
| 202 | self.priority_queue.put(task) |
||
| 203 | elif taskid in self.time_queue: |
||
| 204 | self.time_queue.put(task) |
||
| 205 | elif taskid in self.processing and self.processing[taskid].taskid: |
||
| 206 | # force update a processing task is not allowed as there are so many |
||
| 207 | # problems may happen |
||
| 208 | pass |
||
| 209 | else: |
||
| 210 | if exetime and exetime > now: |
||
| 211 | self.time_queue.put(task) |
||
| 212 | else: |
||
| 213 | self.priority_queue.put(task) |
||
| 214 | |||
| 215 | self.mutex.release() |
||
| 216 | |||
| 217 | def get(self): |
||
| 218 | '''Get a task from queue when bucket available''' |
||
| 219 | if self.bucket.get() < 1: |
||
| 220 | return None |
||
| 221 | now = time.time() |
||
| 222 | self.mutex.acquire() |
||
| 223 | try: |
||
| 224 | task = self.priority_queue.get_nowait() |
||
| 225 | self.bucket.desc() |
||
| 226 | except Queue.Empty: |
||
| 227 | self.mutex.release() |
||
| 228 | return None |
||
| 229 | task.exetime = now + self.processing_timeout |
||
| 230 | self.processing.put(task) |
||
| 231 | self.mutex.release() |
||
| 232 | return task.taskid |
||
| 233 | |||
| 234 | def done(self, taskid): |
||
| 235 | '''Mark task done''' |
||
| 236 | if taskid in self.processing: |
||
| 237 | self.mutex.acquire() |
||
| 238 | if taskid in self.processing: |
||
| 239 | del self.processing[taskid] |
||
| 240 | self.mutex.release() |
||
| 241 | return True |
||
| 242 | return False |
||
| 243 | |||
| 244 | def delete(self, taskid): |
||
| 245 | if taskid not in self: |
||
| 246 | return False |
||
| 247 | if taskid in self.priority_queue: |
||
| 248 | self.mutex.acquire() |
||
| 249 | del self.priority_queue[taskid] |
||
| 250 | self.mutex.release() |
||
| 251 | elif taskid in self.time_queue: |
||
| 252 | self.mutex.acquire() |
||
| 253 | del self.time_queue[taskid] |
||
| 254 | self.mutex.release() |
||
| 255 | elif taskid in self.processing: |
||
| 256 | self.done(taskid) |
||
| 257 | return True |
||
| 258 | |||
| 259 | def size(self): |
||
| 260 | return self.priority_queue.qsize() + self.time_queue.qsize() + self.processing.qsize() |
||
| 261 | |||
| 262 | def is_processing(self, taskid): |
||
| 263 | ''' |
||
| 264 | return True if taskid is in processing |
||
| 265 | ''' |
||
| 266 | return taskid in self.processing and self.processing[taskid].taskid |
||
| 267 | |||
| 268 | def __len__(self): |
||
| 269 | return self.size() |
||
| 270 | |||
| 271 | def __contains__(self, taskid): |
||
| 272 | if taskid in self.priority_queue or taskid in self.time_queue: |
||
| 273 | return True |
||
| 274 | if taskid in self.processing and self.processing[taskid].taskid: |
||
| 275 | return True |
||
| 276 | return False |
||
| 277 | |||
| 293 |