| Total Complexity | 35 |
| Total Lines | 119 |
| Duplicated Lines | 15.97 % |
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:
| 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.burst.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 size(self): |
||
| 223 | return self.priority_queue.qsize() + self.time_queue.qsize() + self.processing.qsize() |
||
| 224 | |||
| 225 | def __len__(self): |
||
| 226 | return self.size() |
||
| 227 | |||
| 228 | def __contains__(self, taskid): |
||
| 229 | if taskid in self.priority_queue or taskid in self.time_queue: |
||
| 230 | return True |
||
| 231 | if taskid in self.processing and self.processing[taskid].taskid: |
||
| 232 | return True |
||
| 233 | return False |
||
| 234 | |||
| 250 |