private async Task RunPhase(int episodeId, SrStoredEpisodePhases phase, Func <Task> action, Func <Task> actionOnSuccess) { try { var status = await _storage.GetEpisodeStatus(episodeId); Enum.TryParse <SrStoredEpisodePhases>(status.Phase, out var currentPhase); if (currentPhase > phase || currentPhase == phase && status.State != SrStoredEpisodeStates.Unknown) { if (currentPhase == phase && status.State != SrStoredEpisodeStates.Error) { await actionOnSuccess(); } return; } await _storage.StoreEpisodeStatus(episodeId, SrStoredEpisodeStatus.Started(episodeId, phase)); await action(); await _storage.StoreEpisodeStatus(episodeId, SrStoredEpisodeStatus.Done(episodeId, phase)); await actionOnSuccess(); } catch (Exception e) { _logger.LogError($"Error on phase {phase} for episode {episodeId}", e); await _storage.StoreEpisodeStatus(episodeId, SrStoredEpisodeStatus.Error(episodeId, phase, e.Message)); } }
public static SrStoredEpisodeStatus Error(int episodeId, SrStoredEpisodePhases phase, string exception) { return(new SrStoredEpisodeStatus { EpisodeId = episodeId, Phase = phase.ToString(), State = SrStoredEpisodeStates.Error, Info = exception }); }
public static SrStoredEpisodeStatus Done(int episodeId, SrStoredEpisodePhases phase) { return(new SrStoredEpisodeStatus { EpisodeId = episodeId, Phase = phase.ToString(), State = SrStoredEpisodeStates.Done, Info = string.Empty }); }