Update Chain Data #155
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Update Chain Data | |
| on: | |
| schedule: | |
| - cron: '*/30 * * * *' # Every 30 minutes | |
| workflow_dispatch: # Manual trigger | |
| permissions: | |
| issues: write | |
| contents: read | |
| jobs: | |
| update-data: | |
| runs-on: ubuntu-latest | |
| env: | |
| API_BASE_URL: https://backend-phi-green.vercel.app/api | |
| steps: | |
| - name: Test Connection | |
| run: | | |
| for i in {1..3}; do | |
| echo "Testing API connection (attempt $i/3)..." | |
| raw_response=$(curl -s -X GET \ | |
| -H "x-api-key: ${{ secrets.UPDATE_API_KEY }}" \ | |
| "${{ env.API_BASE_URL }}/test") | |
| echo "Raw API Response:" | |
| echo "$raw_response" | |
| # Check if response is empty | |
| if [ -z "$raw_response" ]; then | |
| echo "Error: Empty response from API" | |
| if [ $i -lt 3 ]; then | |
| echo "Retrying in 10 seconds..." | |
| sleep 10 | |
| continue | |
| fi | |
| exit 1 | |
| fi | |
| # Validate JSON and check success status | |
| if echo "$raw_response" | jq -e . >/dev/null 2>&1; then | |
| success=$(echo "$raw_response" | jq -r '.success') | |
| echo "Success status: $success" | |
| if [ "$success" = "true" ]; then | |
| echo "API test successful" | |
| break | |
| else | |
| echo "API test failed: success=false" | |
| if [ $i -lt 3 ]; then | |
| echo "Retrying in 10 seconds..." | |
| sleep 10 | |
| continue | |
| fi | |
| exit 1 | |
| fi | |
| else | |
| echo "Error: Invalid JSON response" | |
| if [ $i -lt 3 ]; then | |
| echo "Retrying in 10 seconds..." | |
| sleep 10 | |
| continue | |
| fi | |
| exit 1 | |
| fi | |
| done | |
| - name: Trigger Update | |
| if: success() | |
| run: | | |
| echo "Triggering batch update..." | |
| raw_response=$(curl -s -X POST \ | |
| -H "x-api-key: ${{ secrets.UPDATE_API_KEY }}" \ | |
| -H "Content-Type: application/json" \ | |
| "${{ env.API_BASE_URL }}/update/batch") | |
| echo "Raw Update Response:" | |
| echo "$raw_response" | |
| if [ -z "$raw_response" ]; then | |
| echo "Error: Empty response from update endpoint" | |
| exit 1 | |
| fi | |
| if echo "$raw_response" | jq -e . >/dev/null 2>&1; then | |
| if [ "$(echo "$raw_response" | jq -r '.success')" = "true" ]; then | |
| echo "Update process started successfully" | |
| else | |
| echo "Update trigger failed" | |
| exit 1 | |
| fi | |
| else | |
| echo "Error: Invalid JSON response from update endpoint" | |
| exit 1 | |
| fi | |
| echo "Waiting 45 seconds for processing..." | |
| sleep 45 | |
| - name: Verify Update | |
| if: success() | |
| run: | | |
| for i in {1..3}; do | |
| echo "Checking health status (attempt $i/3)..." | |
| raw_response=$(curl -s -X GET \ | |
| -H "x-api-key: ${{ secrets.UPDATE_API_KEY }}" \ | |
| "${{ env.API_BASE_URL }}/health") | |
| echo "Raw Health Response:" | |
| echo "$raw_response" | |
| if [ -z "$raw_response" ]; then | |
| echo "Error: Empty response from health endpoint" | |
| if [ $i -lt 3 ]; then | |
| echo "Retrying in 15 seconds..." | |
| sleep 15 | |
| continue | |
| fi | |
| exit 1 | |
| fi | |
| if echo "$raw_response" | jq -e . >/dev/null 2>&1; then | |
| success=$(echo "$raw_response" | jq -r '.success') | |
| status=$(echo "$raw_response" | jq -r '.status') | |
| dbState=$(echo "$raw_response" | jq -r '.metrics.dbState') | |
| echo "Health check results:" | |
| echo "Success: $success" | |
| echo "Status: $status" | |
| echo "DB State: $dbState" | |
| # Accept both 'ok' and 'degraded' states if success is true | |
| if [ "$success" = "true" ] && { [ "$status" = "ok" ] || [ "$status" = "degraded" ]; }; then | |
| echo "Health check passed (Status: $status)" | |
| if [ "$status" = "degraded" ]; then | |
| echo "Warning: Service is in degraded state but still functional" | |
| fi | |
| break | |
| else | |
| echo "Health check failed" | |
| echo "Error: $(echo "$raw_response" | jq -r '.error // "Unknown error"')" | |
| if [ $i -lt 3 ]; then | |
| echo "Retrying in 15 seconds..." | |
| sleep 15 | |
| continue | |
| fi | |
| exit 1 | |
| fi | |
| else | |
| echo "Error: Invalid JSON response from health endpoint" | |
| echo "Raw response: $raw_response" | |
| if [ $i -lt 3 ]; then | |
| echo "Retrying in 15 seconds..." | |
| sleep 15 | |
| continue | |
| fi | |
| curl -v -H "x-api-key: ${{ secrets.UPDATE_API_KEY }}" "${{ env.API_BASE_URL }}/health" | |
| exit 1 | |
| fi | |
| done | |
| - name: Create Issue on Failure | |
| if: failure() | |
| uses: actions/github-script@v6 | |
| with: | |
| script: | | |
| const issueBody = ` | |
| Data update job failed at ${new Date().toISOString()} | |
| Please check the GitHub Actions logs for more details: | |
| ${process.env.GITHUB_SERVER_URL}/${process.env.GITHUB_REPOSITORY}/actions/runs/${process.env.GITHUB_RUN_ID} | |
| `; | |
| await github.rest.issues.create({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| title: '🚨 Data Update Failed', | |
| body: issueBody, | |
| labels: ['bug', 'automated'] | |
| }); |