public override IPreingestCommand FactoryMethod(Guid guid, dynamic data) { if (data == null) { Logger.LogInformation("FactoryMethod :: {1} : {0}.", "Incoming data is empty", guid); return(null); } Plan[] plans = data.scheduledPlan == null ? null : JsonConvert.DeserializeObject <Plan[]>(data.scheduledPlan.ToString()); PreingestAction[] actions = data.preingest == null ? null : JsonConvert.DeserializeObject <PreingestAction[]>(data.preingest.ToString()); if (plans == null) { Logger.LogInformation("FactoryMethod :: {1} : {0}.", "No scheduled plan found, just exit", guid); return(null); } Queue <Plan> queue = new Queue <Plan>(plans); Plan next = null; Plan previous = null; while (queue.Count > 0) { Plan item = queue.Peek(); //found one running (should not), just break it if (item.Status == ExecutionStatus.Executing) { break; } //found one done (previous), peek if null done else next if (item.Status == ExecutionStatus.Done) { previous = queue.Dequeue(); Plan peek = queue.Count > 0 ? queue.Peek() : null; if (peek == null) //done just exit { Logger.LogInformation("FactoryMethod :: {1} : {0}.", "Peek queue. See nothing. Probably done with the plan", guid); return(null); } if (peek.Status == ExecutionStatus.Done) { continue; } if (peek.Status == ExecutionStatus.Pending) { next = queue.Dequeue(); break; } } //found one pending, just fire next if (item.Status == ExecutionStatus.Pending) { next = queue.Dequeue(); break; } } if (next == null) { Logger.LogInformation("FactoryMethod :: {1} : {0}.", "Exit the factory method, no next task planned", guid); return(null); } if (previous == null && next != null) { IKey key = new DefaultKey(next.ActionName); if (!this._executionCommand.ContainsKey(key)) { Logger.LogInformation("FactoryMethod :: {1} : No key found in dictionary with {0}.", key, guid); return(null); } bool isNextOverallStatusOk2Run = OnStartError(previous, next, plans, actions); if (!isNextOverallStatusOk2Run) { Logger.LogInformation("FactoryMethod :: {1} : Overall status tell us not to continue {0}.", key, guid); return(new FailCommand(next.ActionName, Logger, _webapiUrl)); } IPreingestCommand command = this._executionCommand[key]; return(command); } if (previous != null && next != null) { if (actions == null) { Logger.LogInformation("FactoryMethod :: {0} : Plan described previous and next action, but there is no actions list returned. Hmmm....", guid); return(null); } var action = actions.Where(item => item.Name == previous.ActionName.ToString()).FirstOrDefault(); if (action == null) { Logger.LogInformation("FactoryMethod :: {1} : No action found in the list with the name {0}.", previous.ActionName.ToString(), guid); return(null); } IKey key = new DefaultKey(next.ActionName); if (!this._executionCommand.ContainsKey(key)) { Logger.LogInformation("FactoryMethod :: {1} : No key found in dictionary with {0}.", key, guid); return(null); } bool isPreviousOk2Run = false; switch (action.ActionStatus) { case "Error": if (previous.ContinueOnError) { isPreviousOk2Run = true; } break; case "Failed": if (previous.ContinueOnFailed) { isPreviousOk2Run = true; } break; case "Success": { isPreviousOk2Run = true; } break; default: isPreviousOk2Run = false; break; } bool isNextOverallStatusOk2Run = OnStartError(previous, next, plans, actions); if (isPreviousOk2Run) { if (!isNextOverallStatusOk2Run) { Logger.LogInformation("FactoryMethod :: {1} : Overall status tell us not to continue {0}.", key, guid); return(new FailCommand(next.ActionName, Logger, _webapiUrl)); } IPreingestCommand command = this._executionCommand[key]; return(command); } Logger.LogInformation("FactoryMethod :: {1} : Not OK to run {0}.", key, guid); } return(null); }
public async Task <bool> Connect(CancellationToken token) { if (Connection == null) { CurrentLogger.LogInformation("Hub connection is empty! Not initialised. Please check if the URL is correct."); return(false); } while (true) { System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly(); System.Diagnostics.FileVersionInfo fvi = System.Diagnostics.FileVersionInfo.GetVersionInfo(assembly.Location); DateTime buildDate = LinkerHelper.GetLinkerTimestampUtc(assembly); CurrentLogger.LogInformation(String.Format("{0} version {1}. Build date and time {2}.", fvi.ProductName, fvi.ProductVersion, DateTimeOffset.FromFileTime(buildDate.ToFileTime()))); CurrentLogger.LogInformation("Hub connection state - Method(Connect) - {0}", Connection.State); if (Connection.State == HubConnectionState.Connected) { lock (consumeLock) { while (!_internalCollection.IsCompleted) { BlockItem item = _internalCollection.Take(); Task.Run(() => { try { dynamic data = JsonConvert.DeserializeObject <dynamic>(item.Data); IPreingestCommand command = Creator.FactoryMethod(item.SessionId, data); if (command != null) { Settings settings = data.settings == null ? null : JsonConvert.DeserializeObject <Settings>(data.settings.ToString()); using (HttpClient client = new HttpClient()) { if (settings == null) { command.Execute(client, item.SessionId); } else { command.Execute(client, item.SessionId, settings); } } } } catch (Exception e) { CurrentLogger.LogInformation("An exception occurred with SessionId {0}.", item.SessionId); CurrentLogger.LogError(e, e.Message); } finally { } }); } } return(true); } try { await Connection.StartAsync(token); CurrentLogger.LogInformation("Hub connection state - Method(Connect) after StartAsync - {0}", Connection.State); return(true); } catch when(token.IsCancellationRequested) { return(false); }