mirror of
https://github.com/logos-storage/logos-storage-network-crawler.git
synced 2026-01-23 15:53:08 +00:00
Sets up application starting and stopping
This commit is contained in:
parent
7eef86f759
commit
26a11e5565
@ -1,5 +1,60 @@
|
||||
import
|
||||
./codexcrawler/todo
|
||||
import pkg/chronicles
|
||||
import pkg/chronos
|
||||
|
||||
import ./codexcrawler/main
|
||||
when defined(posix):
|
||||
import system/ansi_c
|
||||
|
||||
type
|
||||
ApplicationStatus {.pure.} = enum
|
||||
Stopped,
|
||||
Stopping,
|
||||
Running
|
||||
|
||||
Application = ref object
|
||||
status: ApplicationStatus
|
||||
|
||||
proc run(app: Application) =
|
||||
app.status = ApplicationStatus.Running
|
||||
|
||||
waitFor runApplication()
|
||||
|
||||
while app.status == ApplicationStatus.Running:
|
||||
try:
|
||||
chronos.poll()
|
||||
except Exception as exc:
|
||||
error "Unhandled exception", msg = exc.msg
|
||||
quit QuitFailure
|
||||
notice "Done"
|
||||
|
||||
when isMainModule:
|
||||
run()
|
||||
let app = Application()
|
||||
|
||||
# Stopping code must be in scope of app declaration.
|
||||
# Else capture of the instance is not allowed due to {.noconv.}.
|
||||
proc onStopSignal() =
|
||||
app.status = ApplicationStatus.Stopping
|
||||
notice "Stopping Crawler..."
|
||||
|
||||
proc controlCHandler() {.noconv.} =
|
||||
when defined(windows):
|
||||
# workaround for https://github.com/nim-lang/Nim/issues/4057
|
||||
try:
|
||||
setupForeignThreadGc()
|
||||
except Exception as exc: raiseAssert exc.msg
|
||||
notice "Shutting down after having received SIGINT"
|
||||
onStopSignal()
|
||||
|
||||
try:
|
||||
setControlCHook(controlCHandler)
|
||||
except Exception as exc:
|
||||
warn "Cannot set ctrl-c handler", msg = exc.msg
|
||||
|
||||
when defined(posix):
|
||||
proc SIGTERMHandler(signal: cint) {.noconv.} =
|
||||
notice "Shutting down after having received SIGTERM"
|
||||
onStopSignal()
|
||||
|
||||
c_signal(ansi_c.SIGTERM, SIGTERMHandler)
|
||||
|
||||
app.run()
|
||||
|
||||
18
codexcrawler/main.nim
Normal file
18
codexcrawler/main.nim
Normal file
@ -0,0 +1,18 @@
|
||||
import pkg/chronicles
|
||||
import pkg/chronos
|
||||
|
||||
logScope:
|
||||
topics = "main"
|
||||
|
||||
proc runApplication*() {.async.} =
|
||||
proc aaa() {.async.} =
|
||||
while true:
|
||||
notice "a"
|
||||
await sleepAsync(1000)
|
||||
|
||||
asyncSpawn aaa()
|
||||
|
||||
await sleepAsync(1000)
|
||||
|
||||
notice "b"
|
||||
|
||||
@ -1,2 +0,0 @@
|
||||
proc run*() =
|
||||
echo "Run!"
|
||||
Loading…
x
Reference in New Issue
Block a user