public async Task CloseSession()
        {
            // Arrange
            var sessionCount = await _context.SessionCounts.SingleAsync();

            sessionCount.NumClose = 0;
            sessionCount.NumOpen  = 1;
            await _context.Sessions.AddAsync(_existingSession);

            await _context.SaveChangesAsync();

            var handler = new Service.Handlers.CloseSessionHandler(_context, _mapper);
            var message = new CloseSession {
                SessionId = _existingSession.Id
            };

            // Act
            await handler.Handle(message, new TestsMessageHandlerContext());

            // Assert
            var refreshedContext = Common.GetDbContext(_connectionString);
            var session          = await refreshedContext.Sessions.SingleAsync();

            sessionCount = await refreshedContext.SessionCounts.SingleAsync();

            Assert.Equal(message.SessionId, session.Id);
            Assert.Equal(0, session.Status);
            Assert.Equal(0, sessionCount.NumOpen);
            Assert.Equal(1, sessionCount.NumClose);
        }
Ejemplo n.º 2
0
        public async Task Handle(RefreshCalculations message, IMessageHandlerContext context)
        {
            Log.Info($"RefreshCalculations. ProcessId: '{Process.GetCurrentProcess().Id}'");

            var currentJob = new WorkerJob()
            {
                ProcessId = Process.GetCurrentProcess().Id, StartDate = DateTime.UtcNow
            };

            var lastJob = await _db.WorkerJobs.OrderByDescending(o => o.EndDate).FirstOrDefaultAsync();

            var lastJobEndDate = lastJob?.EndDate ?? DateTime.MinValue;

            var checkpoint = DateTime.UtcNow;

            var unprocessedSessionWords = await _db.SessionWords.Where(w => w.DateCreated > lastJobEndDate).ToListAsync();

            Log.Info($"{unprocessedSessionWords.Count} new words found.");

            await CalculateWordsPerSession(unprocessedSessionWords);

            await CalculateWordFrequency(unprocessedSessionWords);

            Log.Info($"Saving checkpoint. Next time will process words from date '{checkpoint:s}'.");
            currentJob.EndDate = DateTime.UtcNow;
            await _db.WorkerJobs.AddAsync(currentJob);

            await _db.SaveChangesAsync();
        }
Ejemplo n.º 3
0
        public async Task Handle(LogRequest message, IMessageHandlerContext context)
        {
            Log.Info($"LogRequest. RequestId: '{message.Id}', ProcessId: '{Process.GetCurrentProcess().Id}'");

            var requestSaved = false;

            var sessionCounters = await this._db.RequestCounts.SingleAsync();

            sessionCounters.Count += 1;

            while (!requestSaved)
            {
                try
                {
                    await _db.SaveChangesAsync();

                    requestSaved = true;
                }
                catch (Exception ex) when(ex.IsConcurrencyException())
                {
                    var dbUpdateException = (DbUpdateException)ex;
                    var entry             = dbUpdateException.Entries.First();
                    var proposedValues    = entry.CurrentValues;
                    var databaseValues    = entry.GetDatabaseValues();

                    proposedValues["Count"] = (int)databaseValues["Count"] + 1;
                    entry.OriginalValues.SetValues(databaseValues);
                }
            }
        }
Ejemplo n.º 4
0
        public async Task Handle(SubmitWords message, IMessageHandlerContext context)
        {
            Log.Info($"SubmitWords. SessionId '{message.SessionId}', ProcessId: '{Process.GetCurrentProcess().Id}'");

            var session = await _db.Sessions.SingleOrDefaultAsync(s => s.Id == message.SessionId);

            if (session == null)
            {
                return;
            }

            if (session.Status == 0)
            {
                return;
            }

            var sessionWords = new List <SessionWord>();

            sessionWords.AddRange(message.Words.Select(word => new SessionWord
            {
                SessionId = message.SessionId,
                Word      = word
            }));

            await _db.SessionWords.AddRangeAsync(sessionWords);

            await _db.SaveChangesAsync();
        }
Ejemplo n.º 5
0
        private async Task UpdateCounters()
        {
            var counterSaved    = false;
            var sessionCounters = await this._db.SessionCounts.SingleAsync();

            sessionCounters.NumOpen += 1;

            while (!counterSaved)
            {
                try
                {
                    await _db.SaveChangesAsync();

                    counterSaved = true;
                }
                catch (Exception ex) when(ex.IsConcurrencyException())
                {
                    ResolveCounterConflicts(ex);
                }
            }
        }