2022-08-04 08:04:30 +00:00
|
|
|
# Nimbus
|
2022-05-09 14:04:48 +00:00
|
|
|
# Copyright (c) 2021 Status Research & Development GmbH
|
|
|
|
# Licensed under either of
|
2022-05-13 16:30:10 +00:00
|
|
|
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0)
|
|
|
|
# * MIT license ([LICENSE-MIT](LICENSE-MIT) or
|
|
|
|
# http://opensource.org/licenses/MIT)
|
|
|
|
# at your option. This file may not be copied, modified, or distributed
|
|
|
|
# except according to those terms.
|
2022-05-09 14:04:48 +00:00
|
|
|
|
2023-04-24 20:24:07 +00:00
|
|
|
## Sync mode pass multiplexer
|
|
|
|
## ==========================
|
|
|
|
##
|
|
|
|
## Pass state diagram:
|
|
|
|
## ::
|
|
|
|
## <init> -> <snap-sync> -> <full-sync> ---+
|
|
|
|
## ^ |
|
|
|
|
## | |
|
|
|
|
## +----------+
|
|
|
|
##
|
2023-04-06 19:42:07 +00:00
|
|
|
{.push raises: [].}
|
|
|
|
|
2022-05-09 14:04:48 +00:00
|
|
|
import
|
2022-05-17 11:09:49 +00:00
|
|
|
chronicles,
|
|
|
|
chronos,
|
2023-04-24 20:24:07 +00:00
|
|
|
./range_desc,
|
|
|
|
./worker/pass,
|
|
|
|
./worker_desc
|
2022-09-16 07:24:12 +00:00
|
|
|
|
2022-05-23 16:53:19 +00:00
|
|
|
logScope:
|
2023-04-06 19:42:07 +00:00
|
|
|
topics = "snap-worker"
|
2022-10-08 17:20:50 +00:00
|
|
|
|
2022-09-30 08:22:14 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
2023-01-17 09:28:14 +00:00
|
|
|
# Private helpers
|
2022-09-30 08:22:14 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2023-04-06 19:42:07 +00:00
|
|
|
template ignoreException(info: static[string]; code: untyped) =
|
2023-01-17 09:28:14 +00:00
|
|
|
try:
|
|
|
|
code
|
|
|
|
except CatchableError as e:
|
2023-04-06 19:42:07 +00:00
|
|
|
error "Exception at " & info & ":", name=($e.name), msg=(e.msg)
|
2022-09-30 08:22:14 +00:00
|
|
|
|
2022-06-06 13:42:08 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public start/stop and admin functions
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2023-04-24 20:24:07 +00:00
|
|
|
proc setup*(ctx: SnapCtxRef): bool =
|
2022-06-06 13:42:08 +00:00
|
|
|
## Global set up
|
2023-04-24 20:24:07 +00:00
|
|
|
ctx.passInitSetup()
|
2023-04-14 22:28:57 +00:00
|
|
|
ignoreException("setup"):
|
2023-04-24 20:24:07 +00:00
|
|
|
ctx.passActor.setup(ctx)
|
2022-11-25 14:56:42 +00:00
|
|
|
true
|
2022-08-04 08:04:30 +00:00
|
|
|
|
|
|
|
proc release*(ctx: SnapCtxRef) =
|
2022-06-06 13:42:08 +00:00
|
|
|
## Global clean up
|
2023-04-14 22:28:57 +00:00
|
|
|
ignoreException("release"):
|
2023-04-24 20:24:07 +00:00
|
|
|
ctx.passActor.release(ctx)
|
|
|
|
ctx.passInitRelease()
|
2022-06-16 08:58:50 +00:00
|
|
|
|
2022-08-04 08:04:30 +00:00
|
|
|
proc start*(buddy: SnapBuddyRef): bool =
|
|
|
|
## Initialise worker peer
|
2023-04-14 22:28:57 +00:00
|
|
|
ignoreException("start"):
|
2023-04-24 20:24:07 +00:00
|
|
|
result = buddy.ctx.passActor.start(buddy)
|
2022-06-06 13:42:08 +00:00
|
|
|
|
2022-08-04 08:04:30 +00:00
|
|
|
proc stop*(buddy: SnapBuddyRef) =
|
2022-06-06 13:42:08 +00:00
|
|
|
## Clean up this peer
|
2023-04-14 22:28:57 +00:00
|
|
|
ignoreException("stop"):
|
2023-04-24 20:24:07 +00:00
|
|
|
buddy.ctx.passActor.stop(buddy)
|
2022-06-06 13:42:08 +00:00
|
|
|
|
2022-05-17 11:09:49 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
2023-04-06 19:42:07 +00:00
|
|
|
# Public functions, sync handler multiplexers
|
2022-05-17 11:09:49 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2022-11-14 14:13:00 +00:00
|
|
|
proc runDaemon*(ctx: SnapCtxRef) {.async.} =
|
2023-04-06 19:42:07 +00:00
|
|
|
## Sync processsing multiplexer
|
|
|
|
ignoreException("runDaemon"):
|
2023-04-24 20:24:07 +00:00
|
|
|
await ctx.passActor.daemon(ctx)
|
2022-11-14 14:13:00 +00:00
|
|
|
|
|
|
|
proc runSingle*(buddy: SnapBuddyRef) {.async.} =
|
2023-04-06 19:42:07 +00:00
|
|
|
## Sync processsing multiplexer
|
|
|
|
ignoreException("runSingle"):
|
2023-04-24 20:24:07 +00:00
|
|
|
await buddy.ctx.passActor.single(buddy)
|
2022-05-09 14:04:48 +00:00
|
|
|
|
2023-04-14 22:28:57 +00:00
|
|
|
proc runPool*(buddy: SnapBuddyRef, last: bool; laps: int): bool =
|
2023-04-06 19:42:07 +00:00
|
|
|
## Sync processsing multiplexer
|
|
|
|
ignoreException("runPool"):
|
2023-04-24 20:24:07 +00:00
|
|
|
result = buddy.ctx.passActor.pool(buddy,last,laps)
|
2022-05-17 11:09:49 +00:00
|
|
|
|
2022-08-04 08:04:30 +00:00
|
|
|
proc runMulti*(buddy: SnapBuddyRef) {.async.} =
|
2023-04-06 19:42:07 +00:00
|
|
|
## Sync processsing multiplexer
|
|
|
|
ignoreException("runMulti"):
|
2023-04-24 20:24:07 +00:00
|
|
|
await buddy.ctx.passActor.multi(buddy)
|
2022-11-08 18:56:04 +00:00
|
|
|
|
2022-05-17 11:09:49 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|