Пример #1
0
        public async Task ProcessEventsAsync(PartitionContext context, IEnumerable<EventData> messages)
        {
            var client = await InitTopic();

            foreach (EventData eventData in messages)
            {
                await Task.Delay(_rand.Next(100, 2000));        // spice of life

                var json = Encoding.UTF8.GetString(eventData.GetBytes());

                Trace.TraceInformation(string.Format("Message received.  Partition: '{0}', Data: '{1}'", context.Lease.PartitionId, json));

                var request = JsonConvert.DeserializeObject<PurchaseRequest>(json);

                var result = new PurchaseResult
                {
                    Timestamp = DateTimeOffset.UtcNow,
                    Request = request,
                    Id = Guid.NewGuid()
                };

                try
                {
                    await _repos.AddPurchasedTicketsAsync(request);
                    result.Success = true;
                }
                catch(Exception ex)
                {
                    result.Success = false;
                    result.ExceptionJson = JsonConvert.SerializeObject(ex);
                    result.FailureReason = PurchaseFailureReason.ArtistInRehab;
                }

                var topicMsg = new BrokeredMessage();

                topicMsg.Properties["json"] = JsonConvert.SerializeObject(result);
                topicMsg.CorrelationId = (string) eventData.Properties["correlationId"];

                await client.SendAsync(topicMsg);
            }

            if (_checkpointStopWatch.Elapsed > TimeSpan.FromMinutes(5))
            {
                await context.CheckpointAsync();

                lock (this)
                {
                    _checkpointStopWatch.Reset();
                }
            }
        }
Пример #2
0
        public static void SendResult(PurchaseResult result, string connectionId)
        {
            var hubContext = GlobalHost.ConnectionManager.GetHubContext<MainHub>();

            if(result.Success)
            {
                hubContext.Clients.Client(connectionId).orderSucceeded(new { id = result.Request.Id });
            }
            else
            {
                hubContext.Clients.Client(connectionId).orderFailed(new { id = result.Request.Id });
            }

            var summary = new
            {
                id = result.Id,
                requested = result.Request.Timestamp.ToString("u"),
                processed = result.Timestamp.ToString("u"),
                success = result.Success.ToString(),
                ticketcount = result.Request.DesiredTickets.Length
            };

            hubContext.Clients.All.requestProcessed(summary);
        }