Ejemplo n.º 1
0
        internal Task LoadAsync()
        {
            return(Task.Run(async() =>
            {
                try
                {
                    await _log.LogInformation(WebApiConfig.LogSourceSessions, "Service starting...");

                    var ps = 1000;
                    var pn = 1;
                    int count;
                    var expiredSessions = new List <Session>();

                    do
                    {
                        count = 0;
                        var sessions = await _repo.SelectSessions(ps, pn);

                        foreach (var session in sessions)
                        {
                            ++count;

                            if (session.IsExpired())
                            {
                                expiredSessions.Add(session);
                            }
                            else
                            {
                                InsertSession(session);
                            }
                        }

                        ++pn;
                    }while (count == ps);

                    // Mark expired sessions as trully expired in the DB
                    foreach (var session in expiredSessions)
                    {
                        await _repo.EndSession(session.SessionId, session.EndTime);
                    }
                    var logExpiredSessionsCount = expiredSessions.Count > 0 ? $" {expiredSessions.Count} sessions expired" : string.Empty;

                    await _log.LogInformation(WebApiConfig.LogSourceSessions, $"Service started.{logExpiredSessionsCount}");
                }
                catch (Exception ex)
                {
                    await _log.LogError(WebApiConfig.LogSourceSessions,
                                        new Exception("Error loading sessions from database.", ex));
                }
            }));
        }