Commit 1a4ca401 by Őry Máté

vm: implement vm expiration

parent f6687581
{%load i18n%}
{%blocktrans with instance=instance.name url=instance.get_absolute_url %}
Your instance <a href="{{url}}">{{instance}}</a> has been destroyed due to expiration.
{%endblocktrans%}
{%load i18n%}
{%blocktrans with instance=instance.name url=instance.get_absolute_url %}
Your instance <a href="{{url}}">{{instance}}</a> has been suspended due to expiration.
{%endblocktrans%}
...@@ -28,11 +28,16 @@ celery.conf.update( ...@@ -28,11 +28,16 @@ celery.conf.update(
'schedule': timedelta(seconds=5), 'schedule': timedelta(seconds=5),
'options': {'queue': 'localhost.man'} 'options': {'queue': 'localhost.man'}
}, },
'vm.periodic_tasks': { 'vm.update_domain_states': {
'task': 'vm.tasks.local_periodic_tasks.update_domain_states', 'task': 'vm.tasks.local_periodic_tasks.update_domain_states',
'schedule': timedelta(seconds=10), 'schedule': timedelta(seconds=10),
'options': {'queue': 'localhost.man'} 'options': {'queue': 'localhost.man'}
}, },
'vm.garbage_collector': {
'task': 'vm.tasks.local_periodic_tasks.garbage_collector',
'schedule': timedelta(minutes=10),
'options': {'queue': 'localhost.man'}
},
'storage.periodic_tasks': { 'storage.periodic_tasks': {
'task': 'storage.tasks.periodic_tasks.garbage_collector', 'task': 'storage.tasks.periodic_tasks.garbage_collector',
'schedule': timedelta(hours=1), 'schedule': timedelta(hours=1),
......
from datetime import datetime
import logging
from django.utils import timezone
from django.utils.translation import ugettext_lazy as _
from manager.mancelery import celery from manager.mancelery import celery
from vm.models import Node from vm.models import Node, Instance
logger = logging.getLogger(__name__)
@celery.task(ignore_result=True) @celery.task(ignore_result=True)
...@@ -7,3 +14,39 @@ def update_domain_states(): ...@@ -7,3 +14,39 @@ def update_domain_states():
nodes = Node.objects.filter(enabled=True).all() nodes = Node.objects.filter(enabled=True).all()
for node in nodes: for node in nodes:
node.update_vm_states() node.update_vm_states()
@celery.task(ignore_result=True)
def garbage_collector(timeout=15):
"""Garbage collector for instances.
Suspends and destroys expired instances.
:param timeout: Seconds before TimeOut exception
:type timeout: int
"""
now = timezone.now()
for i in Instance.objects.filter(destroyed=None).all():
if i.time_of_delete and now < i.time_of_delete:
i.destroy_async()
logger.info("Expired instance %d destroyed.", i.pk)
try:
i.owner.profile.notify(
_('Machine destroyed'),
'dashboard/notifications/vm-destroyed.html',
{'instance': i})
except:
logger.debug('Could not notify owner of instance %d.', i.pk)
elif (i.time_of_suspend and now < i.time_of_suspend and
i.state == 'RUNNING'):
i.sleep_async()
logger.info("Expired instance %d suspended." % i.pk)
try:
i.owner.profile.notify(
_('Machine suspended'),
'dashboard/notifications/vm-suspended.html',
{'instance': i})
except:
logger.debug('Could not notify owner of instance %d.', i.pk)
else:
logger.debug("Instance %d didn't expire." % 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