check for running on machine before starting

This commit is contained in:
Jeff Raymakers
2025-03-09 10:59:38 -07:00
parent 62f9a73a57
commit 8c1f36c9cf
3 changed files with 29 additions and 0 deletions

View File

@@ -55,6 +55,22 @@ void HttpServer::UpdateDatabaseInstance(
}
}
bool HttpServer::IsRunningOnMachine(ClientContext &context) {
if (Started()) {
return true;
}
const auto local_port = GetLocalPort(context);
auto local_url = StringUtil::Format("http://localhost:%d", local_port);
httplib::Client client(local_url);
auto result = client.Get("/info");
if (result) {
return true;
}
return false;
}
bool HttpServer::Started() {
return server_instance && server_instance->main_thread;
}

View File

@@ -24,13 +24,16 @@ class HttpServer {
public:
HttpServer(shared_ptr<DatabaseInstance> _ddb_instance)
: ddb_instance(_ddb_instance) {}
static HttpServer *GetInstance(ClientContext &);
static void UpdateDatabaseInstanceIfRunning(shared_ptr<DatabaseInstance>);
static bool IsRunningOnMachine(ClientContext &);
static bool Started();
static void StopInstance();
static const HttpServer &Start(ClientContext &, bool *was_started = nullptr);
static bool Stop();
std::string LocalUrl() const;
private:

View File

@@ -22,6 +22,11 @@
namespace duckdb {
std::string StartUIFunction(ClientContext &context) {
if (!ui::HttpServer::Started() &&
ui::HttpServer::IsRunningOnMachine(context)) {
return "UI already running in a different DuckDB instance";
}
const auto &server = ui::HttpServer::Start(context);
const auto local_url = server.LocalUrl();
@@ -33,6 +38,11 @@ std::string StartUIFunction(ClientContext &context) {
}
std::string StartUIServerFunction(ClientContext &context) {
if (!ui::HttpServer::Started() &&
ui::HttpServer::IsRunningOnMachine(context)) {
return "UI already running in a different DuckDB instance";
}
bool was_started = false;
const auto &server = ui::HttpServer::Start(context, &was_started);
const char *already = was_started ? "already " : "";