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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
|
// Copyright Epic Games, Inc. All Rights Reserved.
#include "sessions.h"
#include <zencore/basicfile.h>
#include <zencore/fmtutils.h>
#include <zencore/logging.h>
namespace zen {
using namespace std::literals;
class SessionLog : public TRefCounted<SessionLog>
{
public:
SessionLog(std::filesystem::path LogFilePath) { m_LogFile.Open(LogFilePath, BasicFile::Mode::kWrite); }
private:
BasicFile m_LogFile;
};
class SessionLogStore
{
public:
SessionLogStore(std::filesystem::path StoragePath) : m_StoragePath(std::move(StoragePath)) {}
~SessionLogStore() = default;
Ref<SessionLog> GetLogForSession(const Oid& SessionId)
{
// For now, just return a new log for each session. We can implement actual log storage and retrieval later.
return Ref(new SessionLog(m_StoragePath / (SessionId.ToString() + ".log")));
}
Ref<SessionLog> CreateLogForSession(const Oid& SessionId)
{
// For now, just return a new log for each session. We can implement actual log storage and retrieval later.
return Ref(new SessionLog(m_StoragePath / (SessionId.ToString() + ".log")));
}
private:
std::filesystem::path m_StoragePath;
};
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));
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::SessionsService() : m_Log(logging::Get("sessions"))
{
}
SessionsService::~SessionsService() = default;
bool
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))
{
return false;
}
ZEN_INFO("Session {} registered (AppName: {}, Mode: {}, JobId: {})", SessionId, AppName, Mode, JobId);
const DateTime Now = DateTime::Now();
m_Sessions.emplace(SessionId,
Ref(new Session(SessionInfo{.Id = SessionId,
.AppName = std::move(AppName),
.Mode = std::move(Mode),
.JobId = JobId,
.Metadata = CbObject::Clone(Metadata),
.CreatedAt = Now,
.UpdatedAt = Now})));
return true;
}
bool
SessionsService::UpdateSession(const Oid& SessionId, CbObjectView Metadata)
{
RwLock::ExclusiveLockScope Lock(m_Lock);
auto It = m_Sessions.find(SessionId);
if (It == m_Sessions.end())
{
return false;
}
It.value()->UpdateMetadata(Metadata);
const SessionInfo& Info = It.value()->Info();
ZEN_DEBUG("Session {} updated (AppName: {}, JobId: {})", SessionId, Info.AppName, Info.JobId);
return true;
}
Ref<SessionsService::Session>
SessionsService::GetSession(const Oid& SessionId) const
{
RwLock::SharedLockScope Lock(m_Lock);
auto It = m_Sessions.find(SessionId);
if (It == m_Sessions.end())
{
return {};
}
return It->second;
}
std::vector<Ref<SessionsService::Session>>
SessionsService::GetSessions() const
{
RwLock::SharedLockScope Lock(m_Lock);
std::vector<Ref<Session>> Result;
Result.reserve(m_Sessions.size());
for (const auto& [Id, SessionRef] : m_Sessions)
{
Result.push_back(SessionRef);
}
return Result;
}
bool
SessionsService::RemoveSession(const Oid& SessionId)
{
RwLock::ExclusiveLockScope Lock(m_Lock);
auto It = m_Sessions.find(SessionId);
if (It == m_Sessions.end())
{
return false;
}
ZEN_INFO("Session {} removed (AppName: {}, JobId: {})", SessionId, It.value()->Info().AppName, It.value()->Info().JobId);
Ref<Session> Ended = It.value();
Ended->SetEndedAt(DateTime::Now());
m_EndedSessions.push_back(std::move(Ended));
m_Sessions.erase(It);
return true;
}
std::vector<Ref<SessionsService::Session>>
SessionsService::GetEndedSessions() const
{
RwLock::SharedLockScope Lock(m_Lock);
return m_EndedSessions;
}
uint64_t
SessionsService::GetSessionCount() const
{
RwLock::SharedLockScope Lock(m_Lock);
return m_Sessions.size();
}
} // namespace zen
|