local_periodic_tasks.py 3.04 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
import logging
from django.utils import timezone
20
from django.utils.translation import ugettext_noop
21

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(
Őry Máté committed
51 52
                    ugettext_noop('%(instance)s destroyed'),
                    ugettext_noop(
53 54 55
                        'Your instance <a href="%(url)s">%(instance)s</a> '
                        'has been destroyed due to expiration.'),
                    instance=i.name, url=i.get_absolute_url())
56 57 58
            except Exception as e:
                logger.debug('Could not notify owner of instance %d .%s',
                             i.pk, unicode(e))
Bach Dániel committed
59
        elif (i.time_of_suspend and now > i.time_of_suspend and
60
              i.state == 'RUNNING'):
61
            i.sleep.async(system=True)
62 63 64
            logger.info("Expired instance %d suspended." % i.pk)
            try:
                i.owner.profile.notify(
65
                    ugettext_noop('%(instance)s suspended'),
Őry Máté committed
66
                    ugettext_noop(
67 68 69 70
                        'Your instance <a href="%(url)s">%(instance)s</a> '
                        'has been suspended due to expiration. '
                        'You can resume or destroy it.'),
                    instance=i.name, url=i.get_absolute_url())
71 72 73
            except Exception as e:
                logger.debug('Could not notify owner of instance %d .%s',
                             i.pk, unicode(e))
74 75 76
        elif i.is_expiring():
            logger.debug("Instance %d expires soon." % i.pk)
            i.notify_owners_about_expiration()
77 78
        else:
            logger.debug("Instance %d didn't expire." % i.pk)