private string hashtag; // keep note of the hashtag we are tracking public override async Task OnActivateAsync() { this.GetPrimaryKey(out hashtag); this.State.Hashtag = hashtag; // if this is our first ever activation, let the Counter Grain know if (!this.State.BeenCounted) { // record that the grain has now been counted, and store the state this.State.BeenCounted = true; var counter = CounterFactory.GetGrain(0); await Task.WhenAll(counter.IncrementCounter(), this.State.WriteStateAsync()); } await base.OnActivateAsync(); }
/// <summary> /// Get the score for a set of comma separated hashtags /// </summary> /// <param name="hashtags"></param> /// <returns></returns> public async Task <ActionResult> GetScores(string hashtags) { // get a handle the to dispatcher grain var tweetGrain = TweetDispatcherGrainFactory.GetGrain(0); // get the scores for the hashtags var tweetGrainTask = tweetGrain.GetTotals(hashtags.ToLower().Split(',')); // get a handle the to counter grain var counterGrain = CounterFactory.GetGrain(0); // get the total number of hashtag activations var counterGrainTask = counterGrain.GetTotalCounter(); // wait for tasks to complete await Task.WhenAll(tweetGrainTask, counterGrainTask); // return the json return(Json(new object[] { tweetGrainTask.Result, counterGrainTask.Result }, JsonRequestBehavior.AllowGet)); }