private async Task CreateBandActorTask(BandActorGenerator bag, CancellationToken cancellationToken) { CryptoRandom random = new CryptoRandom(); while (!cancellationToken.IsCancellationRequested && this.MaxBandsToCreatePerServiceInstance > 0) { bool created = false; while (!created && !cancellationToken.IsCancellationRequested) { ActorId bandActorId; ActorId doctorActorId; int randomCountyId = -1; string doctorName = null; randomCountyId = random.Next(0, bag.doctorsPerCounty.Keys.Count); doctorName = bag.GetRandomName(random); CountyRecord randomCountyRecord = bag.doctorsPerCounty.Keys.ElementAt(randomCountyId); BandInfo bandActorInfo = bag.GetRandomHealthStatus(randomCountyRecord, random); try { bandActorId = new ActorId(Guid.NewGuid()); doctorActorId = new ActorId(bandActorInfo.DoctorId); IDoctorActor docActor = ActorProxy.Create <IDoctorActor>(doctorActorId, this.DoctorServiceUri); await docActor.NewAsync(doctorName, randomCountyRecord); IBandActor bandActor = ActorProxy.Create <IBandActor>(bandActorId, this.ActorServiceUri); await bandActor.NewAsync(bandActorInfo); ServiceEventSource.Current.Message("Actor created {0} verifying...", bandActorId); await VerifyActors( new HealthIndexCalculator(this.Context), bandActorId, doctorName, randomCountyRecord, bandActorInfo, docActor, bandActor, cancellationToken); } catch (Exception e) { ServiceEventSource.Current.ServiceMessage(this, "Failed to iniitalize band or doctor. {0}", e.ToString()); } created = true; } this.MaxBandsToCreatePerServiceInstance--; ServiceEventSource.Current.ServiceMessage(this, "Created Actors, {0} remaining", this.MaxBandsToCreatePerServiceInstance); await Task.Delay(100, cancellationToken); } }
private async Task CreateKnownActors(BandActorGenerator bag, ConfigurationSettings settings, CancellationToken cancellationToken, bool verify) { CryptoRandom random = new CryptoRandom(); FabricClient fc = new FabricClient(); HealthIndexCalculator hic = new HealthIndexCalculator(this.Context); KeyedCollection <string, ConfigurationProperty> serviceParameters = settings.Sections["HealthMetrics.BandCreationService.Settings"].Parameters; while (!cancellationToken.IsCancellationRequested) { ActorId bandActorId; ActorId doctorActorId; int randomCountyId = -1; string doctorName = null; randomCountyId = int.Parse(serviceParameters["KnownCountyIdIndex"].Value); //(2968 is King, WA) || (2231 is Multnomah, OR) || (1870 is St. Lawrence, NY) doctorName = serviceParameters["KnownDoctorName"].Value; CountyRecord randomCountyRecord = bag.doctorsPerCounty.Keys.ElementAt(randomCountyId); BandInfo bandActorInfo = bag.GetRandomHealthStatus(randomCountyRecord, random); try { bandActorInfo.PersonName = serviceParameters["KnownPatientName"].Value; bandActorId = new ActorId(new Guid(serviceParameters["KnownPatientId"].Value)); bandActorInfo.DoctorId = new Guid(serviceParameters["KnownDoctorId"].Value); doctorActorId = new ActorId(bandActorInfo.DoctorId); bag.doctorsPerCounty[bag.doctorsPerCounty.Keys.ElementAt(randomCountyId)].Add(bandActorInfo.DoctorId); IDoctorActor docActor = ActorProxy.Create <IDoctorActor>(doctorActorId, this.DoctorServiceUri); await docActor.NewAsync(doctorName, randomCountyRecord); IBandActor bandActor = ActorProxy.Create <IBandActor>(bandActorId, this.ActorServiceUri); await bandActor.NewAsync(bandActorInfo); if (verify) { await VerifyActors(hic, bandActorId, doctorName, randomCountyRecord, bandActorInfo, docActor, bandActor); break; } await Task.Delay(TimeSpan.FromMinutes(1), cancellationToken); } catch (Exception e) { ServiceEventSource.Current.ServiceMessage(this, "Exception when creating actor {0}", e.ToString()); } } }
protected override async Task RunAsync(CancellationToken cancellationToken) { ConfigurationSettings configSettings = FabricRuntime.GetActivationContext().GetConfigurationPackageObject("Config").Settings; KeyedCollection <string, ConfigurationProperty> serviceParameters = configSettings.Sections["HealthMetrics.BandCreationService.Settings"].Parameters; this.NumberOfCreationThreads = int.Parse(serviceParameters["NumberOfCreationThreads"].Value); this.MaxBandsToCreatePerServiceInstance = int.Parse(serviceParameters["MaxBandsToCreatePerServiceInstance"].Value); this.ActorServiceUri = new ServiceUriBuilder(serviceParameters["BandActorServiceName"].Value).ToUri(); this.DoctorServiceUri = new ServiceUriBuilder(serviceParameters["DoctorActorServiceName"].Value).ToUri(); this.GenerateKnownPeople = bool.Parse(serviceParameters["GenerateKnownPeople"].Value); string dataPath = FabricRuntime.GetActivationContext().GetDataPackageObject("Data").Path; BandActorGenerator bag = new BandActorGenerator(configSettings, dataPath); bag.Prepare(); ServicePrimer primer = new ServicePrimer(); await primer.WaitForStatefulService(this.ActorServiceUri, cancellationToken); List <Task> tasks = new List <Task>(); if (this.GenerateKnownPeople) { bool verify = bool.Parse(serviceParameters["VerifyKnownPeople"].Value); tasks.Add(Task.Run(() => this.CreateKnownActors(bag, configSettings, cancellationToken, verify))); } for (int i = 0; i < this.NumberOfCreationThreads; i++) { tasks.Add(Task.Run(() => this.CreateBandActorTask(bag, cancellationToken), cancellationToken)); } ServiceEventSource.Current.ServiceMessage(this, "Band Creation has begun."); await Task.WhenAll(tasks); ServiceEventSource.Current.ServiceMessage(this, "Band Creation has completed."); }