blob: 8f07bfc31f1ab5b15135cae064cea1b06e3690c5 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
|
// Copyright Epic Games, Inc. All Rights Reserved.
#pragma once
#include <zencore/compactbinary.h>
#include <zencore/logbase.h>
#include <zencore/thread.h>
#include <zencore/uid.h>
ZEN_THIRD_PARTY_INCLUDES_START
#include <tsl/robin_map.h>
ZEN_THIRD_PARTY_INCLUDES_END
#include <deque>
#include <optional>
#include <string>
#include <vector>
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<Session>
{
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<LogEntry> 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<LogEntry> 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<SessionLog> m_Log;
mutable RwLock m_LogLock;
std::deque<LogEntry> 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<Session> GetSession(const Oid& SessionId) const;
std::vector<Ref<Session>> GetSessions() const;
std::vector<Ref<Session>> 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, Ref<Session>, Oid::Hasher> m_Sessions;
std::vector<Ref<Session>> m_EndedSessions;
std::unique_ptr<SessionLogStore> m_SessionLogs;
};
} // namespace zen
|