예제 #1
0
class Prefetcher(object):

    def __init__(self, conf, **local_conf):
        self.conf = conf
        tank.store.create_stores(conf)
        self.cache = ImageCache(conf)
        registry.configure_registry_client(conf)

    def fetch_image_into_cache(self, image_id):
        ctx = registry.get_client_context(self.conf,
                                          is_admin=True, show_deleted=True)
        try:
            image_meta = registry.get_image_metadata(ctx, image_id)
            if image_meta['status'] != 'active':
                logger.warn(_("Image '%s' is not active. Not caching."),
                            image_id)
                return False

        except exception.NotFound:
            logger.warn(_("No metadata found for image '%s'"), image_id)
            return False

        image_data, image_size = get_from_backend(image_meta['location'])
        logger.debug(_("Caching image '%s'"), image_id)
        self.cache.cache_image_iter(image_id, image_data)
        return True

    def run(self):

        images = self.cache.get_queued_images()
        if not images:
            logger.debug(_("Nothing to prefetch."))
            return True

        num_images = len(images)
        logger.debug(_("Found %d images to prefetch"), num_images)

        pool = eventlet.GreenPool(num_images)
        results = pool.imap(self.fetch_image_into_cache, images)
        successes = sum([1 for r in results if r is True])
        if successes != num_images:
            logger.error(_("Failed to successfully cache all "
                           "images in queue."))
            return False

        logger.info(_("Successfully cached all %d images"), num_images)
        return True
예제 #2
0
class Prefetcher(object):
    def __init__(self, conf, **local_conf):
        self.conf = conf
        tank.store.create_stores(conf)
        self.cache = ImageCache(conf)
        registry.configure_registry_client(conf)

    def fetch_image_into_cache(self, image_id):
        ctx = registry.get_client_context(self.conf, is_admin=True, show_deleted=True)
        try:
            image_meta = registry.get_image_metadata(ctx, image_id)
            if image_meta["status"] != "active":
                logger.warn(_("Image '%s' is not active. Not caching."), image_id)
                return False

        except exception.NotFound:
            logger.warn(_("No metadata found for image '%s'"), image_id)
            return False

        image_data, image_size = get_from_backend(image_meta["location"])
        logger.debug(_("Caching image '%s'"), image_id)
        self.cache.cache_image_iter(image_id, image_data)
        return True

    def run(self):

        images = self.cache.get_queued_images()
        if not images:
            logger.debug(_("Nothing to prefetch."))
            return True

        num_images = len(images)
        logger.debug(_("Found %d images to prefetch"), num_images)

        pool = eventlet.GreenPool(num_images)
        results = pool.imap(self.fetch_image_into_cache, images)
        successes = sum([1 for r in results if r is True])
        if successes != num_images:
            logger.error(_("Failed to successfully cache all " "images in queue."))
            return False

        logger.info(_("Successfully cached all %d images"), num_images)
        return True