webmcp
changeset 316:a2c733535b8e
Support intervals; Interactive shell requires application name now
author | jbe |
---|---|
date | Mon Mar 23 16:05:56 2015 +0100 (2015-03-23) |
parents | 0303fad417e2 |
children | 732c4d53a823 |
files | demo-app/config/demo.lua framework/bin/mcp.lua framework/env/execute/_initializers.lua framework/env/request/handler.lua framework/env/request/initialize.lua |
line diff
1.1 --- a/demo-app/config/demo.lua Sun Mar 22 23:12:33 2015 +0100 1.2 +++ b/demo-app/config/demo.lua Mon Mar 23 16:05:56 2015 +0100 1.3 @@ -13,7 +13,8 @@ 1.4 1.5 listen{ 1.6 { proto = "tcp4", port = 8080 }, 1.7 - { proto = "tcp6", port = 8080 } 1.8 + { proto = "tcp6", port = 8080 }, 1.9 + { proto = "interval", delay = 60, handler = function() io.stderr:write("Background job executed here\n") end } 1.10 } 1.11 1.12 --[[
2.1 --- a/framework/bin/mcp.lua Sun Mar 22 23:12:33 2015 +0100 2.2 +++ b/framework/bin/mcp.lua Mon Mar 23 16:05:56 2015 +0100 2.3 @@ -108,15 +108,12 @@ 2.4 arg2 == "-h" or arg2 == "--help" -- if first arg is provided by wrapper 2.5 then 2.6 helpout = io.stdout 2.7 - elseif 2.8 - #WEBMCP_CONFIG_NAMES < 1 or 2.9 - (WEBMCP_MODE == "interactive") ~= (arg3 == "INTERACTIVE") 2.10 - then 2.11 + elseif #WEBMCP_CONFIG_NAMES < 1 then 2.12 helpout = io.stderr 2.13 end 2.14 if helpout then 2.15 - helpout:write("Usage: moonbridge -- <framework path>/bin/mcp.lua <framework path> <app base path> <app name> <config name> [<config name> ...]\n") 2.16 - helpout:write(" or: lua -i <framework path>/bin/mcp.lua <framework path> <app base path> INTERACTIVE <config name> [<config name> ...]\n") 2.17 + helpout:write("Usage: moonbridge [moonbr opts] -- <framework path>/bin/mcp.lua <framework path> <app base path> <app name> <config name> [<config name> ...]\n") 2.18 + helpout:write(" or: lua -i [Lua opts] -- <framework path>/bin/mcp.lua <framework path> <app base path> <app name> <config name> [<config name> ...]\n") 2.19 if helpout == io.stderr then 2.20 return 1 2.21 else 2.22 @@ -128,9 +125,7 @@ 2.23 end 2.24 WEBMCP_FRAMEWORK_PATH = append_trailing_slash(arg1) 2.25 WEBMCP_BASE_PATH = append_trailing_slash(arg2) 2.26 - if WEBMCP_MODE == "listen" then 2.27 - WEBMCP_APP_NAME = arg3 2.28 - end 2.29 + WEBMCP_APP_NAME = arg3 2.30 end 2.31 2.32 -- check if framework path is correct 2.33 @@ -302,19 +297,31 @@ 2.34 local max_requests_per_fork = http_options.max_requests_per_fork or 100 2.35 local http = require("moonbridge_http") 2.36 for i, listener in ipairs(listeners) do 2.37 + local interval_handlers = {} 2.38 + for j, entry in ipairs(listener) do 2.39 + if entry.proto == "interval" then 2.40 + local name = entry.name or "Unnamed interval #" .. #interval_handlers+1 2.41 + interval_handlers[name] = entry.handler 2.42 + entry.name = name 2.43 + end 2.44 + end 2.45 local request_count = 0 2.46 local function inner_handler(http_request) 2.47 - request_count = request_count + 1 2.48 request.handler(http_request, request_count >= max_requests_per_fork) 2.49 end 2.50 local outer_handler = http.generate_handler(inner_handler, http_options) 2.51 listener.prepare = postfork_init 2.52 listener.connect = function(socket) 2.53 - outer_handler(socket) 2.54 + request_count = request_count + 1 2.55 + request.initialize() 2.56 + if socket.interval then 2.57 + interval_handlers[socket.interval]() 2.58 + else 2.59 + outer_handler(socket) 2.60 + end 2.61 return request_count < min_requests_per_fork 2.62 end 2.63 listener.finish = execute.finalizers 2.64 moonbridge_listen(listener) 2.65 end 2.66 end 2.67 -
3.1 --- a/framework/env/execute/_initializers.lua Sun Mar 22 23:12:33 2015 +0100 3.2 +++ b/framework/env/execute/_initializers.lua Mon Mar 23 16:05:56 2015 +0100 3.3 @@ -6,9 +6,7 @@ 3.4 execute._create_sorted_execution_list( 3.5 function(add_by_path) 3.6 add_by_path(initializer_path_element) 3.7 - if WEBMCP_APP_NAME then -- allow for interactive mode 3.8 - add_by_path(WEBMCP_APP_NAME, initializer_path_element) 3.9 - end 3.10 + add_by_path(WEBMCP_APP_NAME, initializer_path_element) 3.11 end, 3.12 function(full_path, relative_path) 3.13 execute.file_path{ file_path = full_path }
4.1 --- a/framework/env/request/handler.lua Sun Mar 22 23:12:33 2015 +0100 4.2 +++ b/framework/env/request/handler.lua Mon Mar 23 16:05:56 2015 +0100 4.3 @@ -4,7 +4,7 @@ 4.4 close -- boolean indicating whether the server should announce to close the connection 4.5 ) 4.6 4.7 -Called by mcp.lua to process an HTTP request. Performs some initializations, calls request.router(), and handles the request. 4.8 +Called by mcp.lua to process an HTTP request. Calls request.router(), and handles the request. Note: request initializers will have to be (automatically) executed before this function is invoked by mcp.lua. 4.9 4.10 --]]-- 4.11 4.12 @@ -19,14 +19,6 @@ 4.13 end 4.14 4.15 function request.handler(http_request, close) 4.16 - _G.app = {} -- may be overwritten or modified by request initializers 4.17 - do 4.18 - request._in_progress = true -- NOTE: must be set to true before initializer functions are called 4.19 - for i, func in ipairs(request._initializers) do 4.20 - func() 4.21 - end 4.22 - end 4.23 - 4.24 request._http_request = http_request 4.25 local path = http_request.path 4.26 if path then
5.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 5.2 +++ b/framework/env/request/initialize.lua Mon Mar 23 16:05:56 2015 +0100 5.3 @@ -0,0 +1,15 @@ 5.4 +--[[-- 5.5 +request.initialize() 5.6 + 5.7 +Executes all request initializers. Request initializers are added (and executed) using the request.for_each(...) call. Calling request.configure(...) before invoking request.initialize() for the first time also adds a request initializer. 5.8 + 5.9 +--]]-- 5.10 +function request.initialize() 5.11 + _G.app = {} -- may be overwritten or modified by request initializers 5.12 + do 5.13 + request._in_progress = true -- NOTE: must be set to true before initializer functions are called 5.14 + for i, func in ipairs(request._initializers) do 5.15 + func() 5.16 + end 5.17 + end 5.18 +end