Skip to content

Commit

Permalink
refactor: make jsonrpc remote server quieter by default
Browse files Browse the repository at this point in the history
  • Loading branch information
edubart committed Dec 10, 2024
1 parent 30f793e commit e2f9bae
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 23 deletions.
32 changes: 11 additions & 21 deletions src/cartesi-machine.lua
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,10 @@ where options are:
--version-json
display cartesi machine semantic version and exit.
--remote-spawn[=<ip>:<port>]
spawns a remote cartesi machine listening to <ip>:<port> and uses it,
in case the address is omitted, it defaults to "127.0.0.1:0"
--remote-spawn
spawns a remote cartesi machine,
when --remote-address is specified, it listens on the specified address,
otherwise it listens on "127.0.0.1:0".
--remote-address=<ip>:<port>
use a remote cartesi machine listening to <ip>:<port> instead of
Expand Down Expand Up @@ -1307,17 +1308,10 @@ local options = {
end,
},
{
"^%-%-remote%-spawn(%=?)(.*)$",
function(o, v)
"^%-%-remote%-spawn$",
function(o)
if not o then return false end
if o == "=" then
if not v or #v < 1 then return false end
remote_spawn = v
elseif #v ~= 0 then
return false
else
remote_spawn = "127.0.0.1:0"
end
remote_spawn = true
return true
end,
},
Expand Down Expand Up @@ -1620,7 +1614,6 @@ end
local function new_machine()
assert(not remote_health_check or remote_address, "missing remote address")
if remote_address then
stderr("Connecting to JSONRPC remote cartesi machine at '%s'\n", remote_address)
local jsonrpc = require("cartesi.jsonrpc")
local new_m = assert(jsonrpc.connect_server(remote_address))
if remote_fork then
Expand All @@ -1632,16 +1625,16 @@ local function new_machine()
stderr("Rebound forked JSONRPC remote cartesi machine at '%s'\n", remote_fork)
end
end
local v = assert(new_m:get_server_version())
stderr("Connected to JSONRPC remote cartesi machine, version is %d.%d.%d\n", v.major, v.minor, v.patch)
if remote_health_check then os.exit(0, true) end
stderr("Connected to JSONRPC remote cartesi machine at '%s'\n", remote_address)
local shutdown = function() new_m:shutdown_server() end
setmetatable(remote_closer, {
__gc = function()
local address = new_m:get_server_address()
if remote_shutdown then
local ok, err = pcall(shutdown)
if ok then
stderr("Shutted down JSONRPC remote cartesi machine at '%s'\n", address)
stderr("Shutdown JSONRPC remote cartesi machine at '%s'\n", address)
else
stderr("Failed to shutdown JSONRPC remote cartesi machine: %s\n", err)
end
Expand All @@ -1653,7 +1646,6 @@ local function new_machine()
end
end,
})
if remote_health_check then os.exit(0, true) end
return new_m
else
return cartesi.new()
Expand All @@ -1673,10 +1665,8 @@ local runtime_config = {
}

if remote_spawn then
assert(not remote_address, "--remote-spawn cannot be used with --remote-address")
local jsonrpc = require("cartesi.jsonrpc")
stderr("Spawning JSONRPC remote cartesi machine at '%s'\n", remote_spawn)
local _ <close>, address, pid = jsonrpc.spawn_server(remote_spawn)
local _ <close>, address, pid = jsonrpc.spawn_server(remote_address)
stderr("Spawned JSONRPC remote cartesi machine at '%s' with pid %d\n", address, pid)
remote_address = address
end
Expand Down
2 changes: 1 addition & 1 deletion src/clua-cartesi-jsonrpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ static int mod_connect_server(lua_State *L) {
/// \brief This is the jsonrpc.spawn_server() method implementation.
static int mod_spawn_server(lua_State *L) {
lua_settop(L, 1);
const char *address = !lua_isnil(L, 1) ? luaL_checkstring(L, 1) : "127.0.0.1:0";
const char *address = luaL_optstring(L, 1, "127.0.0.1:0");
lua_newtable(L); // server
auto &m = clua_push_to(L, clua_managed_cm_ptr<cm_machine>(nullptr)); // server object
const char *bound_address = nullptr;
Expand Down
2 changes: 1 addition & 1 deletion src/jsonrpc-remote-machine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1642,7 +1642,7 @@ static bool stringval(const char *pre, const char *str, const char **val) {

static void init_logger(const char *strlevel) {
using namespace slog;
severity_level level = severity_level::info;
severity_level level = severity_level::warning;
if (strlevel == nullptr) {
strlevel = std::getenv("REMOTE_CARTESI_MACHINE_LOG_LEVEL");
}
Expand Down

0 comments on commit e2f9bae

Please sign in to comment.