Commit a6e93b0b by Bach Dániel

vm: fix garbage collector

parent c369ff1e
...@@ -616,21 +616,23 @@ class Instance(AclBase, VirtualMachineDescModel, TimeStampedModel): ...@@ -616,21 +616,23 @@ class Instance(AclBase, VirtualMachineDescModel, TimeStampedModel):
interval what the Lease allows. This rate is configurable with the interval what the Lease allows. This rate is configurable with the
only parameter, threshold (0.1 = 10% by default). only parameter, threshold (0.1 = 10% by default).
""" """
return (self._is_suspend_expiring(self, threshold) or return (self._is_suspend_expiring(threshold) or
self._is_delete_expiring(self, threshold)) self._is_delete_expiring(threshold))
def _is_suspend_expiring(self, threshold=0.1): def _is_suspend_expiring(self, threshold=0.1):
interval = self.lease.suspend_interval interval = self.lease.suspend_interval
if interval is not None: if self.time_of_suspend is not None and interval is not None:
limit = timezone.now() + threshold * self.lease.suspend_interval limit = timezone.now() + timedelta(seconds=(
threshold * self.lease.suspend_interval.total_seconds()))
return limit > self.time_of_suspend return limit > self.time_of_suspend
else: else:
return False return False
def _is_delete_expiring(self, threshold=0.1): def _is_delete_expiring(self, threshold=0.1):
interval = self.lease.delete_interval interval = self.lease.delete_interval
if interval is not None: if self.time_of_delete is not None and interval is not None:
limit = timezone.now() + threshold * self.lease.delete_interval limit = timezone.now() + timedelta(seconds=(
threshold * self.lease.delete_interval.total_seconds()))
return limit > self.time_of_delete return limit > self.time_of_delete
else: else:
return False return False
......
...@@ -26,7 +26,7 @@ def garbage_collector(timeout=15): ...@@ -26,7 +26,7 @@ def garbage_collector(timeout=15):
""" """
now = timezone.now() now = timezone.now()
for i in Instance.objects.filter(destroyed=None).all(): for i in Instance.objects.filter(destroyed=None).all():
if i.time_of_delete and now < i.time_of_delete: if i.time_of_delete and now > i.time_of_delete:
i.destroy_async() i.destroy_async()
logger.info("Expired instance %d destroyed.", i.pk) logger.info("Expired instance %d destroyed.", i.pk)
try: try:
...@@ -37,7 +37,7 @@ def garbage_collector(timeout=15): ...@@ -37,7 +37,7 @@ def garbage_collector(timeout=15):
except Exception as e: except Exception as e:
logger.debug('Could not notify owner of instance %d .%s', logger.debug('Could not notify owner of instance %d .%s',
i.pk, unicode(e)) i.pk, unicode(e))
elif (i.time_of_suspend and now < i.time_of_suspend and elif (i.time_of_suspend and now > i.time_of_suspend and
i.state == 'RUNNING'): i.state == 'RUNNING'):
i.sleep_async() i.sleep_async()
logger.info("Expired instance %d suspended." % i.pk) logger.info("Expired instance %d suspended." % i.pk)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment