From f6e67b7493a3b937c346548fad819f6b63d54d4e Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Tue, 29 Apr 2014 13:43:40 +0800 Subject: [PATCH] add bootstrap script --- examples/config | 5 +++-- examples/main.lua | 1 - lualib/skynet.lua | 9 +++++++-- service/bootstrap.lua | 13 +++++++++++++ service/service_mgr.lua | 9 ++++++--- service/simplemonitor.lua | 1 - skynet-src/skynet_imp.h | 2 +- skynet-src/skynet_main.c | 2 +- skynet-src/skynet_start.c | 21 ++++++++++++--------- 9 files changed, 43 insertions(+), 20 deletions(-) create mode 100644 service/bootstrap.lua diff --git a/examples/config b/examples/config index f9cca2ea..1dcb7014 100644 --- a/examples/config +++ b/examples/config @@ -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" diff --git a/examples/main.lua b/examples/main.lua index 58ff380f..7731bdab 100644 --- a/examples/main.lua +++ b/examples/main.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) diff --git a/lualib/skynet.lua b/lualib/skynet.lua index 6dde1946..34d427df 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -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) diff --git a/service/bootstrap.lua b/service/bootstrap.lua new file mode 100644 index 00000000..cf297687 --- /dev/null +++ b/service/bootstrap.lua @@ -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) diff --git a/service/service_mgr.lua b/service/service_mgr.lua index 7f771f3f..f387cf36 100644 --- a/service/service_mgr.lua +++ b/service/service_mgr.lua @@ -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) diff --git a/service/simplemonitor.lua b/service/simplemonitor.lua index 56653d0c..39a7c17c 100644 --- a/service/simplemonitor.lua +++ b/service/simplemonitor.lua @@ -16,7 +16,6 @@ skynet.register_protocol { end service_map[address] = false end - print(string.format("[:%x] exit", address)) end } diff --git a/skynet-src/skynet_imp.h b/skynet-src/skynet_imp.h index 63b6c139..cdfea566 100644 --- a/skynet-src/skynet_imp.h +++ b/skynet-src/skynet_imp.h @@ -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; }; diff --git a/skynet-src/skynet_main.c b/skynet-src/skynet_main.c index 7c318668..15c29bce 100644 --- a/skynet-src/skynet_main.c +++ b/skynet-src/skynet_main.c @@ -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); diff --git a/skynet-src/skynet_start.c b/skynet-src/skynet_start.c index 764c1a05..0128d444 100644 --- a/skynet-src/skynet_start.c +++ b/skynet-src/skynet_start.c @@ -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(); } -