periodic_tasks.py 2.55 KB
Newer Older
1 2 3
from storage.models import DataStore
from manager.mancelery import celery
import logging
4
from storage.tasks import remote_tasks
5 6 7 8 9

logger = logging.getLogger(__name__)


@celery.task
10 11
def garbage_collector(timeout=15):
    """ Garbage collector for disk images.
Őry Máté committed
12

13 14
    If there is not enough free space on datastore (default 10%)
    deletes oldest images from trash.
15

16
    :param timeout: Seconds before TimeOut exception
Guba Sándor committed
17
    :type timeout: int
18
    """
19
    for ds in DataStore.objects.all():
20
        queue_name = ds.get_remote_queue_name('storage')
Guba Sándor committed
21 22 23 24 25
        files = set(remote_tasks.list_files.apply_async(
            args=[ds.path], queue=queue_name).get(timeout=timeout))
        disks = set(ds.get_deletable_disks())
        queue_name = ds.get_remote_queue_name('storage')
        for i in disks & files:
26 27 28 29 30 31 32 33 34 35
            logger.info("Image: %s at Datastore: %s moved to trash folder." %
                        (i, ds.path))
            remote_tasks.move_to_trash.apply_async(
                args=[ds.path, i], queue=queue_name).get(timeout=timeout)
        try:
            remote_tasks.make_free_space.apply_async(
                args=[ds.path], queue=queue_name).get(timeout=timeout)
        except Exception as e:
            logger.warning(str(e))

36

37 38
@celery.task
def list_orphan_disks(timeout=15):
39
    """List disk image files without Disk object in the database.
Őry Máté committed
40

41 42 43 44
    Exclude cloud-xxxxxxxx.dump format images.

    :param timeout: Seconds before TimeOut exception
    :type timeoit: int
45 46
    """
    import re
47
    for ds in DataStore.objects.all():
48
        queue_name = ds.get_remote_queue_name('storage')
Őry Máté committed
49 50 51 52
        files = set(remote_tasks.list_files.apply_async(
            args=[ds.path], queue=queue_name).get(timeout=timeout))
        disks = set([disk.filename for disk in ds.disk_set.all()])
        for i in files - disks:
53 54
            if not re.match('cloud-[0-9]*\.dump', i):
                logging.warning("Orphan disk: %s" % i)
55

56 57 58

@celery.task
def list_missing_disks(timeout=15):
59 60 61 62
    """List Disk objects without disk image files.

    :param timeout: Seconds before TimeOut exception
    :type timeoit: int
63
    """
64
    for ds in DataStore.objects.all():
65
        queue_name = ds.get_remote_queue_name('storage')
Őry Máté committed
66 67 68 69 70
        files = set(remote_tasks.list_files.apply_async(
            args=[ds.path], queue=queue_name).get(timeout=timeout))
        disks = set([disk.filename for disk in
                     ds.disk_set.filter(destroyed__isnull=True)])
        for i in disks - files:
71 72
            logging.critical("Image: %s is missing from %s datastore."
                             % (i, ds.path))