| 291 | |
| 292 | |
| 293 | class Task(UUIDModel, VersionedModel): |
| 294 | type = models.CharField(max_length=128, db_index=True) |
| 295 | subject_id = models.UUIDField(db_index=True) |
| 296 | status = models.CharField(max_length=1, choices=TASK_STATUS_CHOICES, default='P', db_index=True) |
| 297 | started_at = models.DateTimeField(null=True) |
| 298 | finished_at = models.DateTimeField(null=True) |
| 299 | parent = models.ForeignKey('self', related_name='children', null=True, on_delete=models.CASCADE) |
| 300 | complete_with_children = models.BooleanField(default=False) |
| 301 | library = models.ForeignKey(Library, related_name='task_library', on_delete=models.CASCADE, null=True, blank=True) |
| 302 | |
| 303 | class Meta: |
| 304 | ordering = ['created_at'] |
| 305 | |
| 306 | def __str__(self): |
| 307 | return '{}: {}'.format(self.type, self.created_at) |
| 308 | |
| 309 | def start(self): |
| 310 | self.status = 'S' |
| 311 | self.started_at = timezone.now() |
| 312 | self.save() |
| 313 | |
| 314 | def complete(self, next_type=None, next_subject_id=None): |
| 315 | # Set status of current task and queue up next task if appropriate |
| 316 | self.status = 'C' |
| 317 | self.finished_at = timezone.now() |
| 318 | self.save() |
| 319 | |
| 320 | # Create next task in the chain if there should be one |
| 321 | if not self.parent and next_type: |
| 322 | Task(type=next_type, subject_id=next_subject_id, library=self.library).save() |
| 323 | |
| 324 | if self.parent and self.parent.complete_with_children: |
| 325 | # If all siblings are complete, we should mark our parent as complete |
| 326 | with transaction.atomic(): |
| 327 | # select_for_update() will block if another process is working with these children |
| 328 | siblings = self.parent.children.select_for_update().filter(status='C') |
| 329 | if siblings.count() == self.parent.children.count(): |
| 330 | self.parent.complete( |
| 331 | next_type=next_type, next_subject_id=next_subject_id) |
| 332 | |
| 333 | def failed(self, error=None, traceback=None): |
| 334 | self.status = 'F' |
| 335 | self.finished_at = timezone.now() |
| 336 | self.save() |
| 337 | |
| 338 | if error: |
| 339 | logger.error(error) |
no outgoing calls
no test coverage detected