重构语法树/符号表
This commit is contained in:
@@ -0,0 +1,292 @@
|
||||
#include <spdlog/spdlog.h>
|
||||
#include "./async_executor.hpp"
|
||||
|
||||
namespace lsp::scheduler
|
||||
{
|
||||
namespace
|
||||
{
|
||||
constexpr const char* kLogTag = "AsyncExecutor";
|
||||
|
||||
// Helper for formatting durations
|
||||
template<typename Duration>
|
||||
std::string FormatDuration(Duration duration)
|
||||
{
|
||||
auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(duration).count();
|
||||
if (ms < 1000)
|
||||
return std::to_string(ms) + "ms";
|
||||
else
|
||||
return std::to_string(ms / 1000.0) + "s";
|
||||
}
|
||||
}
|
||||
|
||||
// ============= AsyncExecutor Implementation =============
|
||||
AsyncExecutor::AsyncExecutor(size_t concurrency) :
|
||||
executor_(concurrency)
|
||||
{
|
||||
spdlog::info("{}: Initialized with {} threads (hardware: {})", kLogTag, concurrency, std::thread::hardware_concurrency());
|
||||
}
|
||||
|
||||
AsyncExecutor::~AsyncExecutor()
|
||||
{
|
||||
spdlog::info("{}: Shutting down...", kLogTag);
|
||||
LogStatus();
|
||||
WaitAll();
|
||||
spdlog::info("{}: Destroyed", kLogTag);
|
||||
}
|
||||
|
||||
void AsyncExecutor::Submit(const std::string& task_id, TaskFunc task, Callback callback)
|
||||
{
|
||||
auto submit_start = Clock::now();
|
||||
spdlog::debug("[{}] Submitting task", task_id);
|
||||
|
||||
auto context = std::make_shared<TaskContext>();
|
||||
context->callback = callback;
|
||||
bool replaced_existing = RegisterTask(task_id, context);
|
||||
|
||||
LogTaskStart(task_id, replaced_existing);
|
||||
|
||||
try
|
||||
{
|
||||
executor_.async([this, task_id, task = std::move(task), context]() {
|
||||
ExecuteTask(task_id, std::move(task), context);
|
||||
});
|
||||
|
||||
auto duration = GetElapsedTime(submit_start);
|
||||
spdlog::debug("[{}] Submitted in {}", task_id, FormatDuration(duration));
|
||||
}
|
||||
catch (const std::exception& e)
|
||||
{
|
||||
spdlog::error("[{}] Failed to submit: {}", task_id, e.what());
|
||||
UnregisterTask(task_id);
|
||||
stats_.IncrementFailed();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
bool AsyncExecutor::Cancel(const std::string& task_id)
|
||||
{
|
||||
bool cancelled = MarkTaskCancelled(task_id);
|
||||
|
||||
if (cancelled)
|
||||
spdlog::info("[{}] Cancelled successfully", task_id);
|
||||
else
|
||||
spdlog::debug("[{}] Cancel requested but task not found", task_id);
|
||||
|
||||
return cancelled;
|
||||
}
|
||||
|
||||
void AsyncExecutor::WaitAll()
|
||||
{
|
||||
spdlog::info("{}: Waiting for all tasks...", kLogTag);
|
||||
|
||||
auto wait_start = Clock::now();
|
||||
executor_.wait_for_all();
|
||||
auto duration = GetElapsedTime(wait_start);
|
||||
|
||||
spdlog::info("{}: All tasks completed in {}", kLogTag, FormatDuration(duration));
|
||||
LogStatus();
|
||||
}
|
||||
|
||||
size_t AsyncExecutor::GetRunningTaskCount() const
|
||||
{
|
||||
std::lock_guard lock(mutex_);
|
||||
return running_tasks_.size();
|
||||
}
|
||||
|
||||
AsyncExecutor::Statistics AsyncExecutor::GetStatistics() const
|
||||
{
|
||||
std::lock_guard lock(mutex_);
|
||||
return stats_.GetSnapshot(running_tasks_.size());
|
||||
}
|
||||
|
||||
void AsyncExecutor::ExecuteTask(const std::string& task_id, TaskFunc task, TaskContextPtr context)
|
||||
{
|
||||
auto task_start = Clock::now();
|
||||
spdlog::info("[{}] Task execution started", task_id);
|
||||
|
||||
try
|
||||
{
|
||||
// Check if already cancelled
|
||||
if (context->cancelled.load())
|
||||
{
|
||||
spdlog::debug("[{}] Task was pre-cancelled", task_id);
|
||||
stats_.IncrementCancelled();
|
||||
LogTaskCompletion(task_id, task_start, false);
|
||||
UnregisterTask(task_id);
|
||||
return;
|
||||
}
|
||||
|
||||
// Execute the task
|
||||
spdlog::debug("[{}] Executing task function", task_id);
|
||||
auto result = task();
|
||||
|
||||
// Check if cancelled during execution
|
||||
if (context->cancelled.load())
|
||||
{
|
||||
spdlog::debug("[{}] Task cancelled during execution", task_id);
|
||||
stats_.IncrementCancelled();
|
||||
}
|
||||
else if (result)
|
||||
{
|
||||
spdlog::trace("[{}] Task produced {} bytes result", task_id, result->size());
|
||||
if (context->callback && result.has_value())
|
||||
context->callback(result.value());
|
||||
stats_.IncrementCompleted();
|
||||
}
|
||||
else
|
||||
{
|
||||
spdlog::warn("[{}] Task completed with no result", task_id);
|
||||
stats_.IncrementCompleted();
|
||||
}
|
||||
|
||||
LogTaskCompletion(task_id, task_start, true);
|
||||
}
|
||||
catch (const std::exception& e)
|
||||
{
|
||||
spdlog::error("[{}] Task failed: {}", task_id, e.what());
|
||||
stats_.IncrementFailed();
|
||||
LogTaskCompletion(task_id, task_start, false);
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
spdlog::error("[{}] Task failed with unknown exception", task_id);
|
||||
stats_.IncrementFailed();
|
||||
LogTaskCompletion(task_id, task_start, false);
|
||||
}
|
||||
|
||||
UnregisterTask(task_id);
|
||||
|
||||
// Periodic status logging
|
||||
if (stats_.completed.load() % kStatusLogInterval == 0)
|
||||
LogStatus();
|
||||
}
|
||||
|
||||
bool AsyncExecutor::RegisterTask(const std::string& task_id, TaskContextPtr context)
|
||||
{
|
||||
std::lock_guard lock(mutex_);
|
||||
|
||||
bool replaced = false;
|
||||
auto it = running_tasks_.find(task_id);
|
||||
|
||||
if (it != running_tasks_.end())
|
||||
{
|
||||
spdlog::warn("[{}] Cancelling existing task", task_id);
|
||||
it->second->cancelled.store(true);
|
||||
stats_.IncrementCancelled();
|
||||
replaced = true;
|
||||
}
|
||||
|
||||
running_tasks_[task_id] = context;
|
||||
stats_.IncrementSubmitted();
|
||||
|
||||
spdlog::debug("[{}] Registered (running: {}, total: {})", task_id, running_tasks_.size(), stats_.submitted.load());
|
||||
return replaced;
|
||||
}
|
||||
|
||||
void AsyncExecutor::UnregisterTask(const std::string& task_id)
|
||||
{
|
||||
std::lock_guard lock(mutex_);
|
||||
|
||||
auto it = running_tasks_.find(task_id);
|
||||
if (it != running_tasks_.end())
|
||||
{
|
||||
auto lifetime = GetElapsedTime(it->second->start_time);
|
||||
spdlog::trace("[{}] Task lifetime: {}", task_id, FormatDuration(lifetime));
|
||||
|
||||
running_tasks_.erase(it);
|
||||
spdlog::debug("[{}] Unregistered (remaining: {})", task_id, running_tasks_.size());
|
||||
}
|
||||
else
|
||||
{
|
||||
spdlog::warn("[{}] Attempted to unregister non-existent task", task_id);
|
||||
}
|
||||
}
|
||||
|
||||
bool AsyncExecutor::MarkTaskCancelled(const std::string& task_id)
|
||||
{
|
||||
std::lock_guard lock(mutex_);
|
||||
|
||||
auto it = running_tasks_.find(task_id);
|
||||
if (it != running_tasks_.end())
|
||||
{
|
||||
it->second->cancelled.store(true);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void AsyncExecutor::LogTaskStart(const std::string& task_id, bool replaced_existing) const
|
||||
{
|
||||
auto stats = GetStatistics();
|
||||
|
||||
if (replaced_existing)
|
||||
spdlog::info("[{}] Submitted (replaced existing) - Running: {}, Total: {}", task_id, stats.running, stats.submitted);
|
||||
else
|
||||
spdlog::debug("[{}] Submitted - Running: {}, Total: {}", task_id, stats.running, stats.submitted);
|
||||
}
|
||||
|
||||
void AsyncExecutor::LogTaskCompletion(const std::string& task_id, TimePoint start_time, bool success) const
|
||||
{
|
||||
auto duration = GetElapsedTime(start_time);
|
||||
|
||||
if (success)
|
||||
spdlog::info("[{}] Completed in {}", task_id, FormatDuration(duration));
|
||||
else
|
||||
spdlog::warn("[{}] Failed/cancelled after {}", task_id, FormatDuration(duration));
|
||||
}
|
||||
|
||||
std::chrono::milliseconds AsyncExecutor::GetElapsedTime(TimePoint start_time) const
|
||||
{
|
||||
return std::chrono::duration_cast<std::chrono::milliseconds>(Clock::now() - start_time);
|
||||
}
|
||||
|
||||
void AsyncExecutor::LogStatus() const
|
||||
{
|
||||
// if (spdlog::get_level() > spdlog::level::debug)
|
||||
// return;
|
||||
// 一次性获取所有需要的数据
|
||||
std::vector<std::pair<std::string, std::pair<TimePoint, bool>>> active_tasks;
|
||||
Statistics stats;
|
||||
|
||||
{
|
||||
std::lock_guard lock(mutex_);
|
||||
stats = stats_.GetSnapshot(running_tasks_.size());
|
||||
|
||||
for (const auto& [id, context] : running_tasks_)
|
||||
active_tasks.emplace_back(id, std::make_pair(context->start_time, context->cancelled.load()));
|
||||
}
|
||||
|
||||
auto total = stats.submitted;
|
||||
std::ostringstream status_output;
|
||||
|
||||
status_output << "\n";
|
||||
status_output << " ╭────────────────────────────────────────────╮\n";
|
||||
status_output << " │ 🔔 AsyncExecutor Dashboard │\n";
|
||||
status_output << " ├────────────────────────────────────────────┤\n";
|
||||
status_output << " │ │\n";
|
||||
status_output << " │ 📊 SYSTEM METRICS │\n";
|
||||
status_output << " │ ────────────────────────────────────── │\n";
|
||||
status_output << fmt::format(" │ 📤 Submitted ▶ {:>6} total │\n", stats.submitted);
|
||||
status_output << fmt::format(" │ 🚀 Running ▶ {:>6} tasks ({:>5.1f}%) │\n", stats.running, total > 0 ? (stats.running * 100.0 / total) : 0);
|
||||
status_output << fmt::format(" │ ✅ Completed ▶ {:>6} tasks ({:>5.1f}%) │\n", stats.completed, total > 0 ? (stats.completed * 100.0 / total) : 0);
|
||||
status_output << fmt::format(" │ ❌ Failed ▶ {:>6} tasks ({:>5.1f}%) │\n", stats.failed, total > 0 ? (stats.failed * 100.0 / total) : 0);
|
||||
status_output << fmt::format(" │ 🥏 Cancelled ▶ {:>6} tasks ({:>5.1f}%) │\n", stats.cancelled, total > 0 ? (stats.cancelled * 100.0 / total) : 0);
|
||||
status_output << " │ │\n";
|
||||
if (stats.running > 0)
|
||||
{
|
||||
status_output << " │ 🎯 ACTIVE TASKS │\n";
|
||||
status_output << " │ ────────────────────────────────────── │\n";
|
||||
for (const auto& [id, info] : active_tasks)
|
||||
{
|
||||
auto [start_time, is_cancelled] = info;
|
||||
auto runtime = GetElapsedTime(start_time);
|
||||
auto status_icon = is_cancelled ? "🔴" : "🟢";
|
||||
auto short_id = id.length() > 20 ? id.substr(0, 20) + "…" : id;
|
||||
status_output << fmt::format(" │ {} │ {:22} │ ⏱️ {:<8}│\n", status_icon, short_id, FormatDuration(runtime));
|
||||
}
|
||||
}
|
||||
status_output << " ╰────────────────────────────────────────────╯";
|
||||
spdlog::debug("{}", status_output.str());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
#pragma once
|
||||
#include <atomic>
|
||||
#include <functional>
|
||||
#include <mutex>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <chrono>
|
||||
#include <taskflow/taskflow.hpp>
|
||||
|
||||
namespace lsp::scheduler
|
||||
{
|
||||
class AsyncExecutor
|
||||
{
|
||||
public:
|
||||
using TaskFunc = std::function<std::optional<std::string>()>;
|
||||
using Callback = std::function<void(const std::string&)>;
|
||||
using Clock = std::chrono::steady_clock;
|
||||
using TimePoint = Clock::time_point;
|
||||
|
||||
explicit AsyncExecutor(size_t concurrency = std::thread::hardware_concurrency());
|
||||
~AsyncExecutor();
|
||||
|
||||
// Delete copy operations
|
||||
AsyncExecutor(const AsyncExecutor&) = delete;
|
||||
AsyncExecutor& operator=(const AsyncExecutor&) = delete;
|
||||
|
||||
// Public interface
|
||||
void Submit(const std::string& task_id, TaskFunc task, Callback callback = nullptr);
|
||||
bool Cancel(const std::string& task_id);
|
||||
void WaitAll();
|
||||
|
||||
// Status and statistics
|
||||
size_t GetRunningTaskCount() const;
|
||||
void LogStatus() const;
|
||||
|
||||
struct Statistics
|
||||
{
|
||||
size_t running = 0;
|
||||
uint64_t submitted = 0;
|
||||
uint64_t completed = 0;
|
||||
uint64_t failed = 0;
|
||||
uint64_t cancelled = 0;
|
||||
};
|
||||
|
||||
Statistics GetStatistics() const;
|
||||
|
||||
private:
|
||||
struct TaskContext
|
||||
{
|
||||
std::atomic<bool> cancelled { false };
|
||||
Callback callback {nullptr};
|
||||
TimePoint start_time;
|
||||
|
||||
TaskContext() :
|
||||
start_time(Clock::now()) {}
|
||||
};
|
||||
|
||||
struct StatsManager
|
||||
{
|
||||
std::atomic<uint64_t> submitted{0};
|
||||
std::atomic<uint64_t> completed{0};
|
||||
std::atomic<uint64_t> failed{0};
|
||||
std::atomic<uint64_t> cancelled{0};
|
||||
|
||||
void IncrementSubmitted() { submitted++; }
|
||||
void IncrementCompleted() { completed++; }
|
||||
void IncrementFailed() { failed++; }
|
||||
void IncrementCancelled() { cancelled++; }
|
||||
|
||||
Statistics GetSnapshot(size_t running_count) const
|
||||
{
|
||||
return {
|
||||
.running = running_count,
|
||||
.submitted = submitted.load(),
|
||||
.completed = completed.load(),
|
||||
.failed = failed.load(),
|
||||
.cancelled = cancelled.load()
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
using TaskContextPtr = std::shared_ptr<TaskContext>;
|
||||
|
||||
// Task lifecycle management
|
||||
void ExecuteTask(const std::string& task_id, TaskFunc task, TaskContextPtr context);
|
||||
bool RegisterTask(const std::string& task_id, TaskContextPtr context);
|
||||
void UnregisterTask(const std::string& task_id);
|
||||
bool MarkTaskCancelled(const std::string& task_id);
|
||||
|
||||
// Logging helpers
|
||||
void LogTaskStart(const std::string& task_id, bool replaced_existing) const;
|
||||
void LogTaskCompletion(const std::string& task_id, TimePoint start_time, bool success) const;
|
||||
std::chrono::milliseconds GetElapsedTime(TimePoint start_time) const;
|
||||
|
||||
private:
|
||||
// Core components
|
||||
tf::Executor executor_;
|
||||
mutable std::mutex mutex_;
|
||||
std::unordered_map<std::string, TaskContextPtr> running_tasks_;
|
||||
StatsManager stats_;
|
||||
|
||||
// Configuration
|
||||
static constexpr size_t kStatusLogInterval = 10;
|
||||
};
|
||||
}
|
||||
@@ -1,318 +0,0 @@
|
||||
#include <spdlog/spdlog.h>
|
||||
#include "./request.hpp"
|
||||
|
||||
namespace lsp::scheduler
|
||||
{
|
||||
namespace
|
||||
{
|
||||
constexpr const char* kLogTag = "RequestScheduler";
|
||||
|
||||
// Helper for formatting durations
|
||||
template<typename Duration>
|
||||
std::string FormatDuration(Duration duration)
|
||||
{
|
||||
auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(duration).count();
|
||||
if (ms < 1000)
|
||||
return std::to_string(ms) + "ms";
|
||||
else
|
||||
return std::to_string(ms / 1000.0) + "s";
|
||||
}
|
||||
}
|
||||
|
||||
// ============= Request Implementation =============
|
||||
Request::Request(size_t concurrency) :
|
||||
executor_(concurrency)
|
||||
{
|
||||
spdlog::info("{}: Initialized with {} threads (hardware: {})", kLogTag, concurrency, std::thread::hardware_concurrency());
|
||||
}
|
||||
|
||||
Request::~Request()
|
||||
{
|
||||
spdlog::info("{}: Shutting down...", kLogTag);
|
||||
LogStatus();
|
||||
WaitAll();
|
||||
spdlog::info("{}: Destroyed", kLogTag);
|
||||
}
|
||||
|
||||
void Request::Submit(const std::string& request_id, TaskFunc task)
|
||||
{
|
||||
auto submit_start = Clock::now();
|
||||
spdlog::debug("[{}] Submitting task", request_id);
|
||||
|
||||
auto context = std::make_shared<TaskContext>();
|
||||
bool replaced_existing = RegisterTask(request_id, context);
|
||||
|
||||
LogTaskStart(request_id, replaced_existing);
|
||||
|
||||
try
|
||||
{
|
||||
executor_.async([this, request_id, task = std::move(task), context]() {
|
||||
ExecuteTask(request_id, std::move(task), context);
|
||||
});
|
||||
|
||||
auto duration = GetElapsedTime(submit_start);
|
||||
spdlog::debug("[{}] Submitted in {}", request_id, FormatDuration(duration));
|
||||
}
|
||||
catch (const std::exception& e)
|
||||
{
|
||||
spdlog::error("[{}] Failed to submit: {}", request_id, e.what());
|
||||
UnregisterTask(request_id);
|
||||
total_failed_++;
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
bool Request::Cancel(const std::string& request_id)
|
||||
{
|
||||
bool cancelled = MarkTaskCancelled(request_id);
|
||||
|
||||
if (cancelled)
|
||||
spdlog::info("[{}] Cancelled successfully", request_id);
|
||||
else
|
||||
spdlog::debug("[{}] Cancel requested but task not found", request_id);
|
||||
|
||||
return cancelled;
|
||||
}
|
||||
|
||||
void Request::SetResponseCallback(ResponseCallback callback)
|
||||
{
|
||||
std::lock_guard lock(mutex_);
|
||||
response_callback_ = std::move(callback);
|
||||
}
|
||||
|
||||
void Request::WaitAll()
|
||||
{
|
||||
spdlog::info("{}: Waiting for all tasks...", kLogTag);
|
||||
|
||||
auto wait_start = Clock::now();
|
||||
executor_.wait_for_all();
|
||||
auto duration = GetElapsedTime(wait_start);
|
||||
|
||||
spdlog::info("{}: All tasks completed in {}", kLogTag, FormatDuration(duration));
|
||||
LogStatus();
|
||||
}
|
||||
|
||||
size_t Request::GetRunningTaskCount() const
|
||||
{
|
||||
std::lock_guard lock(mutex_);
|
||||
return running_tasks_.size();
|
||||
}
|
||||
|
||||
Request::Statistics Request::GetStatistics() const
|
||||
{
|
||||
std::lock_guard lock(mutex_);
|
||||
return {
|
||||
.running = running_tasks_.size(),
|
||||
.submitted = total_submitted_.load(),
|
||||
.completed = total_completed_.load(),
|
||||
.failed = total_failed_.load(),
|
||||
.cancelled = total_cancelled_.load()
|
||||
};
|
||||
}
|
||||
|
||||
void Request::ExecuteTask(const std::string& request_id, TaskFunc task, TaskContextPtr context)
|
||||
{
|
||||
auto task_start = Clock::now();
|
||||
spdlog::info("[{}] Task execution started", request_id);
|
||||
|
||||
try
|
||||
{
|
||||
// Check if already cancelled
|
||||
if (context->cancelled.load())
|
||||
{
|
||||
spdlog::debug("[{}] Task was pre-cancelled", request_id);
|
||||
total_cancelled_++;
|
||||
LogTaskCompletion(request_id, task_start, false);
|
||||
UnregisterTask(request_id);
|
||||
return;
|
||||
}
|
||||
|
||||
// Execute the task
|
||||
spdlog::debug("[{}] Executing task function", request_id);
|
||||
auto result = task();
|
||||
|
||||
// Check if cancelled during execution
|
||||
if (context->cancelled.load())
|
||||
{
|
||||
spdlog::debug("[{}] Task cancelled during execution", request_id);
|
||||
total_cancelled_++;
|
||||
}
|
||||
else if (result)
|
||||
{
|
||||
spdlog::trace("[{}] Task produced {} bytes result", request_id, result->size());
|
||||
SendResponse(*result);
|
||||
total_completed_++;
|
||||
}
|
||||
else
|
||||
{
|
||||
spdlog::warn("[{}] Task completed with no result", request_id);
|
||||
total_completed_++;
|
||||
}
|
||||
|
||||
LogTaskCompletion(request_id, task_start, true);
|
||||
}
|
||||
catch (const std::exception& e)
|
||||
{
|
||||
spdlog::error("[{}] Task failed: {}", request_id, e.what());
|
||||
total_failed_++;
|
||||
LogTaskCompletion(request_id, task_start, false);
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
spdlog::error("[{}] Task failed with unknown exception", request_id);
|
||||
total_failed_++;
|
||||
LogTaskCompletion(request_id, task_start, false);
|
||||
}
|
||||
|
||||
UnregisterTask(request_id);
|
||||
|
||||
// Periodic status logging
|
||||
if (total_completed_.load() % kStatusLogInterval * 10 == 0)
|
||||
LogStatus();
|
||||
}
|
||||
|
||||
bool Request::RegisterTask(const std::string& request_id, TaskContextPtr context)
|
||||
{
|
||||
std::lock_guard lock(mutex_);
|
||||
|
||||
bool replaced = false;
|
||||
auto it = running_tasks_.find(request_id);
|
||||
|
||||
if (it != running_tasks_.end())
|
||||
{
|
||||
spdlog::warn("[{}] Cancelling existing task", request_id);
|
||||
it->second->cancelled.store(true);
|
||||
total_cancelled_++;
|
||||
replaced = true;
|
||||
}
|
||||
|
||||
running_tasks_[request_id] = context;
|
||||
total_submitted_++;
|
||||
|
||||
spdlog::debug("[{}] Registered (running: {}, total: {})", request_id, running_tasks_.size(), total_submitted_.load());
|
||||
return replaced;
|
||||
}
|
||||
|
||||
void Request::UnregisterTask(const std::string& request_id)
|
||||
{
|
||||
std::lock_guard lock(mutex_);
|
||||
|
||||
auto it = running_tasks_.find(request_id);
|
||||
if (it != running_tasks_.end())
|
||||
{
|
||||
auto lifetime = GetElapsedTime(it->second->start_time);
|
||||
spdlog::trace("[{}] Task lifetime: {}", request_id, FormatDuration(lifetime));
|
||||
|
||||
running_tasks_.erase(it);
|
||||
spdlog::debug("[{}] Unregistered (remaining: {})", request_id, running_tasks_.size());
|
||||
}
|
||||
else
|
||||
{
|
||||
spdlog::warn("[{}] Attempted to unregister non-existent task", request_id);
|
||||
}
|
||||
}
|
||||
|
||||
bool Request::MarkTaskCancelled(const std::string& request_id)
|
||||
{
|
||||
std::lock_guard lock(mutex_);
|
||||
|
||||
auto it = running_tasks_.find(request_id);
|
||||
if (it != running_tasks_.end())
|
||||
{
|
||||
it->second->cancelled.store(true);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void Request::SendResponse(const std::string& response) const
|
||||
{
|
||||
ResponseCallback callback;
|
||||
{
|
||||
std::lock_guard lock(mutex_);
|
||||
callback = response_callback_;
|
||||
}
|
||||
|
||||
if (!callback)
|
||||
{
|
||||
spdlog::error("{}: No response callback set! Response lost ({} bytes)", kLogTag, response.size());
|
||||
return;
|
||||
}
|
||||
|
||||
spdlog::trace("{}: Sending response ({} bytes)", kLogTag, response.size());
|
||||
|
||||
try
|
||||
{
|
||||
callback(response);
|
||||
spdlog::trace("{}: Response sent successfully", kLogTag);
|
||||
}
|
||||
catch (const std::exception& e)
|
||||
{
|
||||
spdlog::error("{}: Response callback failed: {}", kLogTag, e.what());
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
spdlog::error("{}: Response callback failed with unknown exception", kLogTag);
|
||||
}
|
||||
}
|
||||
|
||||
void Request::LogTaskStart(const std::string& request_id, bool replaced_existing) const
|
||||
{
|
||||
auto stats = GetStatistics();
|
||||
|
||||
if (replaced_existing)
|
||||
spdlog::info("[{}] Submitted (replaced existing) - Running: {}, Total: {}", request_id, stats.running, stats.submitted);
|
||||
else
|
||||
spdlog::debug("[{}] Submitted - Running: {}, Total: {}", request_id, stats.running, stats.submitted);
|
||||
}
|
||||
|
||||
void Request::LogTaskCompletion(const std::string& request_id, TimePoint start_time, bool success) const
|
||||
{
|
||||
auto duration = GetElapsedTime(start_time);
|
||||
|
||||
if (success)
|
||||
spdlog::info("[{}] Completed in {}", request_id, FormatDuration(duration));
|
||||
else
|
||||
spdlog::warn("[{}] Failed/cancelled after {}", request_id, FormatDuration(duration));
|
||||
}
|
||||
|
||||
std::chrono::milliseconds Request::GetElapsedTime(TimePoint start_time) const
|
||||
{
|
||||
return std::chrono::duration_cast<std::chrono::milliseconds>(Clock::now() - start_time);
|
||||
}
|
||||
|
||||
void Request::LogStatus() const
|
||||
{
|
||||
auto stats = GetStatistics();
|
||||
auto total = stats.submitted;
|
||||
std::ostringstream status_output;
|
||||
|
||||
status_output << "\n";
|
||||
status_output << " ╭────────────────────────────────────────────╮\n";
|
||||
status_output << " │ 🔔 Request Scheduler Dashboard │\n";
|
||||
status_output << " ├────────────────────────────────────────────┤\n";
|
||||
status_output << " │ │\n";
|
||||
status_output << " │ 📊 SYSTEM METRICS │\n";
|
||||
status_output << " │ ────────────────────────────────────── │\n";
|
||||
status_output << fmt::format(" │ 📤 Submitted ▶ {:>6} total │\n", stats.submitted);
|
||||
status_output << fmt::format(" │ 🚀 Running ▶ {:>6} tasks ({:>5.1f}%) │\n", stats.running, total > 0 ? (stats.running * 100.0 / total) : 0);
|
||||
status_output << fmt::format(" │ ✅ Completed ▶ {:>6} tasks ({:>5.1f}%) │\n", stats.completed, total > 0 ? (stats.completed * 100.0 / total) : 0);
|
||||
status_output << fmt::format(" │ ❌ Failed ▶ {:>6} tasks ({:>5.1f}%) │\n", stats.failed, total > 0 ? (stats.failed * 100.0 / total) : 0);
|
||||
status_output << fmt::format(" │ 🥏 Cancelled ▶ {:>6} tasks ({:>5.1f}%) │\n", stats.cancelled, total > 0 ? (stats.cancelled * 100.0 / total) : 0);
|
||||
status_output << " │ │\n";
|
||||
if (stats.running > 0) {
|
||||
status_output << " │ 🎯 ACTIVE TASKS │\n";
|
||||
status_output << " │ ────────────────────────────────────── │\n";
|
||||
std::lock_guard lock(mutex_);
|
||||
for (const auto& [id, context] : running_tasks_) {
|
||||
auto runtime = GetElapsedTime(context->start_time);
|
||||
auto status_icon = context->cancelled.load() ? "🔴" : "🟢";
|
||||
auto short_id = id.length() > 20 ? id.substr(0, 20) + "…" : id;
|
||||
status_output << fmt::format(" │ {} │ {:22} │ ⏱️ {:<8}│\n", status_icon, short_id, FormatDuration(runtime));
|
||||
}
|
||||
}
|
||||
status_output << " ╰────────────────────────────────────────────╯";
|
||||
spdlog::debug("{}", status_output.str());
|
||||
}
|
||||
}
|
||||
@@ -1,91 +0,0 @@
|
||||
#pragma once
|
||||
#include <atomic>
|
||||
#include <functional>
|
||||
#include <mutex>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <chrono>
|
||||
#include <taskflow/taskflow.hpp>
|
||||
|
||||
namespace lsp::scheduler
|
||||
{
|
||||
class Request
|
||||
{
|
||||
public:
|
||||
using TaskFunc = std::function<std::optional<std::string>()>;
|
||||
using ResponseCallback = std::function<void(const std::string&)>;
|
||||
using Clock = std::chrono::steady_clock;
|
||||
using TimePoint = Clock::time_point;
|
||||
|
||||
explicit Request(size_t concurrency = std::thread::hardware_concurrency());
|
||||
~Request();
|
||||
|
||||
// Delete copy operations
|
||||
Request(const Request&) = delete;
|
||||
Request& operator=(const Request&) = delete;
|
||||
|
||||
// Public interface
|
||||
void Submit(const std::string& request_id, TaskFunc task);
|
||||
bool Cancel(const std::string& request_id);
|
||||
void SetResponseCallback(ResponseCallback callback);
|
||||
void WaitAll();
|
||||
|
||||
// Status and statistics
|
||||
size_t GetRunningTaskCount() const;
|
||||
void LogStatus() const;
|
||||
|
||||
struct Statistics
|
||||
{
|
||||
size_t running = 0;
|
||||
uint64_t submitted = 0;
|
||||
uint64_t completed = 0;
|
||||
uint64_t failed = 0;
|
||||
uint64_t cancelled = 0;
|
||||
};
|
||||
|
||||
Statistics GetStatistics() const;
|
||||
|
||||
private:
|
||||
struct TaskContext
|
||||
{
|
||||
std::atomic<bool> cancelled{ false };
|
||||
TimePoint start_time;
|
||||
|
||||
TaskContext() :
|
||||
start_time(Clock::now()) {}
|
||||
};
|
||||
|
||||
using TaskContextPtr = std::shared_ptr<TaskContext>;
|
||||
|
||||
// Task lifecycle management
|
||||
void ExecuteTask(const std::string& request_id, TaskFunc task, TaskContextPtr context);
|
||||
bool RegisterTask(const std::string& request_id, TaskContextPtr context);
|
||||
void UnregisterTask(const std::string& request_id);
|
||||
bool MarkTaskCancelled(const std::string& request_id);
|
||||
|
||||
// Response handling
|
||||
void SendResponse(const std::string& response) const;
|
||||
|
||||
// Logging helpers
|
||||
void LogTaskStart(const std::string& request_id, bool replaced_existing) const;
|
||||
void LogTaskCompletion(const std::string& request_id, TimePoint start_time, bool success) const;
|
||||
std::chrono::milliseconds GetElapsedTime(TimePoint start_time) const;
|
||||
|
||||
private:
|
||||
// Core components
|
||||
tf::Executor executor_;
|
||||
mutable std::mutex mutex_;
|
||||
std::unordered_map<std::string, TaskContextPtr> running_tasks_;
|
||||
ResponseCallback response_callback_;
|
||||
|
||||
// Statistics
|
||||
std::atomic<uint64_t> total_submitted_{ 0 };
|
||||
std::atomic<uint64_t> total_completed_{ 0 };
|
||||
std::atomic<uint64_t> total_failed_{ 0 };
|
||||
std::atomic<uint64_t> total_cancelled_{ 0 };
|
||||
|
||||
// Configuration
|
||||
static constexpr size_t kStatusLogInterval = 5;
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user