Skip to content

Commit 670a8af

Browse files
committed
Adding watch/interval params info in the instance registry
1 parent d92292f commit 670a8af

File tree

3 files changed

+50
-20
lines changed

3 files changed

+50
-20
lines changed

src/app.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -879,10 +879,10 @@ void App::serve(int suggestedPort, bool watch, int interval)
879879
LOG_MSG << " Auto-update: disabled";
880880
}
881881

882-
serverThread = std::thread([this, suggestedPort]() {
882+
serverThread = std::thread([this, suggestedPort, watch, interval]() {
883883
int newPort = imp->httpServer_->bindToPortIncremental(suggestedPort);
884884
if (0 < newPort) {
885-
imp->registry_ = std::make_unique<InstanceRegistry>(newPort, settings());
885+
imp->registry_ = std::make_unique<InstanceRegistry>(newPort, watch ? interval : 0, settings());
886886
try {
887887
imp->registry_->startHeartbeat();
888888
LOG_MSG << "\nStarting HTTP API server on port " << newPort << "...";

src/instregistry.cpp

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,8 @@ struct InstanceRegistry::Impl {
184184
cwd TEXT NOT NULL,
185185
config_path TEXT NOT NULL,
186186
status TEXT NOT NULL DEFAULT 'healthy',
187-
created_at INTEGER DEFAULT (strftime('%s', 'now'))
187+
created_at INTEGER DEFAULT (strftime('%s', 'now')),
188+
params TEXT
188189
);
189190
190191
CREATE INDEX IF NOT EXISTS idx_instances_heartbeat ON instances(last_heartbeat);
@@ -283,7 +284,7 @@ struct InstanceRegistry::Impl {
283284
}
284285
}
285286

286-
void registerInstance(int port, const Settings &settings) {
287+
void registerInstance(int port, int watchInterval, const Settings &settings) {
287288
std::lock_guard<std::mutex> lock(dbMutex_);
288289

289290
const auto [now, nowStr] = curTimestamp();
@@ -293,8 +294,8 @@ struct InstanceRegistry::Impl {
293294
const char *insertSQL = R"(
294295
INSERT OR REPLACE INTO instances
295296
(id, pid, port, host, project_id, name, started_at, started_at_str,
296-
last_heartbeat, last_heartbeat_str, cwd, config_path, status)
297-
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
297+
last_heartbeat, last_heartbeat_str, cwd, config_path, status, params)
298+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
298299
)";
299300

300301
SqliteStmt stmt(db_);
@@ -311,17 +312,21 @@ struct InstanceRegistry::Impl {
311312
sqlite3_bind_int(stmt.ref(), 3, port);
312313
sqlite3_bind_text(stmt.ref(), 4, "localhost", -1, SQLITE_STATIC);
313314
auto projectId = settings.getProjectId();
314-
sqlite3_bind_text(stmt.ref(), 5, projectId.c_str(), -1, SQLITE_STATIC);
315-
sqlite3_bind_text(stmt.ref(), 6, name.c_str(), -1, SQLITE_STATIC);
315+
sqlite3_bind_text(stmt.ref(), 5, projectId.c_str(), -1, SQLITE_TRANSIENT);
316+
sqlite3_bind_text(stmt.ref(), 6, name.c_str(), -1, SQLITE_TRANSIENT);
316317
sqlite3_bind_int64(stmt.ref(), 7, now);
317-
sqlite3_bind_text(stmt.ref(), 8, nowStr.c_str(), -1, SQLITE_STATIC);
318+
sqlite3_bind_text(stmt.ref(), 8, nowStr.c_str(), -1, SQLITE_TRANSIENT);
318319
sqlite3_bind_int64(stmt.ref(), 9, now);
319-
sqlite3_bind_text(stmt.ref(), 10, nowStr.c_str(), -1, SQLITE_STATIC);
320+
sqlite3_bind_text(stmt.ref(), 10, nowStr.c_str(), -1, SQLITE_TRANSIENT);
320321
std::string cwd = std::filesystem::current_path().string();
321322
std::string absConfig = std::filesystem::absolute(settings.configPath()).string();
322323
sqlite3_bind_text(stmt.ref(), 11, cwd.c_str(), -1, SQLITE_TRANSIENT);
323324
sqlite3_bind_text(stmt.ref(), 12, absConfig.c_str(), -1, SQLITE_TRANSIENT);
324325
sqlite3_bind_text(stmt.ref(), 13, "healthy", -1, SQLITE_STATIC);
326+
nlohmann::json jsonParams;
327+
jsonParams["watch_interval"] = watchInterval;
328+
auto paramsText = jsonParams.dump();
329+
sqlite3_bind_text(stmt.ref(), 14, paramsText.c_str(), -1, SQLITE_TRANSIENT);
325330
rc = sqlite3_step(stmt.ref());
326331
if (rc != SQLITE_DONE) {
327332
LOG_MSG << "Failed to register instance: " << sqlite3_errmsg(db_);
@@ -402,7 +407,7 @@ struct InstanceRegistry::Impl {
402407
const char *selectSQL = R"(
403408
SELECT id, pid, port, host, project_id, name, started_at,
404409
started_at_str, last_heartbeat, last_heartbeat_str,
405-
cwd, config_path, status
410+
cwd, config_path, status, params
406411
FROM instances
407412
WHERE (strftime('%s', 'now') - last_heartbeat) < 30
408413
ORDER BY last_heartbeat DESC
@@ -432,6 +437,7 @@ struct InstanceRegistry::Impl {
432437
instance["cwd"] = stmt.getStr(j++);
433438
instance["config"] = stmt.getStr(j++);
434439
instance["status"] = stmt.getStr(j++);
440+
instance["params"] = stmt.getStr(j++);
435441
active.push_back(instance);
436442
}
437443
return active;
@@ -443,18 +449,18 @@ InstanceRegistry::InstanceRegistry(const std::string &registryPath)
443449
{
444450
}
445451

446-
InstanceRegistry::InstanceRegistry(int port, const Settings &settings, const std::string &registryPath)
452+
InstanceRegistry::InstanceRegistry(int port, int watchInterval, const Settings &settings, const std::string &registryPath)
447453
: imp(std::make_unique<Impl>(registryPath))
448454
{
449455
imp->bRegistered_ = true;
450-
registerInstance(port, settings);
456+
registerInstance(port, watchInterval, settings);
451457
}
452458

453459
InstanceRegistry::~InstanceRegistry() = default;
454460

455-
void InstanceRegistry::registerInstance(int port, const Settings &settings)
461+
void InstanceRegistry::registerInstance(int port, int watchInterval, const Settings &settings)
456462
{
457-
imp->registerInstance(port, settings);
463+
imp->registerInstance(port, watchInterval, settings);
458464
}
459465

460466
void InstanceRegistry::unregister()

ui/dashboard/webview/src/main.cpp

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ namespace {
6969
if (!j["jsonData"].contains("source")) {
7070
throw std::runtime_error("Missing jsonData.source");
7171
}
72+
#if 0
7273
auto projectId = j["jsonData"]["source"].value("project_id", std::string{});
7374
// Note: projectId can be empty (auto-generation)
7475

@@ -84,6 +85,7 @@ namespace {
8485
if (fileProjectId != projectId) {
8586
throw std::runtime_error("ProjectId mismatch");
8687
}
88+
#endif
8789
return true;
8890
}
8991
return false;
@@ -486,8 +488,21 @@ int main() {
486488
throw std::runtime_error("Executable not found: " + exePath);
487489
if (!std::filesystem::exists(configPath))
488490
throw std::runtime_error("Config file not found: " + configPath);
491+
492+
std::vector<std::string> args = { "--no-startup-tests", "--config", configPath, "serve", "--yes" };
493+
bool watch = j[2];
494+
if (watch) {
495+
args.push_back("--watch");
496+
int interval = j[3];
497+
if (0 < interval) {
498+
args.push_back("--interval");
499+
args.push_back(std::to_string(interval));
500+
} else {
501+
LOG_MSG << "Invalid interval value, using to default value";
502+
}
503+
}
489504
ProcessManager proc;
490-
if (proc.startProcess(exePath, { "--no-startup-tests", "--config", configPath, "serve", "--yes" })) {
505+
if (proc.startProcess(exePath, args)) {
491506
res["status"] = "success";
492507
res["message"] = "Embedder started successfully";
493508
LOG_MSG << "Started embedder process" << proc.getProcessId() << "for projectId";
@@ -599,13 +614,22 @@ int main() {
599614
try {
600615
auto j = nlohmann::json::parse(data);
601616
if (!j.is_array() || j.size() == 0) {
602-
throw std::runtime_error("Invalid parameters for stopServe");
617+
throw std::runtime_error("Invalid parameters for checkPathExists");
618+
}
619+
std::string pathStr = j[0];
620+
fs::path p;
621+
if (pathStr.empty()) {
622+
// Treat empty path as current working directory
623+
p = fs::current_path();
624+
LOG_MSG << "checkPathExists: empty path => using current directory:" << fs::absolute(p).string();
625+
} else {
626+
p = fs::path(pathStr);
603627
}
604-
std::string path = j[0];
605-
if (fs::exists(path)) {
628+
if (fs::exists(p)) {
606629
res["status"] = "success";
630+
res["path"] = fs::absolute(p).string();
607631
} else {
608-
throw std::runtime_error("Path does not exist.");
632+
throw std::runtime_error("Path does not exist: " + p.string());
609633
}
610634
} catch (const std::exception &ex) {
611635
LOG_MSG << ex.what();

0 commit comments

Comments
 (0)