コード例 #1
0
        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;
        }
コード例 #2
0
        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;
            }
        }