public async Task <IHttpActionResult> UpdateServiceHealthState(UpdateHealthRequest request)
        {
            var error   = "";
            var handler = HandlersFactory.GetProfilerHandler(TheSettingService, TheLoggerService);

            handler.Start(LOG_TAG, "UpdateServiceHealthState", GetServiceProperties());

            try
            {
                UpdateHealthRequest.Validate(request);

                int ticketOrderServices           = 0;
                int ticketOrderActors             = 0;
                int eventActors                   = 0;
                ServiceLocationService locator    = new ServiceLocationService();
                UriBuilderService      builder    = new UriBuilderService(Constants.ContosoEventsApplicationInstance, Constants.ContosoEventsTicketOrderServiceName);
                ServicePartitionList   partitions = await _fabricClient.QueryManager.GetPartitionListAsync(builder.ToUri());

                foreach (Partition p in partitions)
                {
                    long minKey = (p.PartitionInformation as Int64RangePartitionInformation).LowKey;
                    ITicketOrderService dispenderService = locator.Create <ITicketOrderService>(builder.ToUri());
                    await dispenderService.UpdateHealthState(GetHealthStateFromString(request.State), request.Message);

                    ticketOrderServices++;
                }

                ActorLocationService actorLocator         = new ActorLocationService();
                UriBuilderService    orderActorBuilder    = new UriBuilderService(Constants.ContosoEventsApplicationInstance, Constants.ContosoEventsTicketOrderActorName);
                ServicePartitionList orderActorPartitions = await _fabricClient.QueryManager.GetPartitionListAsync(orderActorBuilder.ToUri());

                foreach (Partition p in orderActorPartitions)
                {
                    string            minKey = (p.PartitionInformation as Int64RangePartitionInformation).Id.ToString();
                    ITicketOrderActor actor  = actorLocator.Create <ITicketOrderActor>(new ActorId(minKey), Constants.ContosoEventsApplicationName);
                    await actor.UpdateHealthState(GetHealthStateFromString(request.State), request.Message);

                    ticketOrderActors++;
                    // May require contiunuation
                }

                IDataStoreService  dataService = ServiceFactory.GetInstance().GetDataStoreService(TheSettingService, TheLoggerService);
                List <TicketEvent> events      = await dataService.GetEvents();

                UriBuilderService eventActorBuilder = new UriBuilderService(Constants.ContosoEventsApplicationInstance, Constants.ContosoEventsEventActorName);
                foreach (var tEvent in events)
                {
                    IEventActor actor = actorLocator.Create <IEventActor>(new ActorId(tEvent.Id), Constants.ContosoEventsApplicationName);
                    await actor.UpdateHealthState(GetHealthStateFromString(request.State), request.Message);

                    eventActors++;
                    // May require contiunuation
                }

                return(Ok("Done: " + ticketOrderServices + "|" + ticketOrderActors + "|" + eventActors));
            }
            catch (Exception ex)
            {
                error = ex.Message;
                return(BadRequest(ex.Message));
            }
            finally
            {
                handler.Stop(error);
            }
        }
Exemple #2
0
        // IRemindable Interface Implementation
        public async Task ReceiveReminderAsync(string reminderName, byte[] context, TimeSpan dueTime, TimeSpan period)
        {
            var error   = "";
            var handler = HandlersFactory.GetProfilerHandler(SettingService);

            handler.Start(LOG_TAG, "ReceiveReminderAsync", GetActorProperties());

            try
            {
                handler.Info("Reminder " + reminderName);
                var state = await this.StateManager.GetStateAsync <EntityActorState>(ActorStatePropertyName);

                EntityTransaction transaction = (EntityTransaction)ByteArrayToObject(context);
                if (transaction == null)
                {
                    handler.Info("Transaction is null");
                }

                switch (reminderName)
                {
                case ReprocessReminder:
                {
                    // Unregister the reminder so the actor will be garbage collected
                    IActorReminder reminder = this.GetReminder(ReprocessReminder);
                    await this.UnregisterReminderAsync(reminder);

                    // Process this transaction
                    if (transaction.TransactionType == TransactionTypes.Purchase)
                    {
                        state.Purchases++;
                        state.SoldItems += transaction.SoldItems;
                        state.Revenue   += transaction.Revenue;
                        state.Tax       += transaction.Tax;
                        state.Shipping  += transaction.Shipping;
                    }
                    else if (transaction.TransactionType == TransactionTypes.Cancellation)
                    {
                        state.Cancellations++;
                        state.SoldItems -= transaction.SoldItems;
                        state.Revenue   -= transaction.Revenue;
                        state.Tax       -= transaction.Tax;
                        state.Shipping  -= transaction.Shipping;
                    }

                    // Make sure the state is saved
                    await SetEntityStateAsync(state);

                    // Publish an event that we are done processing
                    // Right now it is useless
                    // It is an excercise to see how events work
                    var ev = GetEvent <IEntityActorEvents>();
                    ev.MeasuresRecalculated(await GetEntity(), state.Purchases, state.Cancellations, state.SoldItems, state.Revenue, state.Tax, state.Shipping);

                    // If available, process the parent
                    var parent = await GetParent();

                    if (parent != null)
                    {
                        handler.Info("Parent actor type " + parent.Type);
                        IEntityActor parentActor = ActorLocationService.Create <IEntityActor>(parent.GetPartitionKey(), ApplicationName);
                        await parentActor.Process(transaction);
                    }

                    return;
                }

                default:
                {
                    // We should never arrive here normally. The system won't call reminders that don't exist.
                    // But for our own sake in case we add a new reminder somewhere and forget to handle it, this will remind us.
                    handler.Info("Unknown reminder: " + reminderName);
                    return;
                }
                }
            }
            catch (Exception ex)
            {
                error = ex.Message;
            }
            finally
            {
                handler.Stop(error);
            }
        }