public async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req, ILogger log) { string app = req.Query["app"]; if (string.IsNullOrEmpty(app)) { return(new BadRequestObjectResult("Pass an app name in the query string to clean up load testing data.")); } Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); List <Instance> instances = await _cosmosService.GetAllInstancesOfApp(app.ToLower().Trim()); int successfullyDeleted = 0; foreach (Instance instance in instances) { bool dataElementsDeleted = false; bool instanceEventsDeleted = false; bool dataElementMetadataDeleted = false; bool instanceBackupDeleted = false; bool instanceEventsBackupDeleted = false; bool dataElementsBackupDeleted = false; try { dataElementsDeleted = await _blobService.DeleteDataBlobs(instance); if (dataElementsDeleted) { dataElementMetadataDeleted = await _cosmosService.DeleteDataElementDocuments(instance.Id); dataElementsBackupDeleted = await _backupBlobService.DeleteDataBackup(instance.Id); } instanceEventsDeleted = await _cosmosService.DeleteInstanceEventDocuments(instance.InstanceOwner.PartyId, instance.Id); instanceEventsBackupDeleted = await _backupBlobService.DeleteInstanceEventsBackup(instance.InstanceOwner.PartyId, instance.Id); instanceBackupDeleted = await _backupBlobService.DeleteInstanceBackup(instance.InstanceOwner.PartyId, instance.Id); if (dataElementMetadataDeleted && dataElementsBackupDeleted && instanceEventsDeleted && instanceEventsBackupDeleted && instanceBackupDeleted) { await _cosmosService.DeleteInstanceDocument(instance.InstanceOwner.PartyId, instance.Id); successfullyDeleted += 1; log.LogInformation( "LoadTestingCleanup // Run // Instance deleted: {AppId}/{InstanceId}", instance.AppId, $"{instance.InstanceOwner.PartyId}/{instance.Id}"); } } catch (Exception e) { log.LogError( "LoadTestingCleanup // Run // Error occured when deleting instance: {AppId}/{InstanceId} \r Exception {Exception}", instance.AppId, $"{instance.InstanceOwner.PartyId}/{instance.Id}", e); } } stopwatch.Stop(); log.LogInformation( "LoadTestingCleanup // Run // {DeleteCount} of {OriginalCount} instances deleted in {Duration} s", successfullyDeleted, instances.Count, stopwatch.Elapsed.TotalSeconds); return(new OkObjectResult($"{successfullyDeleted}/{instances.Count} instances and all related data has been successfully deleted.")); }
public async Task Run([TimerTrigger("0 0 3 * * *", RunOnStartup = false)] TimerInfo timer, ILogger log) { List <Instance> instances = await _cosmosService.GetHardDeletedInstances(); List <Application> applications = await _cosmosService.GetApplications(instances.Select(i => i.AppId).ToList()); List <string> autoDeleteAppIds = applications.Where(a => a.AutoDeleteOnProcessEnd == true).Select(a => a.Id).ToList(); int successfullyDeleted = 0; Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); foreach (Instance instance in instances) { bool dataElementsDeleted = false; bool instanceEventsDeleted = false; bool dataElementMetadataDeleted = false; bool instanceBackupDeleted = false; bool instanceEventsBackupDeleted = false; bool dataElementsBackupDeleted = false; try { dataElementsDeleted = await _blobService.DeleteDataBlobs(instance); if (dataElementsDeleted) { dataElementMetadataDeleted = await _cosmosService.DeleteDataElementDocuments(instance.Id); dataElementsBackupDeleted = await _backupBlobService.DeleteDataBackup(instance.Id); } if (autoDeleteAppIds.Contains(instance.AppId)) { instanceEventsDeleted = await _cosmosService.DeleteInstanceEventDocuments(instance.InstanceOwner.PartyId, instance.Id); instanceEventsBackupDeleted = await _backupBlobService.DeleteInstanceEventsBackup(instance.InstanceOwner.PartyId, instance.Id); } instanceBackupDeleted = await _backupBlobService.DeleteInstanceBackup(instance.InstanceOwner.PartyId, instance.Id); if ( dataElementMetadataDeleted && dataElementsBackupDeleted && instanceBackupDeleted && (!autoDeleteAppIds.Contains(instance.AppId) || (instanceEventsDeleted && instanceEventsBackupDeleted))) { await _cosmosService.DeleteInstanceDocument(instance.InstanceOwner.PartyId, instance.Id); successfullyDeleted += 1; log.LogInformation( "NightlyCleanup // Run // Instance deleted: {AppId}/{InstanceId}", instance.AppId, $"{instance.InstanceOwner.PartyId}/{instance.Id}"); } } catch (Exception e) { log.LogError( "NightlyCleanup // Run // Error occured when deleting instance: {AppId}/{InstanceId} \r Exception {Exception}", instance.AppId, $"{instance.InstanceOwner.PartyId}/{instance.Id}", e); } } stopwatch.Stop(); log.LogInformation( "NightlyCleanup // Run // {DeleteCount} of {OriginalCount} instances deleted in {Duration} s", successfullyDeleted, instances.Count, stopwatch.Elapsed.TotalSeconds); }