Skip to content

Celery Support

django_slack_tools.slack_messages.tasks

Celery utils.

cleanup_old_messages(*, base_ts=None, threshold_seconds=7 * 24 * 60 * 60)

Delete old messages created before given threshold.

Parameters:

Name Type Description Default
threshold_seconds int | None

Threshold seconds. Defaults to 7 days.

7 * 24 * 60 * 60
base_ts str | None

Base timestamp to calculate the threshold, in ISO format. If falsy, current timestamp will be used.

None

Returns:

Type Description
int

Number of deleted messages.

Source code in django_slack_tools/slack_messages/tasks.py
@shared_task
def cleanup_old_messages(
    *,
    base_ts: str | None = None,
    threshold_seconds: int | None = 7 * 24 * 60 * 60,  # 7 days
) -> int:
    """Delete old messages created before given threshold.

    Args:
        threshold_seconds: Threshold seconds. Defaults to 7 days.
        base_ts: Base timestamp to calculate the threshold, in ISO format. If falsy, current timestamp will be used.

    Returns:
        Number of deleted messages.
    """
    dt = datetime.fromisoformat(base_ts) if base_ts else timezone.localtime()

    if threshold_seconds is None:
        logger.warning("Threshold seconds not provided, skipping cleanup.")
        return 0

    cleanup_threshold = dt - timedelta(seconds=threshold_seconds)
    logger.debug("Cleaning up messages older than %s.", cleanup_threshold)

    num_deleted, _ = SlackMessage.objects.filter(created__lt=cleanup_threshold).delete()
    logger.info("Deleted %d old messages.", num_deleted)

    return num_deleted

slack_message(*args, **kwargs)

Celery task wrapper for .shortcuts.slack_message.

Parameters:

Name Type Description Default
args Any

Positional arguments.

()
kwargs Any

Keyword arguments.

{}

Returns:

Type Description
str | None

ID of sent message if any, None otherwise.

Source code in django_slack_tools/slack_messages/tasks.py
@shared_task
def slack_message(*args: Any, **kwargs: Any) -> str | None:
    """Celery task wrapper for `.shortcuts.slack_message`.

    Args:
        args: Positional arguments.
        kwargs: Keyword arguments.

    Returns:
        ID of sent message if any, `None` otherwise.
    """
    response = shortcuts.slack_message(*args, **kwargs)
    return response.ts if response else None