| Total Complexity | 41 |
| Total Lines | 140 |
| Duplicated Lines | 13.57 % |
| Changes | 3 | ||
| Bugs | 2 | Features | 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 |
||
| 115 | class TaskQueue(object): |
||
| 116 | |||
| 117 | ''' |
||
| 118 | task queue for scheduler, have a priority queue and a time queue for delayed tasks |
||
| 119 | ''' |
||
| 120 | processing_timeout = 10 * 60 |
||
| 121 | |||
| 122 | def __init__(self, rate=0, burst=0): |
||
| 123 | self.mutex = threading.RLock() |
||
| 124 | self.priority_queue = PriorityTaskQueue() |
||
| 125 | self.time_queue = PriorityTaskQueue() |
||
| 126 | self.processing = PriorityTaskQueue() |
||
| 127 | self.bucket = Bucket(rate=rate, burst=burst) |
||
| 128 | |||
| 129 | @property |
||
| 130 | def rate(self): |
||
| 131 | return self.bucket.rate |
||
| 132 | |||
| 133 | @rate.setter |
||
| 134 | def rate(self, value): |
||
| 135 | self.bucket.rate = value |
||
| 136 | |||
| 137 | @property |
||
| 138 | def burst(self): |
||
| 139 | return self.bucket.burst |
||
| 140 | |||
| 141 | @burst.setter |
||
| 142 | def burst(self, value): |
||
| 143 | self.bucket.burst = value |
||
| 144 | |||
| 145 | def check_update(self): |
||
| 146 | ''' |
||
| 147 | Check time queue and processing queue |
||
| 148 | |||
| 149 | put tasks to priority queue when execute time arrived or process timeout |
||
| 150 | ''' |
||
| 151 | self._check_time_queue() |
||
| 152 | self._check_processing() |
||
| 153 | |||
| 154 | View Code Duplication | def _check_time_queue(self): |
|
|
|
|||
| 155 | now = time.time() |
||
| 156 | self.mutex.acquire() |
||
| 157 | while self.time_queue.qsize() and self.time_queue.top and self.time_queue.top.exetime < now: |
||
| 158 | task = self.time_queue.get_nowait() |
||
| 159 | task.exetime = 0 |
||
| 160 | self.priority_queue.put(task) |
||
| 161 | self.mutex.release() |
||
| 162 | |||
| 163 | View Code Duplication | def _check_processing(self): |
|
| 164 | now = time.time() |
||
| 165 | self.mutex.acquire() |
||
| 166 | while self.processing.qsize() and self.processing.top and self.processing.top.exetime < now: |
||
| 167 | task = self.processing.get_nowait() |
||
| 168 | if task.taskid is None: |
||
| 169 | continue |
||
| 170 | task.exetime = 0 |
||
| 171 | self.priority_queue.put(task) |
||
| 172 | logger.info("processing: retry %s", task.taskid) |
||
| 173 | self.mutex.release() |
||
| 174 | |||
| 175 | def put(self, taskid, priority=0, exetime=0): |
||
| 176 | '''Put a task into task queue''' |
||
| 177 | now = time.time() |
||
| 178 | task = InQueueTask(taskid, priority, exetime) |
||
| 179 | self.mutex.acquire() |
||
| 180 | if taskid in self.priority_queue: |
||
| 181 | self.priority_queue.put(task) |
||
| 182 | elif taskid in self.time_queue: |
||
| 183 | self.time_queue.put(task) |
||
| 184 | elif taskid in self.processing and self.processing[taskid].taskid: |
||
| 185 | # force update a processing task is not allowed as there are so many |
||
| 186 | # problems may happen |
||
| 187 | pass |
||
| 188 | else: |
||
| 189 | if exetime and exetime > now: |
||
| 190 | self.time_queue.put(task) |
||
| 191 | else: |
||
| 192 | self.priority_queue.put(task) |
||
| 193 | self.mutex.release() |
||
| 194 | |||
| 195 | def get(self): |
||
| 196 | '''Get a task from queue when bucket available''' |
||
| 197 | if self.bucket.get() < 1: |
||
| 198 | return None |
||
| 199 | now = time.time() |
||
| 200 | self.mutex.acquire() |
||
| 201 | try: |
||
| 202 | task = self.priority_queue.get_nowait() |
||
| 203 | self.bucket.desc() |
||
| 204 | except Queue.Empty: |
||
| 205 | self.mutex.release() |
||
| 206 | return None |
||
| 207 | task.exetime = now + self.processing_timeout |
||
| 208 | self.processing.put(task) |
||
| 209 | self.mutex.release() |
||
| 210 | return task.taskid |
||
| 211 | |||
| 212 | def done(self, taskid): |
||
| 213 | '''Mark task done''' |
||
| 214 | if taskid in self.processing: |
||
| 215 | self.mutex.acquire() |
||
| 216 | if taskid in self.processing: |
||
| 217 | del self.processing[taskid] |
||
| 218 | self.mutex.release() |
||
| 219 | return True |
||
| 220 | return False |
||
| 221 | |||
| 222 | def delete(self, taskid): |
||
| 223 | if taskid not in self: |
||
| 224 | return False |
||
| 225 | if taskid in self.priority_queue: |
||
| 226 | self.mutex.acquire() |
||
| 227 | del self.priority_queue[taskid] |
||
| 228 | self.mutex.release() |
||
| 229 | elif taskid in self.time_queue: |
||
| 230 | self.mutex.acquire() |
||
| 231 | del self.time_queue[taskid] |
||
| 232 | self.mutex.release() |
||
| 233 | elif taskid in self.processing: |
||
| 234 | self.done(taskid) |
||
| 235 | return True |
||
| 236 | |||
| 237 | def size(self): |
||
| 238 | return self.priority_queue.qsize() + self.time_queue.qsize() + self.processing.qsize() |
||
| 239 | |||
| 240 | def is_processing(self, taskid): |
||
| 241 | ''' |
||
| 242 | return True if taskid is in processing |
||
| 243 | ''' |
||
| 244 | return taskid in self.processing and self.processing[taskid].taskid |
||
| 245 | |||
| 246 | def __len__(self): |
||
| 247 | return self.size() |
||
| 248 | |||
| 249 | def __contains__(self, taskid): |
||
| 250 | if taskid in self.priority_queue or taskid in self.time_queue: |
||
| 251 | return True |
||
| 252 | if taskid in self.processing and self.processing[taskid].taskid: |
||
| 253 | return True |
||
| 254 | return False |
||
| 255 | |||
| 271 |