Skip to content

Commit 002b68e

Browse files
committed
feature: added new API function ngx.worker.exiting() for testing if the current worker process is exiting.
1 parent 4d6dc58 commit 002b68e

File tree

6 files changed

+95
-1
lines changed

6 files changed

+95
-1
lines changed

config

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ NGX_ADDON_SRCS="$NGX_ADDON_SRCS \
223223
$ngx_addon_dir/src/ngx_http_lua_uthread.c \
224224
$ngx_addon_dir/src/ngx_http_lua_timer.c \
225225
$ngx_addon_dir/src/ngx_http_lua_config.c \
226+
$ngx_addon_dir/src/ngx_http_lua_worker.c \
226227
"
227228

228229
NGX_ADDON_DEPS="$NGX_ADDON_DEPS \
@@ -274,6 +275,7 @@ NGX_ADDON_DEPS="$NGX_ADDON_DEPS \
274275
$ngx_addon_dir/src/ngx_http_lua_uthread.h \
275276
$ngx_addon_dir/src/ngx_http_lua_timer.h \
276277
$ngx_addon_dir/src/ngx_http_lua_config.h \
278+
$ngx_addon_dir/src/ngx_http_lua_worker.h \
277279
"
278280

279281
CFLAGS="$CFLAGS -DNDK_SET_VAR"

src/ngx_http_lua_util.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
#include "ngx_http_lua_contentby.h"
4848
#include "ngx_http_lua_timer.h"
4949
#include "ngx_http_lua_config.h"
50+
#include "ngx_http_lua_worker.h"
5051

5152

5253
#if 1
@@ -786,6 +787,7 @@ ngx_http_lua_inject_ngx_api(ngx_conf_t *cf, lua_State *L)
786787
ngx_http_lua_inject_uthread_api(cf->log, L);
787788
ngx_http_lua_inject_timer_api(L);
788789
ngx_http_lua_inject_config_api(L);
790+
ngx_http_lua_inject_worker_api(L);
789791

790792
ngx_http_lua_inject_misc_api(L);
791793

src/ngx_http_lua_worker.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
2+
/*
3+
* Copyright (C) Yichun Zhang (agentzh)
4+
*/
5+
6+
7+
#ifndef DDEBUG
8+
#define DDEBUG 0
9+
#endif
10+
#include "ddebug.h"
11+
12+
13+
#include "ngx_http_lua_worker.h"
14+
15+
16+
static int ngx_http_lua_ngx_worker_exiting(lua_State *L);
17+
18+
19+
void
20+
ngx_http_lua_inject_worker_api(lua_State *L)
21+
{
22+
lua_createtable(L, 0 /* narr */, 1 /* nrec */); /* ngx.timer. */
23+
24+
lua_pushcfunction(L, ngx_http_lua_ngx_worker_exiting);
25+
lua_setfield(L, -2, "exiting");
26+
27+
lua_setfield(L, -2, "worker");
28+
}
29+
30+
31+
static int
32+
ngx_http_lua_ngx_worker_exiting(lua_State *L)
33+
{
34+
lua_pushboolean(L, ngx_exiting);
35+
return 1;
36+
}

src/ngx_http_lua_worker.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
/*
3+
* Copyright (C) Yichun Zhang (agentzh)
4+
*/
5+
6+
7+
#ifndef _NGX_HTTP_LUA_WORKER_H_INCLUDED_
8+
#define _NGX_HTTP_LUA_WORKER_H_INCLUDED_
9+
10+
11+
#include "ngx_http_lua_common.h"
12+
13+
14+
void ngx_http_lua_inject_worker_api(lua_State *L);
15+
16+
17+
#endif /* _NGX_HTTP_LUA_WORKER_H_INCLUDED_ */

t/109-timer-hup.t

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ our $StapScript = $t::StapThread::StapScript;
2828

2929
repeat_each(2);
3030

31-
plan tests => repeat_each() * 59;
31+
plan tests => repeat_each() * 61;
3232

3333
#no_diff();
3434
no_long_string();
@@ -233,10 +233,12 @@ failed to register a new timer after reload: process exiting, context: ngx.timer
233233
234234
local function g(premature)
235235
print("g: timer prematurely expired: ", premature)
236+
print("g: exiting=", ngx.worker.exiting())
236237
end
237238
238239
local function f(premature)
239240
print("f: timer prematurely expired: ", premature)
241+
print("f: exiting=", ngx.worker.exiting())
240242
local ok, err = ngx.timer.at(0, g)
241243
if not ok then
242244
print("f: failed to register a new timer after reload: ", err)
@@ -274,5 +276,7 @@ lua ngx.timer expired
274276
http lua close fake http connection
275277
f: timer prematurely expired: true
276278
f: registered a new timer after reload
279+
f: exiting=true
277280
g: timer prematurely expired: true
281+
g: exiting=true
278282

t/122-worker.t

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# vim:set ft= ts=4 sw=4 et fdm=marker:
2+
3+
use t::TestNginxLua;
4+
5+
#worker_connections(1014);
6+
#master_on();
7+
#workers(2);
8+
#log_level('warn');
9+
10+
repeat_each(2);
11+
12+
plan tests => repeat_each() * (blocks() * 3);
13+
14+
#no_diff();
15+
no_long_string();
16+
run_tests();
17+
18+
__DATA__
19+
20+
=== TEST 1: content_by_lua
21+
--- config
22+
location /lua {
23+
content_by_lua '
24+
ngx.say("worker exiting: ", ngx.worker.exiting())
25+
';
26+
}
27+
--- request
28+
GET /lua
29+
--- response_body
30+
worker exiting: false
31+
--- no_error_log
32+
[error]
33+

0 commit comments

Comments
 (0)