Dramatic speed improvements for build completion

- Might still need to be a background task at some point..
This commit is contained in:
Oliver Walters 2020-11-03 15:56:20 +11:00
parent 3f03adba72
commit 152801f06f
2 changed files with 24 additions and 17 deletions

View File

@ -597,11 +597,17 @@ class Build(MPTTModel):
for build_item in allocated_items:
# Complete the allocation of stock for that item
build_item.completeAllocation(user)
# TODO: This is VERY SLOW as each deletion from the database takes ~1 second to complete
# TODO: Use celery / redis to offload the actual object deletion...
# REF: https://www.botreetechnologies.com/blog/implementing-celery-using-django-for-background-task-processing
# REF: https://code.tutsplus.com/tutorials/using-celery-with-django-for-background-task-processing--cms-28732
# Remove the build item from the database
build_item.delete()
# Complete the allocation of stock for that item
build_item.complete_allocation(user)
# Delete the BuildItem objects from the database
allocated_items.all().delete()
# Ensure that the output is updated correctly
output.build = self
@ -900,7 +906,7 @@ class BuildItem(models.Model):
raise ValidationError(errors)
@transaction.atomic
def completeAllocation(self, user):
def complete_allocation(self, user):
"""
Complete the allocation of this BuildItem into the output stock item.
@ -910,21 +916,22 @@ class BuildItem(models.Model):
item = self.stock_item
# For a trackable part, special consideration needed!
if item.part.trackable:
# Split the allocated stock if there are more available than allocated
if item.quantity > self.quantity:
item = item.splitStock(self.quantity, None, user)
# Update our own reference to the new item
# Make sure we are pointing to the new item
self.stock_item = item
self.save()
if item.part.trackable:
# If the part is trackable, install into the build output
# Install the stock item into the output
item.belongs_to = self.install_into
item.save()
else:
# Part is *not* trackable, so just delete it
item.delete()
# Simply remove the items from stock
item.take_stock(self.quantity, user)
build = models.ForeignKey(
Build,

View File

@ -92,7 +92,7 @@ function makeBuildOutputActionButtons(output, buildInfo) {
data: {
output: outputId,
},
reload: true,
success: reloadTable,
}
);
});