local_periodic_tasks.py 2.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
# Copyright 2014 Budapest University of Technology and Economics (BME IK)
#
# This file is part of CIRCLE Cloud.
#
# CIRCLE is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# CIRCLE is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along
# with CIRCLE.  If not, see <http://www.gnu.org/licenses/>.

18 19 20 21
import logging
from django.utils import timezone
from django.utils.translation import ugettext_lazy as _

22
from manager.mancelery import celery
23 24 25
from vm.models import Node, Instance

logger = logging.getLogger(__name__)
26 27


28
@celery.task(ignore_result=True)
29 30 31 32
def update_domain_states():
    nodes = Node.objects.filter(enabled=True).all()
    for node in nodes:
        node.update_vm_states()
33 34 35 36 37 38 39 40 41 42 43 44


@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()
45
    for i in Instance.objects.filter(destroyed_at=None).all():
Bach Dániel committed
46
        if i.time_of_delete and now > i.time_of_delete:
47
            i.destroy.async(system=True)
48 49 50
            logger.info("Expired instance %d destroyed.", i.pk)
            try:
                i.owner.profile.notify(
51
                    _('%s destroyed') % unicode(i),
52 53
                    'dashboard/notifications/vm-destroyed.html',
                    {'instance': i})
54 55 56
            except Exception as e:
                logger.debug('Could not notify owner of instance %d .%s',
                             i.pk, unicode(e))
Bach Dániel committed
57
        elif (i.time_of_suspend and now > i.time_of_suspend and
58
              i.state == 'RUNNING'):
59
            i.sleep.async(system=True)
60 61 62
            logger.info("Expired instance %d suspended." % i.pk)
            try:
                i.owner.profile.notify(
63
                    _('%s suspended') % unicode(i),
64 65
                    'dashboard/notifications/vm-suspended.html',
                    {'instance': i})
66 67 68
            except Exception as e:
                logger.debug('Could not notify owner of instance %d .%s',
                             i.pk, unicode(e))
69 70 71
        elif i.is_expiring():
            logger.debug("Instance %d expires soon." % i.pk)
            i.notify_owners_about_expiration()
72 73
        else:
            logger.debug("Instance %d didn't expire." % i.pk)