mirror of
https://github.com/cloudwu/skynet.git
synced 2026-07-24 20:23:06 +00:00
add bootstrap script
This commit is contained in:
@@ -4,8 +4,9 @@ logger = nil
|
||||
harbor = 1
|
||||
address = "127.0.0.1:2526"
|
||||
master = "127.0.0.1:2013"
|
||||
start = "main"
|
||||
preload = "preload example" -- run preload.lua helloworld before every lua service start
|
||||
start = "main" -- main script
|
||||
bootstrap = "snlua bootstrap" -- The service for bootstrap
|
||||
-- preload = "preload example" -- run preload.lua helloworld before every lua service start
|
||||
standalone = "0.0.0.0:2013"
|
||||
luaservice = root.."service/?.lua;"..root.."test/?.lua;"..root.."examples/?.lua"
|
||||
snax = root.."examples/?.lua;"..root.."test/?.lua"
|
||||
|
||||
@@ -4,7 +4,6 @@ local max_client = 64
|
||||
|
||||
skynet.start(function()
|
||||
print("Server start")
|
||||
local service = skynet.newservice("service_mgr")
|
||||
skynet.monitor "simplemonitor"
|
||||
local console = skynet.newservice("console")
|
||||
skynet.newservice("debug_console",8000)
|
||||
|
||||
@@ -210,7 +210,7 @@ function skynet.register(name)
|
||||
end
|
||||
|
||||
function skynet.name(name, handle)
|
||||
c.command("NAME", name .. " " .. handle)
|
||||
c.command("NAME", name .. " " .. skynet.address(handle))
|
||||
end
|
||||
|
||||
local self_handle
|
||||
@@ -255,7 +255,12 @@ function skynet.kill(name)
|
||||
end
|
||||
|
||||
function skynet.getenv(key)
|
||||
return c.command("GETENV",key)
|
||||
local ret = c.command("GETENV",key)
|
||||
if ret == "" then
|
||||
return
|
||||
else
|
||||
return ret
|
||||
end
|
||||
end
|
||||
|
||||
function skynet.setenv(key, value)
|
||||
|
||||
13
service/bootstrap.lua
Normal file
13
service/bootstrap.lua
Normal file
@@ -0,0 +1,13 @@
|
||||
local skynet = require "skynet"
|
||||
|
||||
skynet.start(function()
|
||||
local launcher = assert(skynet.launch("snlua launcher"))
|
||||
skynet.name(".launcher", launcher)
|
||||
|
||||
if skynet.getenv "standalone" then
|
||||
local smgr = assert(skynet.newservice "service_mgr")
|
||||
skynet.name("SERVICE", smgr)
|
||||
end
|
||||
assert(skynet.newservice(skynet.getenv "start" or "main"))
|
||||
skynet.exit()
|
||||
end)
|
||||
@@ -65,8 +65,11 @@ skynet.start(function()
|
||||
skynet.ret(skynet.pack(nil))
|
||||
end
|
||||
end)
|
||||
skynet.register(".service")
|
||||
if skynet.getenv "standalone" then
|
||||
skynet.register("SERVICE")
|
||||
local handle = skynet.localname ".service"
|
||||
if handle ~= 0 then
|
||||
skynet.error(".service is already register by ", skynet.address(handle))
|
||||
skynet.exit()
|
||||
else
|
||||
skynet.register(".service")
|
||||
end
|
||||
end)
|
||||
|
||||
@@ -16,7 +16,6 @@ skynet.register_protocol {
|
||||
end
|
||||
service_map[address] = false
|
||||
end
|
||||
print(string.format("[:%x] exit", address))
|
||||
end
|
||||
}
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@ struct skynet_config {
|
||||
const char * module_path;
|
||||
const char * master;
|
||||
const char * local;
|
||||
const char * start;
|
||||
const char * bootstrap;
|
||||
const char * standalone;
|
||||
};
|
||||
|
||||
|
||||
@@ -124,7 +124,7 @@ main(int argc, char *argv[]) {
|
||||
config.logger = optstring("logger",NULL);
|
||||
config.harbor = optint("harbor", 1);
|
||||
config.master = optstring("master","127.0.0.1:2012");
|
||||
config.start = optstring("start","main.lua");
|
||||
config.bootstrap = optstring("bootstrap","snlua bootstrap");
|
||||
config.local = optstring("address","127.0.0.1:2525");
|
||||
config.standalone = optstring("standalone",NULL);
|
||||
|
||||
|
||||
@@ -189,6 +189,14 @@ _start_master(const char * master) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void
|
||||
bootstrap(struct skynet_context * ctx, const char * cmdline) {
|
||||
if (!skynet_command(ctx, "LAUNCH", cmdline)) {
|
||||
fprintf(stderr, "Bootstrap error : %s\n", cmdline);
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
skynet_start(struct skynet_config * config) {
|
||||
skynet_harbor_init(config->harbor);
|
||||
@@ -201,29 +209,24 @@ skynet_start(struct skynet_config * config) {
|
||||
struct skynet_context *ctx;
|
||||
ctx = skynet_context_new("logger", config->logger);
|
||||
if (ctx == NULL) {
|
||||
fprintf(stderr,"launch logger error");
|
||||
fprintf(stderr,"launch logger error\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if (config->standalone) {
|
||||
if (_start_master(config->standalone)) {
|
||||
fprintf(stderr, "Init fail : mater");
|
||||
fprintf(stderr, "Init failed : master\n");
|
||||
return;
|
||||
}
|
||||
}
|
||||
// harbor must be init first
|
||||
if (skynet_harbor_start(config->master , config->local)) {
|
||||
fprintf(stderr, "Init fail : no master");
|
||||
fprintf(stderr, "Init failed : no master\n");
|
||||
return;
|
||||
}
|
||||
|
||||
ctx = skynet_context_new("snlua", "launcher");
|
||||
if (ctx) {
|
||||
skynet_command(ctx, "REG", ".launcher");
|
||||
ctx = skynet_context_new("snlua", config->start);
|
||||
}
|
||||
bootstrap(ctx, config->bootstrap);
|
||||
|
||||
_start(config->thread);
|
||||
skynet_socket_free();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user