Commit 2b6c6fe1 by Czémán Arnold

storagedriver: Rework garbage collector logic

* remove move_to_trash()
* remove recover_from_trash()
* add exits() which say an image exists
* rework make_free_space()
parent 7b358f8c
from disk import Disk from disk import Disk
from storagecelery import celery from storagecelery import celery
from os import path, unlink, statvfs, listdir, mkdir import os
from shutil import move from os import path, unlink, statvfs, listdir
from celery.contrib.abortable import AbortableTask from celery.contrib.abortable import AbortableTask
import logging import logging
...@@ -89,52 +89,24 @@ def get_storage_stat(path): ...@@ -89,52 +89,24 @@ def get_storage_stat(path):
@celery.task @celery.task
def move_to_trash(datastore, disk_name): def exists(path, disk_name):
''' Move path to the trash directory. return os.path.exists(os.path.join(path, disk_name))
'''
trash_path = path.join(datastore, trash_directory)
disk_path = path.join(datastore, disk_name)
if not path.isdir(trash_path):
mkdir(trash_path)
# TODO: trash dir configurable?
move(disk_path, trash_path)
@celery.task @celery.task
def recover_from_trash(datastore, disk_name): def make_free_space(path, deletable_disks, percent=10):
''' Recover named disk from the trash directory.
'''
if path.exists(path.join(datastore, disk_name)):
return False
disk_path = path.join(datastore, trash_directory, disk_name)
# TODO: trash dir configurable?
move(disk_path, datastore)
return True
@celery.task
def make_free_space(datastore, percent=10):
''' Check for free space on datastore. ''' Check for free space on datastore.
If free space is less than the given percent If free space is less than the given percent
removes oldest files to satisfy the given requirement. removes oldest files to satisfy the given requirement.
''' '''
trash_path = path.join(datastore, trash_directory)
def comp(filename):
try:
return path.getctime(path.join(trash_path, filename))
except OSError:
return 0
files = sorted(listdir(trash_path), key=comp)
logger.info("Free space on datastore: %s" % logger.info("Free space on datastore: %s" %
get_storage_stat(trash_path).get('free_percent')) get_storage_stat(path).get('free_percent'))
while get_storage_stat(trash_path).get('free_percent') < percent: while get_storage_stat(path).get('free_percent') < percent:
logger.debug(get_storage_stat(trash_path)) logger.debug(get_storage_stat(path))
try: try:
f = files.pop(0) f = deletable_disks.pop(0)
unlink(path.join(trash_path, f)) unlink(os.path.join(path, f))
logger.info('Image: %s removed.' % f) logger.info('Image: %s removed.' % f)
except IndexError: except IndexError:
raise Exception("Trash folder is empty.") raise Exception("There is not deletable disk.")
return True return True
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