private async Task<bool> loadConfigFromBlob()
        {
            try
            {
                string connectionString = blobConnection;
                string tableName = blobTableName;
                string partitionKey = blobPartitionkey;

                CloudStorageAccount cloudStorageAccount = CloudStorageAccount.Parse(connectionString);
                CloudTableClient tableClient = cloudStorageAccount.CreateCloudTableClient();
                CloudTable cloudTable = tableClient.GetTableReference(tableName);

                TableQuery<ExternalResource> query = new TableQuery<ExternalResource>().Where(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, partitionKey));
                var result = await cloudTable.ExecuteQuerySegmentedAsync(query, null);

                var listResources = result.ToList();

                IReliableDictionary<string, ExternalResource> rsDict = await StateManagerHelper.GetExternalResourceDictAsync(this.StateManager);

                using (ITransaction tx = this.StateManager.CreateTransaction())
                {
                    for (int i = 0; i < listResources.Count; i++)
                    {
                        var item = listResources[i];
                        var exists = await rsDict.TryGetValueAsync(tx, item.RowKey);

                        if (exists.HasValue)
                        {
                            if (false == exists.Value.ETag.Equals(item.ETag))
                            {
                                item.IsUpdate = true;
                            }
                            else
                            {
                                item.IsUpdate = false;
                            }
                        }
                        else
                        {
                            item.IsUpdate = true;
                        }

                        await rsDict.AddOrUpdateAsync(tx, item.RowKey, item, (k, v) =>
                        {
                            return item;
                        }, TimeSpan.FromSeconds(5), CancellationToken.None);

                    }

                    await tx.CommitAsync().ConfigureAwait(false);
                }
            }
            catch (Exception ex)
            {
                ServiceEventSource.Current.Exception("Load resource from blob failed", ex.Message, ex.StackTrace);
                return false;
            }

            return true;
        }
Exemplo n.º 2
0
        public async Task <IActionResult> Get(string name)
        {
            List <ExternalResource> listResources = await StateManagerHelper.GetExternalResourcesAsync(_stateManager);

            var extRes = listResources.Where(p => p.RowKey == name).FirstOrDefault();

            if (extRes != null)
            {
                bool isSuccess = false;
                try
                {
                    isSuccess = await checkResourceHealth(extRes);

                    if (isSuccess)
                    {
                        return(Ok());
                    }
                    else
                    {
                        return(StatusCode(500));
                    }
                }
                catch (Exception ex)
                {
                    ServiceEventSource.Current.Message("Error: {0} - {1}", ex.Message, ex.StackTrace);
                    return(StatusCode(500, ex));
                }
            }
            else
            {
                return(StatusCode(404));
            }
        }
        private async Task<bool> registerResourcesToWatchdog(CancellationToken cancellationToken)
        {
            try
            {
                var listResources = await StateManagerHelper.GetExternalResourcesAsync(this.StateManager);
                var updateResources = listResources.Where(p => p.IsUpdate).ToList();
                List<Task> listTaskRegister = new List<Task>();

                for (int i = 0; i < updateResources.Count; i++)
                {
                    var rs = listResources[i];
                    listTaskRegister.Add(new Task(async () =>
                    {
                        IReliableDictionary<string, ExternalResource> rsDict = await StateManagerHelper.GetExternalResourceDictAsync(this.StateManager);
                        try
                        {
                            await WatchdogHelper.RegisterHealthCheckAsync(rs.RowKey, this.Context.ServiceName, this.Context.PartitionId, rs.Frequency,
                                                                            $"api/health/{rs.RowKey}", cancellationToken);
                            rs.IsUpdate = false;
                            using (ITransaction tx = this.StateManager.CreateTransaction())
                            {
                                await rsDict.AddOrUpdateAsync(tx, rs.RowKey, rs, (k, v) =>
                                {
                                    return rs;
                                }, TimeSpan.FromSeconds(5), CancellationToken.None);

                                await tx.CommitAsync().ConfigureAwait(false);
                            }
                        }
                        catch (Exception ex)
                        {
                            ServiceEventSource.Current.Exception($"Resource {rs.RowKey} register failed", ex.Message, ex.StackTrace);
                        }
                    }));

                }

                listTaskRegister.ForEach(task =>
                {
                    task.Start();
                });

                Task.WaitAll(listTaskRegister.ToArray());

                return true;
            }
            catch (Exception ex)
            {
                ServiceEventSource.Current.Exception("Register resources failed", ex.Message, ex.StackTrace);
                return false;
            }
        }
Exemplo n.º 4
0
        public async Task <IActionResult> Get()
        {
            try
            {
                List <ExternalResource> listResources = await StateManagerHelper.GetExternalResourcesAsync(_stateManager);

                return(Ok(listResources));
            }
            catch (Exception ex)
            {
                return(StatusCode(500, ex));
            }
        }