# HG changeset patch # User jbe # Date 1427123156 -3600 # Node ID a2c733535b8ea565993d0580579d7742f83ca09a # Parent 0303fad417e2731ad57a9d262eac8983c13f25e0 Support intervals; Interactive shell requires application name now diff -r 0303fad417e2 -r a2c733535b8e demo-app/config/demo.lua --- a/demo-app/config/demo.lua Sun Mar 22 23:12:33 2015 +0100 +++ b/demo-app/config/demo.lua Mon Mar 23 16:05:56 2015 +0100 @@ -13,7 +13,8 @@ listen{ { proto = "tcp4", port = 8080 }, - { proto = "tcp6", port = 8080 } + { proto = "tcp6", port = 8080 }, + { proto = "interval", delay = 60, handler = function() io.stderr:write("Background job executed here\n") end } } --[[ diff -r 0303fad417e2 -r a2c733535b8e framework/bin/mcp.lua --- a/framework/bin/mcp.lua Sun Mar 22 23:12:33 2015 +0100 +++ b/framework/bin/mcp.lua Mon Mar 23 16:05:56 2015 +0100 @@ -108,15 +108,12 @@ arg2 == "-h" or arg2 == "--help" -- if first arg is provided by wrapper then helpout = io.stdout - elseif - #WEBMCP_CONFIG_NAMES < 1 or - (WEBMCP_MODE == "interactive") ~= (arg3 == "INTERACTIVE") - then + elseif #WEBMCP_CONFIG_NAMES < 1 then helpout = io.stderr end if helpout then - helpout:write("Usage: moonbridge -- /bin/mcp.lua [ ...]\n") - helpout:write(" or: lua -i /bin/mcp.lua INTERACTIVE [ ...]\n") + helpout:write("Usage: moonbridge [moonbr opts] -- /bin/mcp.lua [ ...]\n") + helpout:write(" or: lua -i [Lua opts] -- /bin/mcp.lua [ ...]\n") if helpout == io.stderr then return 1 else @@ -128,9 +125,7 @@ end WEBMCP_FRAMEWORK_PATH = append_trailing_slash(arg1) WEBMCP_BASE_PATH = append_trailing_slash(arg2) - if WEBMCP_MODE == "listen" then - WEBMCP_APP_NAME = arg3 - end + WEBMCP_APP_NAME = arg3 end -- check if framework path is correct @@ -302,19 +297,31 @@ local max_requests_per_fork = http_options.max_requests_per_fork or 100 local http = require("moonbridge_http") for i, listener in ipairs(listeners) do + local interval_handlers = {} + for j, entry in ipairs(listener) do + if entry.proto == "interval" then + local name = entry.name or "Unnamed interval #" .. #interval_handlers+1 + interval_handlers[name] = entry.handler + entry.name = name + end + end local request_count = 0 local function inner_handler(http_request) - request_count = request_count + 1 request.handler(http_request, request_count >= max_requests_per_fork) end local outer_handler = http.generate_handler(inner_handler, http_options) listener.prepare = postfork_init listener.connect = function(socket) - outer_handler(socket) + request_count = request_count + 1 + request.initialize() + if socket.interval then + interval_handlers[socket.interval]() + else + outer_handler(socket) + end return request_count < min_requests_per_fork end listener.finish = execute.finalizers moonbridge_listen(listener) end end - diff -r 0303fad417e2 -r a2c733535b8e framework/env/execute/_initializers.lua --- a/framework/env/execute/_initializers.lua Sun Mar 22 23:12:33 2015 +0100 +++ b/framework/env/execute/_initializers.lua Mon Mar 23 16:05:56 2015 +0100 @@ -6,9 +6,7 @@ execute._create_sorted_execution_list( function(add_by_path) add_by_path(initializer_path_element) - if WEBMCP_APP_NAME then -- allow for interactive mode - add_by_path(WEBMCP_APP_NAME, initializer_path_element) - end + add_by_path(WEBMCP_APP_NAME, initializer_path_element) end, function(full_path, relative_path) execute.file_path{ file_path = full_path } diff -r 0303fad417e2 -r a2c733535b8e framework/env/request/handler.lua --- a/framework/env/request/handler.lua Sun Mar 22 23:12:33 2015 +0100 +++ b/framework/env/request/handler.lua Mon Mar 23 16:05:56 2015 +0100 @@ -4,7 +4,7 @@ close -- boolean indicating whether the server should announce to close the connection ) -Called by mcp.lua to process an HTTP request. Performs some initializations, calls request.router(), and handles the request. +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. --]]-- @@ -19,14 +19,6 @@ end function request.handler(http_request, close) - _G.app = {} -- may be overwritten or modified by request initializers - do - request._in_progress = true -- NOTE: must be set to true before initializer functions are called - for i, func in ipairs(request._initializers) do - func() - end - end - request._http_request = http_request local path = http_request.path if path then diff -r 0303fad417e2 -r a2c733535b8e framework/env/request/initialize.lua --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/framework/env/request/initialize.lua Mon Mar 23 16:05:56 2015 +0100 @@ -0,0 +1,15 @@ +--[[-- +request.initialize() + +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. + +--]]-- +function request.initialize() + _G.app = {} -- may be overwritten or modified by request initializers + do + request._in_progress = true -- NOTE: must be set to true before initializer functions are called + for i, func in ipairs(request._initializers) do + func() + end + end +end