Example #1
0
class Prefetcher(object):
    def __init__(self, options):
        self.options = options
        glance.store.create_stores(options)
        self.cache = ImageCache(options)

    def fetch_image_into_cache(self, image_id):
        ctx = context.RequestContext(is_admin=True, show_deleted=True)
        image_meta = registry.get_image_metadata(
                    self.options, ctx, image_id)
        with self.cache.open(image_meta, "wb") as cache_file:
            chunks = get_from_backend(image_meta['location'],
                                      expected_size=image_meta['size'],
                                      options=self.options)
            for chunk in chunks:
                cache_file.write(chunk)

    def run(self):
        if self.cache.is_currently_prefetching_any_images():
            logger.debug(_("Currently prefetching, going back to sleep..."))
            return

        try:
            image_id = self.cache.pop_prefetch_item()
        except IndexError:
            logger.debug(_("Nothing to prefetch, going back to sleep..."))
            return

        if self.cache.hit(image_id):
            logger.warn(_("Image %s is already in the cache, deleting "
                        "prefetch job and going back to sleep..."), image_id)
            self.cache.delete_queued_prefetch_image(image_id)
            return

        # NOTE(sirp): if someone is already downloading an image that is in
        # the prefetch queue, then go ahead and delete that item and try to
        # prefetch another
        if self.cache.is_image_currently_being_written(image_id):
            logger.warn(_("Image %s is already being cached, deleting "
                        "prefetch job and going back to sleep..."), image_id)
            self.cache.delete_queued_prefetch_image(image_id)
            return

        logger.debug(_("Prefetching '%s'"), image_id)
        self.cache.do_prefetch(image_id)

        try:
            self.fetch_image_into_cache(image_id)
        finally:
            self.cache.delete_prefetching_image(image_id)