2026-10-23 18:20:04 +02:00
|
|
|
// Living example server entry: serves the demo app on loopback.
|
2026-10-24 03:10:28 +02:00
|
|
|
// Usage: living-server [port] [--no-locust] (default port 8080)
|
|
|
|
|
// The Locust load-test UI (web UI on 8089) runs as a process worker under
|
|
|
|
|
// the real molo::supervisor::Supervisor: started with the server,
|
|
|
|
|
// restarted on crash, stopped with it.
|
2026-10-23 18:20:04 +02:00
|
|
|
|
2026-10-24 03:10:28 +02:00
|
|
|
#include <atomic>
|
2026-10-23 18:20:04 +02:00
|
|
|
#include <chrono>
|
2026-10-24 03:10:28 +02:00
|
|
|
#include <csignal>
|
2026-10-23 18:20:04 +02:00
|
|
|
#include <iostream>
|
|
|
|
|
#include <string>
|
|
|
|
|
#include <thread>
|
|
|
|
|
|
|
|
|
|
#include "app/App.hpp"
|
|
|
|
|
#include "fw/Server.hpp"
|
2026-10-24 03:10:28 +02:00
|
|
|
#include "supervisor/common/clock.hpp"
|
|
|
|
|
#include "supervisor/common/observer.hpp"
|
|
|
|
|
#include "supervisor/supervisor/process_work.hpp"
|
|
|
|
|
#include "supervisor/supervisor/supervisor.hpp"
|
|
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
std::atomic<bool> g_stop{false};
|
|
|
|
|
void on_signal(int) { g_stop = true; }
|
|
|
|
|
} // namespace
|
2026-10-23 18:20:04 +02:00
|
|
|
|
|
|
|
|
int main(int argc, char** argv) {
|
|
|
|
|
int port = 8080;
|
2026-10-24 03:10:28 +02:00
|
|
|
bool with_locust = true;
|
|
|
|
|
for (int i = 2; i < argc; ++i) {
|
|
|
|
|
if (std::string(argv[i]) == "--no-locust") with_locust = false;
|
|
|
|
|
}
|
|
|
|
|
std::signal(SIGINT, on_signal);
|
|
|
|
|
std::signal(SIGTERM, on_signal);
|
2026-10-23 18:20:04 +02:00
|
|
|
if (argc > 1) {
|
|
|
|
|
try {
|
|
|
|
|
port = std::stoi(argv[1]);
|
|
|
|
|
} catch (...) {
|
|
|
|
|
std::cerr << "bad port\n";
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
molo::example::app::Services services;
|
|
|
|
|
auto app = molo::example::app::build_application(services);
|
|
|
|
|
molo::example::fw::Server server(app);
|
2026-10-24 01:59:27 +02:00
|
|
|
server.set_stats(services.stats);
|
2026-10-23 18:20:04 +02:00
|
|
|
if (!server.start("127.0.0.1", port)) {
|
|
|
|
|
std::cerr << "bind failed\n";
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
std::cout << "living example on http://127.0.0.1:" << server.port() << "\n";
|
2026-10-24 03:10:28 +02:00
|
|
|
molo::common::SystemClock clock;
|
|
|
|
|
molo::supervisor::Supervisor supervisor(
|
|
|
|
|
clock, std::make_shared<molo::common::NullObserver>());
|
|
|
|
|
if (with_locust) {
|
|
|
|
|
// Paths are cwd-relative like Services::static_root (run from repo root).
|
|
|
|
|
molo::supervisor::GroupSpec group;
|
|
|
|
|
group.name = "locust";
|
|
|
|
|
supervisor.add_group(
|
|
|
|
|
group, molo::supervisor::make_process_work(
|
|
|
|
|
{{"locust", "-f", "example/locustfile.py", "--host",
|
|
|
|
|
"http://127.0.0.1:" + std::to_string(server.port()),
|
|
|
|
|
"--web-host", "127.0.0.1", "--web-port", "8089"}}));
|
|
|
|
|
supervisor.start_all();
|
|
|
|
|
std::cout << "locust (supervised) on http://127.0.0.1:8089\n";
|
|
|
|
|
}
|
2026-10-23 18:20:04 +02:00
|
|
|
std::cout << "press Ctrl-C to stop\n";
|
2026-10-24 03:10:28 +02:00
|
|
|
while (server.running() && !g_stop) {
|
2026-10-23 18:20:04 +02:00
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(200));
|
|
|
|
|
}
|
2026-10-24 03:10:28 +02:00
|
|
|
supervisor.shutdown(0);
|
2026-10-23 18:20:04 +02:00
|
|
|
return 0;
|
|
|
|
|
}
|