Properly reap child processes.
This commit is contained in:
parent
d9c179f32a
commit
61241defa1
2 changed files with 15 additions and 14 deletions
8
Makefile
8
Makefile
|
|
@ -4,17 +4,17 @@ FILES = netdata_tsrelay.nim
|
||||||
default: development
|
default: development
|
||||||
|
|
||||||
debug: ${FILES}
|
debug: ${FILES}
|
||||||
nim --assertions:on --threads:on --nimcache:.cache c ${FILES}
|
nim --assertions:on --nimcache:.cache c ${FILES}
|
||||||
|
|
||||||
development: ${FILES}
|
development: ${FILES}
|
||||||
# can use gdb with this...
|
# can use gdb with this...
|
||||||
nim --debugInfo --threads:on --linedir:on -d:testing -d:nimTypeNames --nimcache:.cache c ${FILES}
|
nim --debugInfo --linedir:on -d:testing -d:nimTypeNames --nimcache:.cache c ${FILES}
|
||||||
|
|
||||||
debugger: ${FILES}
|
debugger: ${FILES}
|
||||||
nim --debugger:on --threads:on --nimcache:.cache c ${FILES}
|
nim --debugger:on --nimcache:.cache c ${FILES}
|
||||||
|
|
||||||
release: ${FILES}
|
release: ${FILES}
|
||||||
nim -d:release --opt:speed --parallelBuild:0 --threads:on --nimcache:.cache c ${FILES}
|
nim -d:release --opt:speed --parallelBuild:0 --nimcache:.cache c ${FILES}
|
||||||
|
|
||||||
docs:
|
docs:
|
||||||
nim doc ${FILES}
|
nim doc ${FILES}
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,4 @@
|
||||||
# vim: set et nosta sw=4 ts=4 :
|
# vim: set et nosta sw=4 ts=4 :
|
||||||
# im: set et nosta sw=4 ts=4 ft=nim :
|
|
||||||
#
|
#
|
||||||
# Copyright (c) 2018, Mahlon E. Smith <mahlon@martini.nu>
|
# Copyright (c) 2018, Mahlon E. Smith <mahlon@martini.nu>
|
||||||
# All rights reserved.
|
# All rights reserved.
|
||||||
|
|
@ -76,6 +75,7 @@ type
|
||||||
# Global configuration
|
# Global configuration
|
||||||
var conf: Config
|
var conf: Config
|
||||||
|
|
||||||
|
|
||||||
proc hl( msg: string, fg: ForegroundColor, bright=false ): string =
|
proc hl( msg: string, fg: ForegroundColor, bright=false ): string =
|
||||||
## Quick wrapper for color formatting a string, since the 'terminal'
|
## Quick wrapper for color formatting a string, since the 'terminal'
|
||||||
## module only deals with stdout directly.
|
## module only deals with stdout directly.
|
||||||
|
|
@ -171,8 +171,7 @@ proc write_to_database( samples: seq[ JsonNode ] ): void =
|
||||||
|
|
||||||
|
|
||||||
proc process( client: Socket, address: string ): void =
|
proc process( client: Socket, address: string ): void =
|
||||||
## Do the work for a connected client within a thread.
|
## Do the work for a connected client within child process.
|
||||||
## Returns the formatted json data keyed on sample time.
|
|
||||||
let t0 = cpu_time()
|
let t0 = cpu_time()
|
||||||
var raw_data = client.fetch_data
|
var raw_data = client.fetch_data
|
||||||
|
|
||||||
|
|
@ -205,8 +204,14 @@ proc serverloop( conf: Config ): void =
|
||||||
## Open a database connection, bind to the listening socket,
|
## Open a database connection, bind to the listening socket,
|
||||||
## and start serving incoming netdata streams.
|
## and start serving incoming netdata streams.
|
||||||
let db = open( "", "", "", conf.dbopts )
|
let db = open( "", "", "", conf.dbopts )
|
||||||
if conf.verbose: echo( "Successfully tested connection to the backend database.".hl( fgGreen ) )
|
|
||||||
db.close
|
db.close
|
||||||
|
if conf.verbose: echo( "Successfully tested connection to the backend database.".hl( fgGreen ) )
|
||||||
|
|
||||||
|
# Ensure children are properly reaped.
|
||||||
|
#
|
||||||
|
var sa: Sigaction
|
||||||
|
sa.sa_handler = SIG_IGN
|
||||||
|
discard sigaction( SIGCHLD, sa )
|
||||||
|
|
||||||
# Setup listening socket.
|
# Setup listening socket.
|
||||||
#
|
#
|
||||||
|
|
@ -230,9 +235,9 @@ proc serverloop( conf: Config ): void =
|
||||||
var
|
var
|
||||||
client = new Socket
|
client = new Socket
|
||||||
address = ""
|
address = ""
|
||||||
status: cint = 0
|
|
||||||
|
|
||||||
server.acceptAddr( client, address ) # block
|
# Block, waiting for new connections.
|
||||||
|
server.acceptAddr( client, address )
|
||||||
|
|
||||||
if fork() == 0:
|
if fork() == 0:
|
||||||
server.close
|
server.close
|
||||||
|
|
@ -240,8 +245,6 @@ proc serverloop( conf: Config ): void =
|
||||||
quit( 0 )
|
quit( 0 )
|
||||||
|
|
||||||
client.close
|
client.close
|
||||||
|
|
||||||
discard waitpid( P_ALL, status, WNOHANG ) # reap all previous children
|
|
||||||
when defined( testing ): dumpNumberOfInstances()
|
when defined( testing ): dumpNumberOfInstances()
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -294,9 +297,7 @@ proc parse_cmdline: Config =
|
||||||
|
|
||||||
when isMainModule:
|
when isMainModule:
|
||||||
system.addQuitProc( resetAttributes )
|
system.addQuitProc( resetAttributes )
|
||||||
|
|
||||||
conf = parse_cmdline()
|
conf = parse_cmdline()
|
||||||
if conf.debug: echo hl( $conf, fgYellow )
|
if conf.debug: echo hl( $conf, fgYellow )
|
||||||
|
|
||||||
serverloop( conf )
|
serverloop( conf )
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue