public override Task <UpdateStepRequest> HandleStep(Step step) { var updateRequest = new UpdateStepRequest() { Id = step.Id }; switch (step.StepTemplateId) { case "Fibonacci_stepTemplate:0": var completeStep = true; if (testSuspension) { var n = random.Next(0, 2); if (n == 1) { completeStep = false; } var logSuccessfullySent = SendStepLog(step.Id, "Test").GetAwaiter().GetResult(); } if (completeStep) { var result = CalculateFibonacci((Int64)DynamicDataUtility.GetData(step.Inputs, "n-1").Value, (Int64)(DynamicDataUtility.GetData(step.Inputs, "n-2").Value)); updateRequest.Outputs = new Dictionary <string, object>() { { "n", result } }; updateRequest.Status = StepStatuses.Successful; return(Task.FromResult(updateRequest)); } else { updateRequest.Status = StepStatuses.Suspended; return(Task.FromResult(updateRequest)); } case "Pass_Password:0": if (((string)(DynamicDataUtility.GetData(step.Inputs, "secret").Value) == "This is a test")) { { updateRequest.Outputs = new Dictionary <string, object>() { { "secret", (string)(DynamicDataUtility.GetData(step.Inputs, "secret").Value) } }; updateRequest.Status = StepStatuses.Successful; return(Task.FromResult(updateRequest)); } } else { throw new Exception("Failed to get password."); } } throw new NotImplementedException(); }
public void Update_Step_Should_Throw_With_Invalid_Batch_Id() { // Arrange batchRepository.DoesBatchExist(Arg.Any <string>()) .Returns(false); var request = new UpdateStepRequest { BatchId = TestBatchId }; // Act / Assert var exception = Assert.ThrowsAsync <HttpError>(() => Sut.Put(request)); exception.ErrorCode.Should().Be(HttpStatusCode.NotFound.ToString()); exception.Message.Should().Be("Batch TestBatch not found"); }
public async Task <UpdateStepResponse> Put(UpdateStepRequest request) { if (!await batchRepository.DoesBatchExist(request.BatchId)) { throw Err.BatchNotFound(request.BatchId); } var step = request.ConvertTo <Step>(); var updated = await stepRepository.Update(step); if (!updated) { throw Err.StepNotFound(request.StepName); } return(new UpdateStepResponse()); }
public async Task <string> CompleteStep(UpdateStepRequest request, string idToken) { return((await SendPutRequest("/api/steps/" + request.Id, request, true)).Value <string>("objectRefId")); }
/// <summary> /// Used for running the main loop /// </summary> public async void BotLoop() { Logger.LogDebug("Starting loops."); var stopWatch = new Stopwatch(); while (started) { Logger.LogDebug("Starting new Thread"); Step nextStep = null; string encryptionKey = ""; try { var response = await GetNextStep(); nextStep = response.Step; encryptionKey = response.EncryptionKey; } catch (Exception e) { Logger.LogWarning("Error getting next step, will sleep and try again. " + e.Message); } stopWatch.Reset(); stopWatch.Start(); UpdateStepRequest stepResult = new UpdateStepRequest(); string newEncryptionKey = SecurityUtility.RandomString(32, false); if (nextStep != null) { Logger.LogInformation("Processing step " + nextStep.Id); stepResult.Id = nextStep.Id; string decryptedEncryptionKey = encryptionKey != null && encryptionKey != "" ? SecurityUtility.RsaDecryptWithPrivate(encryptionKey, _client.keyPair.PrivateKey): ""; nextStep.Inputs = DynamicDataUtility.DecryptDynamicData(RegisteredTemplates.Where(rt => rt.ReferenceId == nextStep.StepTemplateId).First().InputDefinitions, nextStep.Inputs, Domain.Enums.EncryptionProtocol .AES256, decryptedEncryptionKey, false); var template = RegisteredTemplates.Where(rt => rt.ReferenceId == nextStep.StepTemplateId).First(); try { stepResult = await ProcessStep(nextStep); stepResult.Outputs = DynamicDataUtility.EncryptDynamicData(template.OutputDefinitions, stepResult.Outputs, Domain.Enums.EncryptionProtocol.AES256, newEncryptionKey); stepResult.EncryptionKey = SecurityUtility.RsaEncryptWithPrivate(newEncryptionKey, _client.keyPair.PrivateKey); } catch (Exception e) { await SendStepLog(nextStep.Id, "Encountered unexcepted error " + e.Message + Environment.NewLine + e.StackTrace); stepResult.Status = StepStatuses.Error; stepResult.Log = "Encountered uncaught error at " + e.Message + "."; } int count = 0; string success = ""; try { success = await _client.CompleteStep(stepResult, idToken); Logger.LogInformation("Completed step " + stepResult.Id + " with status " + stepResult.Status); } catch (Exception e) { Logger.LogError("Failed to save step " + stepResult.Id + " with status " + stepResult.Status + " in Cindi with exception " + e.Message + "."); Thread.Sleep(1000); } } else { Logger.LogDebug("No step found"); } loopNumber++; stopWatch.Stop(); Logger.LogDebug("Completed Service Loop " + loopNumber + " took approximately " + stopWatch.ElapsedMilliseconds + "ms"); //Only sleep if no step was picked up if (nextStep == null) { lock (waitTimeLocker) { Logger.LogDebug("Sleeping for " + waitTime + "ms"); Thread.Sleep(waitTime); } } } }
public async override Task <UpdateStepRequest> HandleStep(Step step) { var updateRequest = new UpdateStepRequest() { Id = step.Id }; switch (step.StepTemplateId) { case "_GenerateSystemReport:0": var totalStepCount = (await _mediator.Send(new GetEntitiesQuery <Step> { Page = 0, Size = 0, })).Count; var totalUnassignedStepCount = (await _mediator.Send(new GetEntitiesQuery <Step> { Page = 0, Size = 0, Expression = e => e.Status == StepStatuses.Unassigned })).Count; var totalActiveBotCount = (await _mediator.Send(new GetEntitiesQuery <BotKey> { Page = 0, Size = 0, Expression = e => e.IsDisabled == false })).Count; updateRequest.Outputs = new Dictionary <string, object>() { { "report", "Total Steps: " + totalStepCount + ", Total Unassigned Steps:" + totalUnassignedStepCount + ", Total Active Bots: " + totalActiveBotCount }, { "slack_report", JsonConvert.SerializeObject(new[] { new { type = "section", fields = new[] { new { text = "Total Unassigned Steps: " + totalUnassignedStepCount, type = "mrkdwn" } } }, new { type = "section", fields = new[] { new { text = "Total Steps: " + totalStepCount, type = "mrkdwn" } } }, new { type = "section", fields = new[] { new { text = "Total Active Bots: " + totalActiveBotCount, type = "mrkdwn" } } } }) }, { "markdown", "" } }; updateRequest.Status = StepStatuses.Successful; updateRequest.StatusCode = 0; return(updateRequest); case "_SendSlackMessage:0": var client = new SlackClient(_clientFactory); await client.PostMessage((string)DynamicDataUtility.GetData(step.Inputs, "webhook_url").Value, new { username = (string)DynamicDataUtility.GetData(step.Inputs, "from").Value, icon_emoji = step.Inputs.ContainsKey("icon_emoji") ? (string)DynamicDataUtility.GetData(step.Inputs, "icon_emoji").Value : null, icon_url = step.Inputs.ContainsKey("icon_url") ? (string)DynamicDataUtility.GetData(step.Inputs, "icon_url").Value : null, channel = step.Inputs.ContainsKey("channel") ? (string)DynamicDataUtility.GetData(step.Inputs, "channel").Value : null, blocks = step.Inputs.ContainsKey("blocks") ? JsonConvert.DeserializeObject((string)DynamicDataUtility.GetData(step.Inputs, "blocks").Value) : null, text = step.Inputs.ContainsKey("text") ? (string)DynamicDataUtility.GetData(step.Inputs, "text").Value : null }); updateRequest.Status = StepStatuses.Successful; return(updateRequest); } updateRequest.Status = StepStatuses.Error; updateRequest.Log = "Bot does not have a catch for " + step.StepTemplateId; return(updateRequest); }