Skip to content

Subscription Expiration Script - Usage Guide

The expire-subscriptions-daily.sh script has been refactored to support both CLI and Docker execution patterns.

Execution Environments

The script automatically detects and adapts to three execution environments:

  1. CLI Execution: Running locally with uv and local environment
  2. Docker Inside: Running inside the Docker container
  3. Docker Outside: Running via docker exec to an existing container

Usage Examples

CLI Execution (Local Development)

# Basic execution with default settings (1-day grace period)
./scripts/expire-subscriptions-daily.sh

# With custom grace period
./scripts/expire-subscriptions-daily.sh --grace-days 3

# Dry run to preview changes
./scripts/expire-subscriptions-daily.sh --dry-run --verbose

# Using environment variables
GRACE_DAYS=2 VERBOSE="--verbose" ./scripts/expire-subscriptions-daily.sh

Docker Execution (Production)

# Execute via docker exec (script auto-detects container)
./scripts/expire-subscriptions-daily.sh

# Execute with specific container name
./scripts/expire-subscriptions-daily.sh --container my_katbot_api

# Execute inside container (if you're already inside)
./scripts/expire-subscriptions-daily.sh

Cron Job Setup

For CLI Environment (Host System)

# Add to crontab (runs daily at 2 AM)
0 2 * * * /path/to/project/scripts/expire-subscriptions-daily.sh

For Docker Environment

# Option 1: Run via docker exec from host
0 2 * * * /path/to/project/scripts/expire-subscriptions-daily.sh

# Option 2: Setup cron inside container using the docker wrapper
# (Add to container's crontab)
0 2 * * * /app/scripts/docker-expire-subscriptions.sh

Configuration Options

Command Line Arguments

  • -h, --help: Show help message
  • -g, --grace-days N: Set grace period in days (default: 1)
  • -v, --verbose: Enable verbose output
  • -d, --dry-run: Preview changes without making them
  • -c, --container NAME: Docker container name (default: katbot_api)

Environment Variables

  • GRACE_DAYS: Grace period in days (default: 1)
  • VERBOSE: Set to "--verbose" to enable verbose output
  • DRY_RUN: Set to "--dry-run" to enable dry run mode
  • DOCKER_CONTAINER_NAME: Docker container name (default: katbot_api)

Logging

  • CLI Mode: Logs to PROJECT_DIR/logs/expire-subscriptions.log
  • Docker Mode: Logs to container's /tmp/expire-subscriptions.log
  • Docker Exec Mode: Copies logs from container to host after execution

Environment Detection

The script automatically detects the execution environment:

  1. Docker Inside: Checks for /.dockerenv file
  2. Docker Outside: Checks if Docker is available and container exists
  3. CLI: Falls back to local execution

Files

  • scripts/expire-subscriptions-daily.sh: Main script (supports all environments)
  • scripts/docker-expire-subscriptions.sh: Docker container wrapper for cron jobs

Error Handling

The script provides appropriate error handling and exit codes: - 0: Success - Non-zero: Failure (passes through the original command's exit code)

All errors are logged with timestamps and appropriate context for debugging.