Exemplo n.º 1
0
        public async Task <IHttpActionResult> CancelOrder(string id)
        {
            var error   = "";
            var handler = HandlersFactory.GetProfilerHandler(TheSettingService, TheLoggerService);

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

            try
            {
                IActorLocationService locator    = ServiceFactory.GetInstance().GetActorLocationService();
                ITicketOrderActor     orderActor = locator.Create <ITicketOrderActor>(new ActorId(id), Constants.ContosoEventsApplicationName);
                await orderActor.CancelOrder();

                return(Ok("Ok"));
            }
            catch (Exception ex)
            {
                error = ex.Message;
                return(BadRequest(ex.Message));
            }
            finally
            {
                handler.Stop(error);
            }
        }
        /// <summary>
        /// This is the main entry point for your service replica.
        /// This method executes when this replica of your service becomes primary and has write status.
        /// </summary>
        /// <param name="cancellationToken">Canceled when Service Fabric needs to shut down this service replica.</param>
        protected override async Task RunAsync(CancellationToken cancellationToken)
        {
            // Gets (or creates) a replicated queue called "OrderQueue" in this partition.
            var requests = await this.StateManager.GetOrAddAsync <IReliableQueue <TicketOrder> >(OrderQueueName);

            while (true)
            {
                cancellationToken.ThrowIfCancellationRequested();

                using (var tx = this.StateManager.CreateTransaction())
                {
                    var result = await requests.TryDequeueAsync(tx, TxTimeout, cancellationToken);

                    if (result.HasValue)
                    {
                        var error   = "";
                        var handler = HandlersFactory.GetProfilerHandler(SettingService, LoggerService);
                        handler.Start(LOG_TAG, "AcquiredQueueItem", GetServiceProperties());

                        try
                        {
                            TicketOrder order = result.Value;

                            IActorLocationService locator    = ServiceFactory.GetInstance().GetActorLocationService();
                            ITicketOrderActor     orderActor = locator.Create <ITicketOrderActor>(new ActorId(order.Id), Constants.ContosoEventsApplicationName);
                            await orderActor.ProcessOrder(order);
                        }
                        catch (Exception ex)
                        {
                            error = ex.Message;
                        }
                        finally
                        {
                            handler.Stop(error);
                            if (!string.IsNullOrEmpty(error))
                            {
                                this.HealthReporterService.SendReportForService(HealthState.Error, GetHealthErrorMessage("AcquiredQueueItem", error));
                            }
                        }

                        // This commits the dequeue operations.
                        // If the request to add the stock to the inventory service throws, this commit will not execute
                        // and the items will remain on the queue, so we can be sure that we didn't dequeue items
                        // that didn't get saved successfully in the inventory service.
                        // However there is a very small chance that the stock was added to the inventory service successfully,
                        // but service execution stopped before reaching this commit (machine crash, for example).
                        await tx.CommitAsync();
                    }

                    await Task.Delay(TimeSpan.FromMilliseconds(this.SettingService.GetStatefulServiceLoopPause()), cancellationToken);
                }
            }
        }
Exemplo n.º 3
0
        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);
            }
        }