Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ngx.process.master_pid #158

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions lib/ngx/process.lua
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ ffi.cdef[[
int ngx_http_lua_ffi_enable_privileged_agent(char **err);
int ngx_http_lua_ffi_get_process_type(void);
void ngx_http_lua_ffi_process_signal_graceful_exit(void);
int ngx_http_lua_ffi_master_pid(void);
]]


Expand Down Expand Up @@ -60,4 +61,14 @@ function _M.signal_graceful_exit()
end


function _M.master_pid()
local pid = C.ngx_http_lua_ffi_master_pid()
if pid < 0 then
return nil
end

return pid
end


return _M
78 changes: 78 additions & 0 deletions t/process-master-pid.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# vim:set ft= ts=4 sw=4 et fdm=marker:
use lib 'lib';
use Test::Nginx::Socket::Lua;
use Cwd qw(cwd);

#worker_connections(1014);
#master_process_enabled(1);
#log_level('warn');

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to add the following line here to enable nginx master process:

master_on();

The test scaffold does not turn on nginx master process by default. Without this line, this test file fails on my side:

t/process-master-pid.t .. 1/12
#   Failed test 'TEST 1: ngx.process.master_pid - response_body_like - response is expected (false 69514)'
#   at /home/agentzh/git/lua-resty-core/../test-nginx/lib/Test/Nginx/Socket.pm line 1601.
#                   'false
# 69514
# '
#     doesn't match '(?^s:^true
# \d+$)'

#   Failed test 'TEST 1: ngx.process.master_pid - response_body_like - response is expected (false 69514)'
#   at /home/agentzh/git/lua-resty-core/../test-nginx/lib/Test/Nginx/Socket.pm line 1601.
#                   'false
# 69514
# '
#     doesn't match '(?^s:^true
# \d+$)'
# Looks like you failed 2 tests of 12.
t/process-master-pid.t .. Dubious, test returned 2 (wstat 512, 0x200)
Failed 2/12 subtests

Test Summary Report
-------------------
t/process-master-pid.t (Wstat: 512 Tests: 12 Failed: 2)
  Failed tests:  2, 8
  Non-zero exit status: 2
Files=1, Tests=12,  0 wallclock secs ( 0.01 usr  0.00 sys +  0.09 cusr  0.01 csys =  0.11 CPU)
Result: FAIL

repeat_each(2);

plan tests => repeat_each() * (blocks() * 6);

my $pwd = cwd();

our $HttpConfig = <<_EOC_;
lua_shared_dict dogs 1m;
lua_package_path "$pwd/lib/?.lua;../lua-resty-lrucache/lib/?.lua;;";
init_by_lua_block {
local verbose = false
if verbose then
local dump = require "jit.dump"
dump.on("b", "$Test::Nginx::Util::ErrLogFile")
else
local v = require "jit.v"
v.on("$Test::Nginx::Util::ErrLogFile")
end

require "resty.core"
-- jit.off()
}
_EOC_

#no_diff();
#no_long_string();
check_accum_error_log();
run_tests();

__DATA__

=== TEST 1: ngx.process.master_pid
--- http_config eval: $::HttpConfig
--- config
location = /t {
content_by_lua_block {
local process = require "ngx.process"

local v
local pid = process.master_pid
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is definitely wrong. You missed a pair of parentheses after process.master_pid. Have you actually run this test yourself?

This is another reason why you should make your PR pass the travis ci checks as @membphis suggested ;)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I'm not familar with travis ci, I will try to correct it ,thanks.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And here is not an error ,because pid will be called in then next line :
local v = pid()

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@chronolaw I see, but the pid function name is indeed confusing. How about using the variable name get_pid here? So that there could be no ambiguity here.

for i = 1, 400 do
v = pid()
end

local f = io.open(ngx.config.prefix().."/logs/nginx.pid", "r")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style: needs spaces around binary operators like ...

Also, io.open() may return an error, better wrap it with an assert() call.

if not f then
ngx.say(false)
else
local str = f:read("*l")
ngx.say(v == tonumber(str or "0"))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the sake of debugging test failures, I think we should also output the actual master pid in the .pid file in case of comparison failures.

f:close()
end
ngx.say(v)
}
}
--- request
GET /t
--- response_body_like chop
^true
\d+$
--- error_log eval
qr/\[TRACE \d+ content_by_lua\(nginx\.conf:\d+\):\d loop\]/
--- no_error_log
[error]
-- NYI:
stitch



Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should not put extra blank lines at the end of the .t test files. The tool reindex can automatically format your .t files:

https://github.com/openresty/openresty-devel-utils/blob/master/reindex