🔍 Overview

These Celery tasks re-check blocked Links via Google Safe Browsing and automatically unblock links that are no longer reported as unsafe.


⏱ Schedule (Celery Beat)

Configured in config/settings.py:

  • check_blocked_links_via_google_task — every CHECK_BLOCKED_LINKS_INTERVAL seconds

📝 Task Definitions

@celery.task
def check_blocked_links_via_google_task():
    domain_map = Link.get_domains_map(
        status=LinkStatuses.BLOCKED,
        reason_exclude=BlockReasons.DNS_NOT_RESOLVABLE,
    )
 
    for chunk_domains in chunked(list(domain_map.keys())):
        unsafe_domains = GoogleSafeBrowsing.check_links(chunk_domains)
        if unsafe_domains is None:
            continue
 
        for domain in chunk_domains:
            if domain not in unsafe_domains:
                for obj in domain_map[domain]:
                    unblock_link_task.delay(obj.id)
 
 
@celery.task
def unblock_link_task(link_id: int, action_by: str = "system"):
    try:
        link = Link.objects.get(id=link_id)
    except Link.DoesNotExist:
        return
 
    if link.status != LinkStatuses.BLOCKED:
        return
 
    # DNS must be OK to unblock
    if not is_domain_resolvable(link.domain):
        link.set_block(
            reason=BlockReasons.DNS_NOT_RESOLVABLE,
            with_switch=False,
            action_by=action_by,
        )
        return
 
    # bypass of incorrect unblocking by Google Safe Browsing
    if timezone.now() - link.blocked_at < timezone.timedelta(hours=1):
        return
 
    active_link = link.get_main_active_link(search_direction=SearchDirection.FORWARD)
    link.set_unblock(with_return_to_work=active_link is not None, action_by=action_by)

NameTypeDescription
link_idintPrimary key of the Link to revalidate and unblock.
action_bystrActor requesting the unblock (system by default).

⚙️ Task Behavior & Flow

  1. Blocked links revalidation

    • Collects blocked domains (excluding DNS failures).
    • Calls GoogleSafeBrowsing.check_links() in batches.
    • If a domain is not present in the unsafe list, its links are queued for unblocking.
  2. Unblocking

    • Unblocks only if the domain is resolvable.
    • Skips links blocked less than 1 hour ago.
    • If the link has an ACTIVE link in its chain, set_unblock(with_return_to_work=True) returns it back into work.