aboutsummaryrefslogtreecommitdiff
path: root/src/zenserver/sessions/sessions.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/zenserver/sessions/sessions.cpp')
-rw-r--r--src/zenserver/sessions/sessions.cpp141
1 files changed, 119 insertions, 22 deletions
diff --git a/src/zenserver/sessions/sessions.cpp b/src/zenserver/sessions/sessions.cpp
index d919db6e9..1212ba5d8 100644
--- a/src/zenserver/sessions/sessions.cpp
+++ b/src/zenserver/sessions/sessions.cpp
@@ -46,6 +46,78 @@ SessionsService::Session::Session(const SessionInfo& Info) : m_Info(Info)
}
SessionsService::Session::~Session() = default;
+void
+SessionsService::Session::AppendLog(LogEntry Entry)
+{
+ RwLock::ExclusiveLockScope Lock(m_LogLock);
+ m_LogEntries.push_back(std::move(Entry));
+ ++m_TotalAppended;
+ while (m_LogEntries.size() > MaxLogEntries)
+ {
+ m_LogEntries.pop_front();
+ }
+}
+
+std::vector<SessionsService::LogEntry>
+SessionsService::Session::GetLogEntries(uint32_t Limit, uint32_t Offset) const
+{
+ RwLock::SharedLockScope Lock(m_LogLock);
+
+ const uint32_t Total = uint32_t(m_LogEntries.size());
+ if (Offset >= Total)
+ {
+ return {};
+ }
+
+ const uint32_t Available = Total - Offset;
+ const uint32_t Count = (Limit > 0) ? std::min(Limit, Available) : Available;
+
+ std::vector<LogEntry> Result;
+ Result.reserve(Count);
+ for (uint32_t i = Offset; i < Offset + Count; i++)
+ {
+ Result.push_back(m_LogEntries[i]);
+ }
+ return Result;
+}
+
+uint64_t
+SessionsService::Session::GetLogCount() const
+{
+ RwLock::SharedLockScope Lock(m_LogLock);
+ return m_LogEntries.size();
+}
+
+SessionsService::Session::CursorResult
+SessionsService::Session::GetLogEntriesAfter(uint64_t AfterCursor) const
+{
+ RwLock::SharedLockScope Lock(m_LogLock);
+
+ const uint64_t DequeSize = m_LogEntries.size();
+
+ // Cursor 0 means "give me everything currently in the deque".
+ // Otherwise, compute how many new entries were appended since the cursor.
+ uint64_t NewCount = (AfterCursor == 0) ? DequeSize : (m_TotalAppended > AfterCursor ? m_TotalAppended - AfterCursor : 0);
+
+ // Clamp to what's actually available in the deque (entries may have been evicted).
+ NewCount = std::min(NewCount, DequeSize);
+
+ std::vector<LogEntry> Result;
+ Result.reserve(NewCount);
+
+ const uint64_t StartIndex = DequeSize - NewCount;
+ for (uint64_t i = StartIndex; i < DequeSize; i++)
+ {
+ Result.push_back(m_LogEntries[i]);
+ }
+
+ return CursorResult{
+ .Entries = std::move(Result),
+ .Cursor = m_TotalAppended,
+ .Count = DequeSize,
+ };
+}
+
//////////////////////////////////////////////////////////////////////////
SessionsService::SessionsService() : m_Log(logging::Get("sessions"))
@@ -55,25 +127,31 @@ SessionsService::SessionsService() : m_Log(logging::Get("sessions"))
SessionsService::~SessionsService() = default;
bool
-SessionsService::RegisterSession(const Oid& SessionId, std::string AppName, const Oid& JobId, CbObjectView Metadata)
+SessionsService::RegisterSession(const Oid& SessionId, std::string AppName, std::string Mode, const Oid& JobId, CbObjectView Metadata)
{
- RwLock::ExclusiveLockScope Lock(m_Lock);
-
- if (m_Sessions.contains(SessionId))
+ // Log outside the lock scope — InProcSessionLogSink calls back into
+ // GetSession() which acquires m_Lock shared, so logging while holding
+ // m_Lock exclusively would deadlock.
{
- return false;
+ RwLock::ExclusiveLockScope Lock(m_Lock);
+
+ if (m_Sessions.contains(SessionId))
+ {
+ return false;
+ }
+
+ const DateTime Now = DateTime::Now();
+ m_Sessions.emplace(SessionId,
+ Ref(new Session(SessionInfo{.Id = SessionId,
+ .AppName = AppName,
+ .Mode = Mode,
+ .JobId = JobId,
+ .Metadata = CbObject::Clone(Metadata),
+ .CreatedAt = Now,
+ .UpdatedAt = Now})));
}
- ZEN_INFO("Session {} registered (AppName: {}, JobId: {})", SessionId, AppName, JobId);
-
- const DateTime Now = DateTime::Now();
- m_Sessions.emplace(SessionId,
- Ref(new Session(SessionInfo{.Id = SessionId,
- .AppName = std::move(AppName),
- .JobId = JobId,
- .Metadata = CbObject::Clone(Metadata),
- .CreatedAt = Now,
- .UpdatedAt = Now})));
+ ZEN_INFO("Session {} registered (AppName: {}, Mode: {}, JobId: {})", SessionId, AppName, Mode, JobId);
return true;
}
@@ -126,20 +204,39 @@ SessionsService::GetSessions() const
bool
SessionsService::RemoveSession(const Oid& SessionId)
{
- RwLock::ExclusiveLockScope Lock(m_Lock);
+ std::string RemovedAppName;
+ Oid RemovedJobId;
- auto It = m_Sessions.find(SessionId);
- if (It == m_Sessions.end())
{
- return false;
- }
+ RwLock::ExclusiveLockScope Lock(m_Lock);
+
+ auto It = m_Sessions.find(SessionId);
+ if (It == m_Sessions.end())
+ {
+ return false;
+ }
+
+ RemovedAppName = It.value()->Info().AppName;
+ RemovedJobId = It.value()->Info().JobId;
+
+ Ref<Session> Ended = It.value();
+ Ended->SetEndedAt(DateTime::Now());
+ m_EndedSessions.push_back(std::move(Ended));
- ZEN_INFO("Session {} removed (AppName: {}, JobId: {})", SessionId, It.value()->Info().AppName, It.value()->Info().JobId);
+ m_Sessions.erase(It);
+ }
- m_Sessions.erase(It);
+ ZEN_INFO("Session {} removed (AppName: {}, JobId: {})", SessionId, RemovedAppName, RemovedJobId);
return true;
}
+std::vector<Ref<SessionsService::Session>>
+SessionsService::GetEndedSessions() const
+{
+ RwLock::SharedLockScope Lock(m_Lock);
+ return m_EndedSessions;
+}
+
uint64_t
SessionsService::GetSessionCount() const
{