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); } } }
public async Task <IHttpActionResult> GetEntityView(EntityTypes type, int businesskey) { var error = ""; var handler = HandlersFactory.GetProfilerHandler(TheSettingService); handler.Start(LOG_TAG, "GetEntityView", GetServiceProperties()); try { IActorLocationService actorLocator = ServiceFactory.GetActorLocationService(); IEntityActor entityActor = actorLocator.Create <IEntityActor>(new Entity(type, businesskey).GetPartitionKey(), Constants.ApplicationName); var entity = await entityActor.GetEntity(); var actorView = await entityActor.GetView(); var parent = entity.Parent; var parentView = new EntityView(); if (parent != null) { IEntityActor parentActor = actorLocator.Create <IEntityActor>(new Entity(parent.Type, parent.BusinessKey).GetPartitionKey(), Constants.ApplicationName); parentView = await parentActor.GetView(); } var children = await entityActor.GetChildren(); Dictionary <string, EntityView> childrenViews = new Dictionary <string, EntityView>(); foreach (var child in children) { IEntityActor childActor = actorLocator.Create <IEntityActor>(new Entity(child.Type, child.BusinessKey).GetPartitionKey(), Constants.ApplicationName); var childView = await childActor.GetView(); childrenViews.Add(child.Name, childView); } return(Ok(new { ParentName = parent != null ? parent.Name : "", ParentView = parent != null ? parentView : null, ThisView = actorView, ChildrenViews = childrenViews })); } catch (Exception ex) { error = ex.Message; return(BadRequest(ex.Message)); } finally { handler.Stop(error); } }
public async Task <IHttpActionResult> GetHistricalOrdersByEventId(string eventid) { var error = ""; var handler = HandlersFactory.GetProfilerHandler(TheSettingService, TheLoggerService); handler.Start(LOG_TAG, "GetEventStatsById", GetServiceProperties()); try { IActorLocationService locator = ServiceFactory.GetInstance().GetActorLocationService(); IEventActor eventActor = locator.Create <IEventActor>(new ActorId(eventid), Constants.ContosoEventsApplicationName); return(Ok(await eventActor.GetHistoricalOrders())); } 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 dictionary called "myDictionary" in this partition. var requests = await this.StateManager.GetOrAddAsync <IReliableQueue <EntityTransaction> >(ProcessQueueName); try { // This partition's replica continues processing until the replica is terminated. while (!cancellationToken.IsCancellationRequested) { using (var tx = this.StateManager.CreateTransaction()) { var result = await requests.TryDequeueAsync(tx, TxTimeout, cancellationToken); if (result.HasValue) { var error = ""; var handler = HandlersFactory.GetProfilerHandler(SettingService); handler.Start(LOG_TAG, "AcquiredQueueItem", GetServiceProperties()); try { EntityTransaction transaction = result.Value; handler.Info("Acquired item business key: " + transaction.BusinessKey + " - entity type: " + transaction.EntityType); IActorLocationService locator = ServiceFactory.GetActorLocationService(); IEntityActor entityActor = locator.Create <IEntityActor>(new Entity(transaction.EntityType, transaction.BusinessKey).GetPartitionKey(), Constants.ApplicationName); await entityActor.SubscribeAsync <IEntityActorEvents>(this); await entityActor.Process(transaction); } catch (Exception ex) { error = ex.Message; } finally { handler.Stop(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(); } // Pause for 2 second before continue processing. await Task.Delay(TimeSpan.FromSeconds(2), cancellationToken); } } catch (Exception ex) { } // Pause for 1 second before continue processing. await Task.Delay(TimeSpan.FromSeconds(1), cancellationToken); }
public EnqueuerService(StatefulServiceContext context) : base(context) { this.SettingService = ServiceFactory.GetSettingService(); this.ActorLocationService = ServiceFactory.GetActorLocationService(); }