Exemple #1
0
        /// <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);
                    }
                }
            }
        }