|
| 1 | +package backfill |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "net/http" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/rs/zerolog/log" |
| 11 | + config "github.com/thirdweb-dev/indexer/configs" |
| 12 | + "github.com/thirdweb-dev/indexer/internal/libs" |
| 13 | +) |
| 14 | + |
| 15 | +type DeployS3CommitterRequest struct { |
| 16 | + ZeetDeploymentId string `json:"zeetDeploymentId"` |
| 17 | +} |
| 18 | + |
| 19 | +func DisableIndexerMaybeStartCommitter() { |
| 20 | + serviceURL := config.Cfg.InsightServiceUrl |
| 21 | + apiKey := config.Cfg.InsightServiceApiKey |
| 22 | + zeetDeploymentId := config.Cfg.ZeetDeploymentId |
| 23 | + |
| 24 | + // Prepare request payload |
| 25 | + requestBody := DeployS3CommitterRequest{ |
| 26 | + ZeetDeploymentId: zeetDeploymentId, |
| 27 | + } |
| 28 | + |
| 29 | + jsonData, err := json.Marshal(requestBody) |
| 30 | + if err != nil { |
| 31 | + log.Error().Err(err).Msg("Failed to marshal request body") |
| 32 | + return |
| 33 | + } |
| 34 | + |
| 35 | + // Create HTTP request |
| 36 | + url := fmt.Sprintf("%s/service/chains/%s/deploy-s3-committer", serviceURL, libs.ChainIdStr) |
| 37 | + req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData)) |
| 38 | + if err != nil { |
| 39 | + log.Error().Err(err).Msg("Failed to create HTTP request") |
| 40 | + return |
| 41 | + } |
| 42 | + |
| 43 | + // Set headers |
| 44 | + req.Header.Set("Content-Type", "application/json") |
| 45 | + req.Header.Set("x-service-api-key", apiKey) |
| 46 | + |
| 47 | + // Create HTTP client with timeout |
| 48 | + client := &http.Client{ |
| 49 | + Timeout: 30 * time.Second, |
| 50 | + } |
| 51 | + |
| 52 | + // Send request |
| 53 | + log.Info(). |
| 54 | + Str("url", url). |
| 55 | + Str("zeetDeploymentId", zeetDeploymentId). |
| 56 | + Msg("Sending deploy-s3-committer request to disable indexer") |
| 57 | + |
| 58 | + resp, err := client.Do(req) |
| 59 | + if err != nil { |
| 60 | + log.Error().Err(err).Msg("Failed to send HTTP request") |
| 61 | + return |
| 62 | + } |
| 63 | + defer resp.Body.Close() |
| 64 | + |
| 65 | + // Check response status |
| 66 | + if resp.StatusCode >= 200 && resp.StatusCode < 300 { |
| 67 | + log.Info(). |
| 68 | + Int("statusCode", resp.StatusCode). |
| 69 | + Msg("Successfully sent deploy-s3-committer request. Indexer disabled") |
| 70 | + } else { |
| 71 | + log.Error(). |
| 72 | + Int("statusCode", resp.StatusCode). |
| 73 | + Msg("Deploy-s3-committer request failed. Could not disable indexer") |
| 74 | + } |
| 75 | +} |
0 commit comments