// Copyright Epic Games, Inc. All Rights Reserved. #pragma once #include #include #include #include ZEN_THIRD_PARTY_INCLUDES_START #include ZEN_THIRD_PARTY_INCLUDES_END #include #include #include #include namespace zen { class SessionLogStore; class SessionLog; /** Session tracker * * Acts as a log and session info concentrator when dealing with multiple * servers and external processes acting as a group. */ class SessionsService { public: struct SessionInfo { Oid Id; std::string AppName; std::string Mode; Oid JobId; CbObject Metadata; DateTime CreatedAt; DateTime UpdatedAt; DateTime EndedAt{0}; }; struct LogEntry { DateTime Timestamp; std::string Level; std::string Message; CbObject Data; }; class Session : public TRefCounted { public: Session(const SessionInfo& Info); ~Session(); Session(Session&&) = delete; Session& operator=(Session&&) = delete; const SessionInfo& Info() const { return m_Info; } void UpdateMetadata(CbObjectView Metadata) { m_Info.Metadata = CbObject::Clone(Metadata); m_Info.UpdatedAt = DateTime::Now(); } void SetEndedAt(DateTime When) { m_Info.EndedAt = When; } void AppendLog(LogEntry Entry); std::vector GetLogEntries(uint32_t Limit = 0, uint32_t Offset = 0) const; uint64_t GetLogCount() const; /// Returns entries appended after the given cursor and the new cursor value. /// A cursor of 0 returns all entries currently in the deque. struct CursorResult { std::vector Entries; uint64_t Cursor; // new cursor for next poll uint64_t Count; // current deque size }; CursorResult GetLogEntriesAfter(uint64_t AfterCursor) const; private: SessionInfo m_Info; Ref m_Log; mutable RwLock m_LogLock; std::deque m_LogEntries; uint64_t m_TotalAppended = 0; // monotonically increasing counter static constexpr uint32_t MaxLogEntries = 10000; }; SessionsService(); ~SessionsService(); bool RegisterSession(const Oid& SessionId, std::string AppName, std::string Mode, const Oid& JobId, CbObjectView Metadata); bool UpdateSession(const Oid& SessionId, CbObjectView Metadata); Ref GetSession(const Oid& SessionId) const; std::vector> GetSessions() const; std::vector> GetEndedSessions() const; bool RemoveSession(const Oid& SessionId); uint64_t GetSessionCount() const; private: LoggerRef& Log() { return m_Log; } LoggerRef m_Log; mutable RwLock m_Lock; tsl::robin_map, Oid::Hasher> m_Sessions; std::vector> m_EndedSessions; std::unique_ptr m_SessionLogs; }; } // namespace zen