Ejemplo n.º 1
0
def purge_static_cache(containers: list[str] = None):
    if (settings.AZURE_STATIC_CDN_ENDPOINT_NAME is None
            or len(settings.AZURE_STATIC_CDN_ENDPOINT_NAME) == 0):
        logger.warning("Azure settings not configured")
        return

    if containers is None:
        containers = STATIC_CONTAINERS

    credentials = ClientSecretCredential(
        tenant_id=settings.AZURE_TENANT_ID,
        client_id=settings.AZURE_CLIENT_ID,
        client_secret=settings.AZURE_CLIENT_SECRET,
    )

    client = CdnManagementClient(credentials, settings.AZURE_SUBSCRIPTION_ID)

    paths_group: list[str] = []
    for group in containers:
        paths_group.append(f"/{settings.AZURE_CONTAINER_PREFIX}{group}/*")
    logger.info(
        "Purging paths: %s %s %s %s",
        settings.AZURE_STATIC_CDN_RESOURCE_GROUP,
        settings.AZURE_STATIC_CDN_PROFILE_NAME,
        settings.AZURE_STATIC_CDN_ENDPOINT_NAME,
        paths_group,
    )

    poller = client.endpoints.begin_purge_content(
        settings.AZURE_STATIC_CDN_RESOURCE_GROUP,
        settings.AZURE_STATIC_CDN_PROFILE_NAME,
        settings.AZURE_STATIC_CDN_ENDPOINT_NAME,
        PurgeParameters(content_paths=paths_group),
    )
    poller.result()
Ejemplo n.º 2
0
def purge_endpoint_content(client,
                           resource_group_name,
                           profile_name,
                           endpoint_name,
                           content_paths,
                           no_wait=None):
    purge_paramters = PurgeParameters(content_paths=content_paths)

    return sdk_no_wait(no_wait, client.endpoints.begin_purge_content,
                       resource_group_name, profile_name, endpoint_name,
                       purge_paramters)
Ejemplo n.º 3
0
    def refresh_cache(self, path: Path) -> None:
        path_str = str(Path("/", path))

        self._log_info("Refreshing CDN cache.", path=path_str)

        poller: LROPoller = self.__cdn.endpoints.begin_purge_content(
            resource_group_name=self.__resource_group_name,
            profile_name=self.__profile_name,
            endpoint_name=self.__endpoint_name,
            content_file_paths=PurgeParameters(content_paths=[path_str]),
        )
        poller.wait()

        status = poller.status()
        if not status == "Succeeded":
            raise Exception(
                "Failed to refresh CDN cache. status: {}".format(status))

        self._log_info("Successfully refreshed CDN cache.", path=path_str)
Ejemplo n.º 4
0
def purge_cache(all_paths=False):
    # run after Azure has had a change to purge
    _reschedule(
        timezone.now() + timedelta(minutes=5),
        PURGE_DJ_CACHE,
        "Purge Django Cached Views",
    )

    if all_paths:
        paths = PURGE_GROUPS["__all__"]
    else:
        if not settings.AZURE_CDN_DYNAMIC_PURGE:
            logger.info("Dynamic purging disabled")
            return

        with cache.lock(PURGE_CACHE_LOCK, expire=10, auto_renewal=False):
            paths = cache.get(PURGE_CACHE_PATHS)

            if paths is not None:
                cache.delete(PURGE_CACHE_PATHS)

        if paths is None or len(paths) == 0:
            logger.warning("No paths to purge")
            return

        paths = list(paths)

    if (settings.AZURE_CDN_ENDPOINT_NAME is None
            or len(settings.AZURE_CDN_ENDPOINT_NAME) == 0):
        logger.warning("Azure settings not configured")
        return

    try:
        credentials = ClientSecretCredential(
            tenant_id=settings.AZURE_TENANT_ID,
            client_id=settings.AZURE_CLIENT_ID,
            client_secret=settings.AZURE_CLIENT_SECRET,
        )

        client = CdnManagementClient(credentials,
                                     settings.AZURE_SUBSCRIPTION_ID)

        for paths_group in _path_chunks(paths, MAX_SINGLE_PURGE):
            logger.info(
                "Purging paths: %s %s %s %s",
                settings.AZURE_CDN_RESOURCE_GROUP,
                settings.AZURE_CDN_PROFILE_NAME,
                settings.AZURE_CDN_ENDPOINT_NAME,
                paths_group,
            )

            poller = client.endpoints.begin_purge_content(
                settings.AZURE_CDN_RESOURCE_GROUP,
                settings.AZURE_CDN_PROFILE_NAME,
                settings.AZURE_CDN_ENDPOINT_NAME,
                PurgeParameters(content_paths=paths_group),
            )
            poller.result()
    except (Exception, socket.gaierror) as ex:  # pylint: disable=broad-except
        logger.info("Rescheduling paths: %s", paths)
        queue_purge_paths(paths)

        if "No address associated with hostname" not in str(ex):
            raise