« Back to Index

xarg: parallel processing

View original Gist on GitHub

Tags: #xarg

1. xarg parallel processing.sh

# update --cursor value to acquire all the relevant domain data (see batch-identify-domains.sh)
fastly domain-v1 list --fqdn=test-tf --cursor=<REDACTED> --limit=100 --json | jq -r .data[].id >> /tmp/delete-domains

# now delete all those items
cat /tmp/delete-domains | xargs -P "$(sysctl -n hw.ncpu)" -I % fastly domain-v1 delete --domain-id=%

2. batch-identify-domains.sh

#!/bin/bash

# Initial cursor value
cursor="<STARTING CURSOR>"
output_file="/tmp/delete-domains"

# Clear the output file before appending data
> "$output_file"

while true; do
  # Run the command and capture the output
  response=$(go run ./cmd/fastly/main.go domain-v1 list --fqdn=tf-test --limit=100 --json --cursor="$cursor")

  # Extract IDs and append to the output file
  echo "$response" | jq -r .data[].id >> "$output_file"

  # Extract the next cursor value from the JSON response
  next_cursor=$(echo "$response" | jq -r .meta.next_cursor)

  # Check if there is no next cursor (end of pagination)
  if [[ -z "$next_cursor" || "$next_cursor" == "null" ]]; then
    echo "No more data to fetch. Exiting loop."
    break
  fi

  # Update the cursor for the next iteration
  cursor="$next_cursor"

  echo "Fetched batch. Next cursor: $cursor"
done

echo "All data fetched and saved to $output_file."