remove pidfile on exit, handle SIGTERM/ctrl+c
This commit is contained in:
parent
a03e08b99b
commit
105867be58
|
@ -1,4 +1,4 @@
|
||||||
import json, tables, os, httpcore, asynchttpserver, asyncdispatch, uri, strutils
|
import json, tables, os, httpcore, asynchttpserver, asyncdispatch, uri, strutils, posix
|
||||||
import types, endpoints/[suggestions, journeys, moreJourneys, refreshJourney]
|
import types, endpoints/[suggestions, journeys, moreJourneys, refreshJourney]
|
||||||
|
|
||||||
when not defined(release):
|
when not defined(release):
|
||||||
|
@ -7,6 +7,15 @@ when not defined(release):
|
||||||
|
|
||||||
var endpoints {.threadvar.}: Table[string, proc(data: JsonNode): Future[JsonNode] {.gcsafe.}]
|
var endpoints {.threadvar.}: Table[string, proc(data: JsonNode): Future[JsonNode] {.gcsafe.}]
|
||||||
|
|
||||||
|
proc removePidFile() =
|
||||||
|
if getEnv("PID_FILE") != "":
|
||||||
|
removeFile(getEnv("PID_FILE"))
|
||||||
|
|
||||||
|
proc CtrlCHook() {.noconv.} =
|
||||||
|
echo "Ctrl+C fired! \nStopping Server now!"
|
||||||
|
removePidFile()
|
||||||
|
quit()
|
||||||
|
|
||||||
proc handleRequest(req: Request) {.async,gcsafe.} =
|
proc handleRequest(req: Request) {.async,gcsafe.} =
|
||||||
var resp = NimHttpResponse(
|
var resp = NimHttpResponse(
|
||||||
code: Http404,
|
code: Http404,
|
||||||
|
@ -39,16 +48,16 @@ proc handleRequest(req: Request) {.async,gcsafe.} =
|
||||||
await req.respond(resp.code, resp.msg, resp.headers)
|
await req.respond(resp.code, resp.msg, resp.headers)
|
||||||
|
|
||||||
proc main() =
|
proc main() =
|
||||||
endpoints = initTable[string, proc(data: JsonNode): Future[JsonNode] {.gcsafe.}]()
|
setControlCHook(CtrlCHook)
|
||||||
|
|
||||||
endpoints[$HttpGet & "/suggestions"] = suggestionsEndpoint
|
onSignal(SIGTERM):
|
||||||
endpoints[$HttpGet & "/journeys"] = journeysEndpoint
|
echo "Got SIGTERM! \nStopping Server now!"
|
||||||
endpoints[$HttpGet & "/moreJourneys"] = moreJourneysEndpoint
|
removePidFile()
|
||||||
endpoints[$HttpGet & "/refreshJourney"] = refreshJourneyEndpoint
|
quit()
|
||||||
|
|
||||||
if getEnv("CACHE_PATH") == "":
|
if getEnv("CACHE_PATH") == "":
|
||||||
echo "CACHE_PATH not set! Bye...."
|
echo "CACHE_PATH not set! Bye...."
|
||||||
quit()
|
quit(1)
|
||||||
else:
|
else:
|
||||||
discard existsOrCreateDir(getEnv("CACHE_PATH"))
|
discard existsOrCreateDir(getEnv("CACHE_PATH"))
|
||||||
|
|
||||||
|
@ -56,6 +65,21 @@ proc main() =
|
||||||
if getEnv("PORT") != "":
|
if getEnv("PORT") != "":
|
||||||
port = parseInt(getEnv("PORT"))
|
port = parseInt(getEnv("PORT"))
|
||||||
|
|
||||||
|
if getEnv("PID_FILE") != "":
|
||||||
|
try:
|
||||||
|
writeFile(getEnv("PID_FILE"), $getpid())
|
||||||
|
except:
|
||||||
|
echo "Can't write pid file!"
|
||||||
|
quit(1)
|
||||||
|
|
||||||
|
|
||||||
|
endpoints = initTable[string, proc(data: JsonNode): Future[JsonNode] {.gcsafe.}]()
|
||||||
|
|
||||||
|
endpoints[$HttpGet & "/suggestions"] = suggestionsEndpoint
|
||||||
|
endpoints[$HttpGet & "/journeys"] = journeysEndpoint
|
||||||
|
endpoints[$HttpGet & "/moreJourneys"] = moreJourneysEndpoint
|
||||||
|
endpoints[$HttpGet & "/refreshJourney"] = refreshJourneyEndpoint
|
||||||
|
|
||||||
let server = newAsyncHttpServer()
|
let server = newAsyncHttpServer()
|
||||||
|
|
||||||
when not defined(release):
|
when not defined(release):
|
||||||
|
|
Loading…
Reference in a new issue