step 4
This commit is contained in:
@@ -18,12 +18,20 @@ PersistenceWriter::~PersistenceWriter()
|
||||
StopAndFlush();
|
||||
}
|
||||
|
||||
bool PersistenceWriter::WriteSnapshot(const PersistenceSnapshot& snapshot, std::string& error) const
|
||||
void PersistenceWriter::SetResultCallback(ResultCallback callback)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mMutex);
|
||||
mResultCallback = std::move(callback);
|
||||
}
|
||||
|
||||
bool PersistenceWriter::WriteSnapshot(const PersistenceSnapshot& snapshot, std::string& error)
|
||||
{
|
||||
if (!ValidateSnapshot(snapshot, error))
|
||||
return false;
|
||||
|
||||
return WriteSnapshotThroughSink(snapshot, error);
|
||||
const bool succeeded = WriteSnapshotThroughSink(snapshot, error);
|
||||
PublishWriteResult(snapshot, succeeded, error, false);
|
||||
return succeeded;
|
||||
}
|
||||
|
||||
bool PersistenceWriter::EnqueueSnapshot(const PersistenceSnapshot& snapshot, std::string& error)
|
||||
@@ -137,6 +145,27 @@ bool PersistenceWriter::WriteSnapshotThroughSink(const PersistenceSnapshot& snap
|
||||
return true;
|
||||
}
|
||||
|
||||
void PersistenceWriter::PublishWriteResult(const PersistenceSnapshot& snapshot, bool succeeded, const std::string& errorMessage, bool newerRequestPending)
|
||||
{
|
||||
ResultCallback callback;
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mMutex);
|
||||
callback = mResultCallback;
|
||||
}
|
||||
|
||||
if (!callback)
|
||||
return;
|
||||
|
||||
PersistenceWriteResult result;
|
||||
result.targetKind = snapshot.targetKind;
|
||||
result.targetPath = snapshot.targetPath.string();
|
||||
result.reason = snapshot.reason;
|
||||
result.succeeded = succeeded;
|
||||
result.errorMessage = errorMessage;
|
||||
result.newerRequestPending = newerRequestPending;
|
||||
callback(result);
|
||||
}
|
||||
|
||||
void PersistenceWriter::StartWorkerLocked()
|
||||
{
|
||||
if (mWorkerRunning)
|
||||
@@ -201,13 +230,16 @@ void PersistenceWriter::WorkerMain()
|
||||
|
||||
std::string error;
|
||||
const bool succeeded = WriteSnapshotThroughSink(snapshot, error);
|
||||
bool newerRequestPending = false;
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mMutex);
|
||||
if (succeeded)
|
||||
++mWrittenCount;
|
||||
else
|
||||
++mFailedCount;
|
||||
newerRequestPending = PendingCountLocked() > 0;
|
||||
}
|
||||
PublishWriteResult(snapshot, succeeded, error, newerRequestPending);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -21,17 +21,29 @@ struct PersistenceWriterMetrics
|
||||
uint64_t failedCount = 0;
|
||||
};
|
||||
|
||||
struct PersistenceWriteResult
|
||||
{
|
||||
PersistenceTargetKind targetKind = PersistenceTargetKind::RuntimeState;
|
||||
std::string targetPath;
|
||||
std::string reason;
|
||||
bool succeeded = false;
|
||||
std::string errorMessage;
|
||||
bool newerRequestPending = false;
|
||||
};
|
||||
|
||||
class PersistenceWriter
|
||||
{
|
||||
public:
|
||||
using SnapshotSink = std::function<bool(const PersistenceSnapshot&, std::string&)>;
|
||||
using ResultCallback = std::function<void(const PersistenceWriteResult&)>;
|
||||
|
||||
explicit PersistenceWriter(
|
||||
std::chrono::milliseconds debounceDelay = std::chrono::milliseconds(50),
|
||||
SnapshotSink sink = SnapshotSink());
|
||||
~PersistenceWriter();
|
||||
|
||||
bool WriteSnapshot(const PersistenceSnapshot& snapshot, std::string& error) const;
|
||||
void SetResultCallback(ResultCallback callback);
|
||||
bool WriteSnapshot(const PersistenceSnapshot& snapshot, std::string& error);
|
||||
bool EnqueueSnapshot(const PersistenceSnapshot& snapshot, std::string& error);
|
||||
void StopAndFlush();
|
||||
PersistenceWriterMetrics GetMetrics() const;
|
||||
@@ -45,12 +57,14 @@ private:
|
||||
|
||||
bool ValidateSnapshot(const PersistenceSnapshot& snapshot, std::string& error) const;
|
||||
bool WriteSnapshotThroughSink(const PersistenceSnapshot& snapshot, std::string& error) const;
|
||||
void PublishWriteResult(const PersistenceSnapshot& snapshot, bool succeeded, const std::string& errorMessage, bool newerRequestPending);
|
||||
void StartWorkerLocked();
|
||||
void WorkerMain();
|
||||
std::size_t PendingCountLocked() const;
|
||||
|
||||
std::chrono::milliseconds mDebounceDelay;
|
||||
SnapshotSink mSink;
|
||||
ResultCallback mResultCallback;
|
||||
mutable std::mutex mMutex;
|
||||
std::condition_variable mCondition;
|
||||
std::thread mWorker;
|
||||
|
||||
Reference in New Issue
Block a user