public async Task ExecutePreservation(string idTask) { PreservationService preservationService = new PreservationService(); PreservationTask task = null; try { SendProgressMessage($"Inizio attività di conservazione per il task {idTask}, a breve verranno visualizzate le attività realtive allo stato di conservazione", ProgressMessageLevel.Info, Context.ConnectionId); SendProgressMessage($"L'attività potrebbe richiedere alcuni minuti, attendere prego...", ProgressMessageLevel.Info, Context.ConnectionId); if (!Guid.TryParse(idTask, out Guid taskId)) { throw new ArgumentException($"Il parametro passato non è nel formato corretto.", nameof(idTask)); } task = preservationService.GetPreservationTask(taskId); if (string.IsNullOrEmpty(task.Archive.PreservationConfiguration)) { throw new Exception($"L'archivio {task.Archive.Name} non è stato configurato"); } ArchiveConfiguration archiveConfiguration = JsonConvert.DeserializeObject <ArchiveConfiguration>(task.Archive.PreservationConfiguration); if (task.LockDate.HasValue) { throw new Exception($"Il Task {taskId} è bloccato e non può essere processato"); } if (task.IdPreservation.HasValue) { throw new Exception($"Il Task {taskId} ha già una conservazione associata"); } if (!preservationService.LockTask(task)) { throw new Exception($"Task is LOCK {task.IdPreservationTask}"); } string correlationId = Context.ConnectionId; await StartHubConnection(correlationId); using (HttpClient client = new HttpClient()) { CommandExecutePreservation command = new CommandExecutePreservation(); command.ReferenceId = correlationId; command.IdTask = taskId; command.AutoGenerateNextTask = archiveConfiguration.AutoGeneratedNextTask; command.PDVArchive = ConfigurationHelper.PDVArchiveName; command.RDVArchive = ConfigurationHelper.RDVArchiveName; await client.SendAsync(new HttpRequestMessage(HttpMethod.Post, $"{ConfigurationHelper.WCFHostWebAPIUrl}/api/CQRS") { Content = new ObjectContent <CommandExecutePreservation>(command, new JsonMediaTypeFormatter() { SerializerSettings = _serializerSettings }) }); } } catch (TaskCanceledException tex) { _logger.Warn("Il task ha impiegato molto tempo per l'esecuzione ed è stato annullato. L' attività di conservazione continuerà in background.", tex); SendProgressMessage("L'attività di conservazione stà impiegando più tempo del previsto. Si prega di attendere il completamento del processo.", ProgressMessageLevel.Warning, Context.ConnectionId); } catch (Exception ex) { _logger.Error("Error on create preservation", ex); if (task != null) { preservationService.UnlockTask(task); } SendProgressMessage($"Errore nell'esecuzione dell'attività di conservazione: {ex.Message}", ProgressMessageLevel.Error, Context.ConnectionId); } }
public override async Task Execute(CommandModel commandModel) { PreservationTask preservationTask = null; try { _commandModel = commandModel; if (!(commandModel is CommandExecutePreservation)) { _logger.Error($"Command is not of type {nameof(CommandExecutePreservation)}"); await SendNotification(commandModel.ReferenceId, "E' avvenuto un errore durante la gestione del comando di esecuzione conservazione", NotifyLevel.Error, true); return; } CommandExecutePreservation @command = commandModel as CommandExecutePreservation; if (command.IdTask == Guid.Empty) { _logger.Error($"Command with idTask not defined"); await SendNotification(command.ReferenceId, "Non è stato definito un ID Task per l'esecuzione della conservazione", NotifyLevel.Error, true); return; } preservationTask = _preservationService.GetPreservationTask(command.IdTask); if (preservationTask == null) { _logger.Error($"Task {command.IdTask} not found"); await SendNotification(command.ReferenceId, $"Non è stato trovato un Task con id {command.IdTask}", NotifyLevel.Error, true); return; } PreservationInfoResponse response = _preservationService.CreatePreservation(preservationTask); if (response.HasErros) { await SendNotification(command.ReferenceId, $"Errore nell'attività di conservazione del task con id {command.IdTask}: {response.Error.Message}", NotifyLevel.Error, true); return; } if (preservationTask.TaskType.Type == PreservationTaskTypes.Preservation && command.AutoGenerateNextTask) { try { _preservationService.AutoGenerateNextTask(preservationTask); } catch (Exception ex) { _logger.Warn($"E' avvenuto un errore durante la generazione del task di conservazione successivo al task {command.IdTask}", ex); await SendNotification(command.ReferenceId, $"Non è stato possibile generare il task di conservazione successivo al task {command.IdTask}", NotifyLevel.Warning); } } if (response.AwardBatchesXml != null && response.AwardBatchesXml.Count > 0) { foreach (KeyValuePair <Guid, string> awardBatchXml in response.AwardBatchesXml) { string serializedContent = Convert.ToBase64String(Encoding.UTF8.GetBytes(awardBatchXml.Value)); if (!string.IsNullOrEmpty(command.PDVArchive)) { CommandInsertPreservationPDV pdvCommand = new CommandInsertPreservationPDV() { PDVArchive = command.PDVArchive, ReferenceId = command.ReferenceId, IdAwardBatch = awardBatchXml.Key, Content = serializedContent }; await Mediator.Send(pdvCommand); } if (!string.IsNullOrEmpty(command.RDVArchive)) { CommandInsertPreservationRDV rdvCommand = new CommandInsertPreservationRDV() { RDVArchive = command.RDVArchive, ReferenceId = command.ReferenceId, IdAwardBatch = awardBatchXml.Key, Content = serializedContent }; await Mediator.Send(rdvCommand); } } } await SendNotification(command.ReferenceId, $"Processo di conservazione terminato", NotifyLevel.Info, true); } catch (Exception ex) { _logger.Error($"Error on execute preservation task", ex); await SendNotification(commandModel.ReferenceId, $"E' avvenuto un errore durante l'attività richiesta", NotifyLevel.Error, true); } finally { if (preservationTask != null) { _preservationService.UnlockTask(preservationTask); } } }