public async Task<IActionResult> GetQueueLengthAsync() { ServiceUriBuilder uriBuilder = new ServiceUriBuilder(TenantDataServiceName); Uri serviceUri = uriBuilder.Build(); // service may be partitioned. // this will aggregate the queue lengths from each partition ServicePartitionList partitions = await this.fabricClient.QueryManager.GetPartitionListAsync(serviceUri); long count = 0; foreach (Partition partition in partitions) { Uri getUrl = new HttpServiceUriBuilder() .SetServiceName(serviceUri) .SetPartitionKey(((Int64RangePartitionInformation) partition.PartitionInformation).LowKey) .SetServicePathAndQuery($"/api/devices/queue/length") .Build(); HttpResponseMessage response = await this.httpClient.GetAsync(getUrl, this.serviceCancellationToken); if (response.StatusCode != System.Net.HttpStatusCode.OK) { return this.StatusCode((int) response.StatusCode); } string result = await response.Content.ReadAsStringAsync(); count += Int64.Parse(result); } return this.Ok(count); }
public async Task<IActionResult> Post([FromRoute] string tenantName, [FromBody] TenantApplicationParams parameters) { // First create the application instance. // This won't actually create the services yet. ApplicationDescription application = new ApplicationDescription( new Uri($"{Names.TenantApplicationNamePrefix}/{tenantName}"), Names.TenantApplicationTypeName, parameters.Version); await this.fabricClient.ApplicationManager.CreateApplicationAsync(application, this.operationTimeout, this.serviceCancellationToken); // Now create the data service in the new application instance. ServiceUriBuilder dataServiceNameUriBuilder = new ServiceUriBuilder(application.ApplicationName.ToString(), Names.TenantDataServiceName); StatefulServiceDescription dataServiceDescription = new StatefulServiceDescription() { ApplicationName = application.ApplicationName, HasPersistedState = true, MinReplicaSetSize = 3, TargetReplicaSetSize = 3, PartitionSchemeDescription = new UniformInt64RangePartitionSchemeDescription(parameters.DataPartitionCount, Int64.MinValue, Int64.MaxValue), ServiceName = dataServiceNameUriBuilder.Build(), ServiceTypeName = Names.TenantDataServiceTypeName }; await this.fabricClient.ServiceManager.CreateServiceAsync(dataServiceDescription, this.operationTimeout, this.serviceCancellationToken); // And finally, create the web service in the new application instance. ServiceUriBuilder webServiceNameUriBuilder = new ServiceUriBuilder(application.ApplicationName.ToString(), Names.TenantWebServiceName); StatelessServiceDescription webServiceDescription = new StatelessServiceDescription() { ApplicationName = application.ApplicationName, InstanceCount = parameters.WebInstanceCount, PartitionSchemeDescription = new SingletonPartitionSchemeDescription(), ServiceName = webServiceNameUriBuilder.Build(), ServiceTypeName = Names.TenantWebServiceTypeName }; await this.fabricClient.ServiceManager.CreateServiceAsync(webServiceDescription, this.operationTimeout, this.serviceCancellationToken); return this.Ok(); }
public async Task<IActionResult> GetDevicesAsync() { ServiceUriBuilder uriBuilder = new ServiceUriBuilder(TenantDataServiceName); Uri serviceUri = uriBuilder.Build(); // service may be partitioned. // this will aggregate device IDs from all partitions ServicePartitionList partitions = await this.fabricClient.QueryManager.GetPartitionListAsync(serviceUri); List<DeviceViewModel> deviceViewModels = new List<DeviceViewModel>(); foreach (Partition partition in partitions) { Uri getUrl = new HttpServiceUriBuilder() .SetServiceName(serviceUri) .SetPartitionKey(((Int64RangePartitionInformation) partition.PartitionInformation).LowKey) .SetServicePathAndQuery($"/api/devices") .Build(); HttpResponseMessage response = await this.httpClient.GetAsync(getUrl, this.serviceCancellationToken); if (response.StatusCode != System.Net.HttpStatusCode.OK) { return this.StatusCode((int) response.StatusCode); } JsonSerializer serializer = new JsonSerializer(); using (StreamReader streamReader = new StreamReader(await response.Content.ReadAsStreamAsync())) { using (JsonTextReader jsonReader = new JsonTextReader(streamReader)) { List<DeviceViewModel> result = serializer.Deserialize<List<DeviceViewModel>>(jsonReader); if (result != null) { deviceViewModels.AddRange(result); } } } } return this.Ok(deviceViewModels); }