# Cluster Deletion Integration Handler

## Overview

`IntegrationWithClusterDeleting` is a cluster deletion event handler that automatically cleans up all Integration tasks related to a cluster when the cluster is being deleted.

## Functionality

When receiving a `ClusterEventTypeDeleting` event, this handler will:

1. **Find Related Integrations**: Query all Integration records with status not equal to `NONE` based on Cluster ID
2. **Batch Deletion Processing**: Iterate through each found Integration and execute deletion logic
3. **Status Update**: Update Integration status to `IntegrationStatusNone`
4. **Resource Cleanup**: Run the `delete-pipeline` workflow to delete related pipeline resources

## Implementation Details

### Main Methods

- `Match()`: Matches the `ClusterEventTypeDeleting` event type
- `Execute()`: Main method for executing deletion logic
- `deleteIntegration()`: Helper method for deleting a single Integration

### Query Logic

```sql
SELECT * FROM integrations_v2 
WHERE cluster_id = ? AND status != 'NONE'
```

### Deletion Process

1. Update database status: `UPDATE integrations_v2 SET status = 'NONE' WHERE id = ?`
2. Execute workflow: Run the `delete-pipeline` DAG to clean up pipeline resources

### Error Handling

- Database query failure: Log error and return failure
- Single Integration deletion failure: Log error but continue processing other Integrations
- Workflow execution failure: Log warning but do not interrupt deletion process (status already updated)

### Logging

- Detailed logging including Cluster ID, Integration ID, provider type, and other key information
- Different log levels such as INFO, WARN, ERROR

## Use Cases

This handler will be triggered in the following scenarios:

- User actively deletes a cluster
- Cluster enters deletion state due to other reasons
- Batch cleanup operations

## Security Considerations

- Only processes Integrations with status not equal to `NONE`, avoiding duplicate processing
- Uses transactions to ensure data consistency
- Workflow failures do not cause status rollback, ensuring idempotency of deletion operations

## Monitoring and Observability

- Complete structured logging
- Tracing information including Cluster ID and Integration ID
- Detailed recording of errors and warnings for troubleshooting
