/// <summary>
        /// This is a helper function that can be used for updating the blob both when delete and create kernels are called
        /// </summary>
        /// <param name="kernelId">KernelId is the parameter for the kernel that is being created and deleted</param>
        /// <param name="userId">userId</param>
        /// <param name="flowId">flowId</param>
        /// <param name="connectionString">Ops Connection string that is need to access the blob</param>
        /// <param name="blobUri">blobUri of the blob where the kernel data is stored</param>
        /// <param name="isCreate">IsCerate is the boolean which tells whether to delete or add an entry in the dictionry in the blob</param>
        /// <returns>Returns success or the error message as ApiResult as the case maybe</returns>
        public async Task <ApiResult> UpdateGarbageCollectKernelBlob(string kernelId, string userId, string flowId, string connectionString, string blobUri, bool isCreate)
        {
            try
            {
                string content = await Task.Run(() => BlobHelper.GetBlobContent(connectionString, blobUri));

                Dictionary <string, KernelProperties> kernelPropertiesDict = new Dictionary <string, KernelProperties>();
                if (!string.IsNullOrEmpty(content))
                {
                    kernelPropertiesDict = JsonConvert.DeserializeObject <Dictionary <string, KernelProperties> >(content);
                }
                if (isCreate)
                {
                    kernelPropertiesDict.Add(kernelId, new KernelProperties()
                    {
                        UserId = userId, FlowId = flowId, CreateDate = DateTime.Now
                    });
                }
                else if (kernelPropertiesDict.Count > 0 && kernelPropertiesDict.ContainsKey(kernelId))
                {
                    kernelPropertiesDict.Remove(kernelId);
                }
                content = JsonConvert.SerializeObject(kernelPropertiesDict);
                await Task.Run(() => BlobHelper.SaveContentToBlob(connectionString, blobUri, content));

                return(ApiResult.CreateSuccess("Updated Blob"));
            }
            catch (Exception ex)
            {
                return(ApiResult.CreateError(ex.Message));
            }
        }
예제 #2
0
        /// <summary>
        /// This is the function that gets the kernelList of kernels from the blob that are older than 3 hours and deletes them
        /// </summary>
        /// <param name="connectionString">Pass in the connection string for the Ops blob storage where there is a kernelList kernels mantained as they are created</param>
        /// <param name="blobUri">blobUri: This is the Uri to the diagnostic blob where the kernelList all created kernels is maintained</param>
        /// <returns>Returns the result of delete call. If this fails because a kernel does not exist, it does not fail but continues to delete the remaining kernels</returns>
        public async Task <ApiResult> GarbageCollectListOfKernels(string connectionString, string blobUri, bool isDeleteAll = false)
        {
            string content = await Task.Run(() => BlobHelper.GetBlobContent(connectionString, blobUri));

            List <string> kernelList = new List <string>();

            if (!string.IsNullOrEmpty(content))
            {
                Dictionary <string, KernelProperties> kernelPropertiesDict = JsonConvert.DeserializeObject <Dictionary <string, KernelProperties> >(content);
                foreach (KeyValuePair <string, KernelProperties> kvp in kernelPropertiesDict)
                {
                    if (isDeleteAll)
                    {
                        kernelList.Add(kvp.Key);
                    }
                    else
                    {
                        //A list gets generated for all kernels that are more than 3 hours old. Front end needs to call this api on a regular cadence
                        if (DateTime.Now.AddHours(-3) > kvp.Value.CreateDate)
                        {
                            kernelList.Add(kvp.Key);
                        }
                    }
                }

                var response = await GarbageCollectKernelsHelper(kernelList);

                if (response.Error.HasValue && response.Error.Value)
                {
                    return(response);
                }
                foreach (string kerId in kernelList)
                {
                    kernelPropertiesDict.Remove(kerId);
                }
                if (response.Error.HasValue && response.Error.Value)
                {
                    return(ApiResult.CreateError(response.Message));
                }

                content = JsonConvert.SerializeObject(kernelPropertiesDict);
                await Task.Run(() => BlobHelper.SaveContentToBlob(connectionString, blobUri, content));

                return(response);
            }
            else
            {
                return(ApiResult.CreateSuccess("No Kernels to delete"));
            }
        }