mirror of
https://github.com/status-im/nim-dagger.git
synced 2025-01-13 08:04:28 +00:00
fix(codexnode): ensure timer loop is asyncSpawned (#1038)
* fix(codexnode): stop clock after validator stops * fix(timer): ensure timer loop is asyncSpawned
This commit is contained in:
parent
6d415b0ace
commit
5f2ba14281
@ -763,12 +763,12 @@ proc stop*(self: CodexNodeRef) {.async.} =
|
|||||||
if hostContracts =? self.contracts.host:
|
if hostContracts =? self.contracts.host:
|
||||||
await hostContracts.stop()
|
await hostContracts.stop()
|
||||||
|
|
||||||
if not self.clock.isNil:
|
|
||||||
await self.clock.stop()
|
|
||||||
|
|
||||||
if validatorContracts =? self.contracts.validator:
|
if validatorContracts =? self.contracts.validator:
|
||||||
await validatorContracts.stop()
|
await validatorContracts.stop()
|
||||||
|
|
||||||
|
if not self.clock.isNil:
|
||||||
|
await self.clock.stop()
|
||||||
|
|
||||||
if not self.networkStore.isNil:
|
if not self.networkStore.isNil:
|
||||||
await self.networkStore.close
|
await self.networkStore.close
|
||||||
|
|
||||||
|
@ -30,13 +30,13 @@ proc new*(T: type Timer, timerName = "Unnamed Timer"): Timer =
|
|||||||
## Create a new Timer intance with the given name
|
## Create a new Timer intance with the given name
|
||||||
Timer(name: timerName)
|
Timer(name: timerName)
|
||||||
|
|
||||||
proc timerLoop(timer: Timer) {.async.} =
|
proc timerLoop(timer: Timer) {.async: (raises: []).} =
|
||||||
try:
|
try:
|
||||||
while true:
|
while true:
|
||||||
await timer.callback()
|
await timer.callback()
|
||||||
await sleepAsync(timer.interval)
|
await sleepAsync(timer.interval)
|
||||||
except CancelledError:
|
except CancelledError:
|
||||||
raise
|
discard # do not propagate as timerLoop is asyncSpawned
|
||||||
except CatchableError as exc:
|
except CatchableError as exc:
|
||||||
error "Timer caught unhandled exception: ", name=timer.name, msg=exc.msg
|
error "Timer caught unhandled exception: ", name=timer.name, msg=exc.msg
|
||||||
|
|
||||||
@ -47,9 +47,10 @@ method start*(timer: Timer, callback: TimerCallback, interval: Duration) {.base.
|
|||||||
timer.callback = callback
|
timer.callback = callback
|
||||||
timer.interval = interval
|
timer.interval = interval
|
||||||
timer.loopFuture = timerLoop(timer)
|
timer.loopFuture = timerLoop(timer)
|
||||||
|
asyncSpawn timer.loopFuture
|
||||||
|
|
||||||
method stop*(timer: Timer) {.async, base.} =
|
method stop*(timer: Timer) {.async, base.} =
|
||||||
if timer.loopFuture != nil:
|
if timer.loopFuture != nil and not timer.loopFuture.finished:
|
||||||
trace "Timer stopping: ", name=timer.name
|
trace "Timer stopping: ", name=timer.name
|
||||||
await timer.loopFuture.cancelAndWait()
|
await timer.loopFuture.cancelAndWait()
|
||||||
timer.loopFuture = nil
|
timer.loopFuture = nil
|
||||||
|
Loading…
x
Reference in New Issue
Block a user