Загрузка данных


#!/bin/bash

folder=$(pwd)
address="your_address_here"
port="your_port_here"
xepacrt="Hact3"
MAX_RETRIES=3
MAX_THREADS=10

# Функция обработки одной строки
process_entry() {
    local ucn="$1"
    local attempt=0
    local success=false

    while [ "$attempt" -lt "$MAX_RETRIES" ] && [ "$success" = false ]; do
        attempt=$((attempt + 1))

        # Выполняем запрос, разделяем body и http_code
        http_response=$(curl -s -X POST --tlsv1.2 \
            --write-out "\n%{http_code}" \
            --location "https://${address}:${port}/get-entry" \
            --header 'Content-Type: application/json' \
            --header 'X-Correlation-Id: 01172777' \
            --header "X-EPA-CRT: ${xepacrt}" \
            --data "{
                \"ucn\": \"${ucn}\",
                \"domain\": \"mas\",
                \"system\": \"91\"
            }" 2>&1)

        curl_exit=$?

        # Разделяем ответ: последняя строка — http_code, остальное — body
        http_code=$(echo "$http_response" | tail -n1)
        body=$(echo "$http_response" | sed '$d')

        # Успех: curl отработал без ошибок И статус 2xx
        if [ "$curl_exit" -eq 0 ] && [[ "$http_code" =~ ^2[0-9]{2}$ ]]; then
            success=true
            echo "UID $ucn OK (HTTP $http_code, attempt $attempt)"
        else
            echo "UID $ucn FAIL (curl_exit=$curl_exit, HTTP=$http_code, attempt $attempt/$MAX_RETRIES)"
            # Экспоненциальная задержка перед повтором
            sleep $((attempt))
        fi
    done

    if [ "$success" = false ]; then
        echo "UID $ucn FAILED after $MAX_RETRIES attempts"
    fi
}

export -f process_entry
export address port xepacrt MAX_RETRIES

# Инициализация result.txt
> "$folder/result.txt"
total=$(wc -l < "$folder/numbers.db")
echo "Total rows - $total" >> "$folder/result.txt"
echo "Start script..." >> "$folder/result.txt"
start_time=$(date +%s)

# Параллельный запуск через xargs
cat "$folder/numbers.db" | xargs -I {} -P "$MAX_THREADS" -n 1 \
    bash -c 'process_entry "$@"' _ {} >> "$folder/result.txt" 2>&1

end_time=$(date +%s)
elapsed=$((end_time - start_time))

echo "...Finish (elapsed: ${elapsed}s)" >> "$folder/result.txt"