gestione delete_after_processing
This commit is contained in:
@@ -25,6 +25,9 @@ services:
|
|||||||
DB_PASSWORD: "your_secure_password"
|
DB_PASSWORD: "your_secure_password"
|
||||||
DB_NAME: "ase_lar"
|
DB_NAME: "ase_lar"
|
||||||
|
|
||||||
|
# File Processing Behavior
|
||||||
|
# DELETE_AFTER_PROCESSING: "true" # Cancella file dopo elaborazione corretta (default: false = mantiene i file)
|
||||||
|
|
||||||
# Logging (opzionale)
|
# Logging (opzionale)
|
||||||
LOG_LEVEL: "INFO"
|
LOG_LEVEL: "INFO"
|
||||||
volumes:
|
volumes:
|
||||||
@@ -55,6 +58,9 @@ services:
|
|||||||
DB_PASSWORD: "your_secure_password"
|
DB_PASSWORD: "your_secure_password"
|
||||||
DB_NAME: "ase_lar"
|
DB_NAME: "ase_lar"
|
||||||
|
|
||||||
|
# File Processing Behavior
|
||||||
|
# DELETE_AFTER_PROCESSING: "true" # Cancella file dopo elaborazione corretta (default: false = mantiene i file)
|
||||||
|
|
||||||
# Logging (opzionale)
|
# Logging (opzionale)
|
||||||
LOG_LEVEL: "INFO"
|
LOG_LEVEL: "INFO"
|
||||||
volumes:
|
volumes:
|
||||||
@@ -85,6 +91,7 @@ services:
|
|||||||
DB_USER: "ase_user"
|
DB_USER: "ase_user"
|
||||||
DB_PASSWORD: "your_secure_password"
|
DB_PASSWORD: "your_secure_password"
|
||||||
DB_NAME: "ase_lar"
|
DB_NAME: "ase_lar"
|
||||||
|
# DELETE_AFTER_PROCESSING: "true" # Cancella file dopo elaborazione corretta (default: false = mantiene i file)
|
||||||
LOG_LEVEL: "INFO"
|
LOG_LEVEL: "INFO"
|
||||||
volumes:
|
volumes:
|
||||||
- ./logs/ftp2:/app/logs
|
- ./logs/ftp2:/app/logs
|
||||||
|
|||||||
71
docs/FILE_DELETION_POLICY.md
Normal file
71
docs/FILE_DELETION_POLICY.md
Normal file
@@ -0,0 +1,71 @@
|
|||||||
|
# File Deletion Policy
|
||||||
|
|
||||||
|
## Comportamento di Default
|
||||||
|
|
||||||
|
Per impostazione predefinita, i file ricevuti via FTP/SFTP vengono **mantenuti** sul server dopo l'elaborazione:
|
||||||
|
|
||||||
|
- ✅ **Elaborazione riuscita**: il file viene rinominato con timestamp e salvato nella directory dell'utente, i dati vengono inseriti nel database
|
||||||
|
- ❌ **Elaborazione fallita**: il file rimane nella directory dell'utente per permettere debug e riprocessamento manuale
|
||||||
|
|
||||||
|
## Abilitare la Cancellazione Automatica
|
||||||
|
|
||||||
|
Per cancellare automaticamente i file dopo un'elaborazione **riuscita**, imposta la variabile d'ambiente nel `docker-compose.yml`:
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
environment:
|
||||||
|
DELETE_AFTER_PROCESSING: "true"
|
||||||
|
```
|
||||||
|
|
||||||
|
### Valori Accettati
|
||||||
|
|
||||||
|
La variabile accetta i seguenti valori (case-insensitive):
|
||||||
|
- `true`, `1`, `yes` → cancellazione **abilitata**
|
||||||
|
- `false`, `0`, `no` o qualsiasi altro valore → cancellazione **disabilitata** (default)
|
||||||
|
|
||||||
|
## Comportamento con DELETE_AFTER_PROCESSING=true
|
||||||
|
|
||||||
|
| Scenario | Comportamento |
|
||||||
|
|----------|---------------|
|
||||||
|
| File elaborato con successo | ✅ Dati inseriti nel DB → File **cancellato** |
|
||||||
|
| Errore durante elaborazione | ❌ Errore loggato → File **mantenuto** per debug |
|
||||||
|
| File vuoto | 🗑️ File cancellato immediatamente (comportamento esistente) |
|
||||||
|
|
||||||
|
## Log
|
||||||
|
|
||||||
|
Quando un file viene cancellato dopo l'elaborazione, viene loggato:
|
||||||
|
|
||||||
|
```
|
||||||
|
INFO: File example_20250103120000.csv loaded successfully
|
||||||
|
INFO: File example_20250103120000.csv deleted after successful processing
|
||||||
|
```
|
||||||
|
|
||||||
|
In caso di errore durante la cancellazione:
|
||||||
|
|
||||||
|
```
|
||||||
|
WARNING: Failed to delete file example_20250103120000.csv: [errno] [description]
|
||||||
|
```
|
||||||
|
|
||||||
|
## Esempio Configurazione
|
||||||
|
|
||||||
|
### Mantenere i file (default)
|
||||||
|
```yaml
|
||||||
|
ftp-server:
|
||||||
|
environment:
|
||||||
|
DB_HOST: "mysql-server"
|
||||||
|
# DELETE_AFTER_PROCESSING non impostata o impostata a false
|
||||||
|
```
|
||||||
|
|
||||||
|
### Cancellare i file dopo elaborazione
|
||||||
|
```yaml
|
||||||
|
ftp-server:
|
||||||
|
environment:
|
||||||
|
DB_HOST: "mysql-server"
|
||||||
|
DELETE_AFTER_PROCESSING: "true"
|
||||||
|
```
|
||||||
|
|
||||||
|
## Note Implementative
|
||||||
|
|
||||||
|
- La cancellazione avviene **solo dopo** l'inserimento riuscito nel database
|
||||||
|
- Se la cancellazione fallisce, viene loggato un warning ma l'elaborazione è considerata riuscita
|
||||||
|
- I file con errori di elaborazione rimangono sempre sul server indipendentemente dalla configurazione
|
||||||
|
- La policy si applica sia a FTP che a SFTP
|
||||||
@@ -34,6 +34,10 @@ class Config:
|
|||||||
self.fileext = c.get("ftpserver", "fileext").upper().split("|")
|
self.fileext = c.get("ftpserver", "fileext").upper().split("|")
|
||||||
self.defperm = c.get("ftpserver", "defaultUserPerm")
|
self.defperm = c.get("ftpserver", "defaultUserPerm")
|
||||||
|
|
||||||
|
# File processing behavior: delete files after successful processing
|
||||||
|
# Set DELETE_AFTER_PROCESSING=true in docker-compose to enable
|
||||||
|
self.delete_after_processing = os.getenv("DELETE_AFTER_PROCESSING", "false").lower() in ("true", "1", "yes")
|
||||||
|
|
||||||
# CSV FILE setting
|
# CSV FILE setting
|
||||||
self.csvfs = c.get("csvfs", "path")
|
self.csvfs = c.get("csvfs", "path")
|
||||||
|
|
||||||
|
|||||||
@@ -109,6 +109,14 @@ async def on_file_received_async(self: object, file: str) -> None:
|
|||||||
# Note: autocommit=True in connection, no need for explicit commit
|
# Note: autocommit=True in connection, no need for explicit commit
|
||||||
logger.info(f"File {new_filename} loaded successfully")
|
logger.info(f"File {new_filename} loaded successfully")
|
||||||
|
|
||||||
|
# Delete file after successful processing if configured
|
||||||
|
if getattr(cfg, 'delete_after_processing', False):
|
||||||
|
try:
|
||||||
|
os.remove(f"{path}/{new_filename}")
|
||||||
|
logger.info(f"File {new_filename} deleted after successful processing")
|
||||||
|
except Exception as e:
|
||||||
|
logger.warning(f"Failed to delete file {new_filename}: {e}")
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"File {new_filename} not loaded. Held in user path.")
|
logger.error(f"File {new_filename} not loaded. Held in user path.")
|
||||||
logger.error(f"{e}")
|
logger.error(f"{e}")
|
||||||
|
|||||||
Reference in New Issue
Block a user