Split close and destroy in separate functions

This commit is contained in:
Arnaud 2025-10-09 13:10:00 +02:00
parent d3d2347e8f
commit b78d8d76fb
No known key found for this signature in database
GPG Key ID: B8FBC178F10CA7AE
3 changed files with 17 additions and 21 deletions

View File

@ -31,7 +31,7 @@ type NodeLifecycleMsgType* = enum
CREATE_NODE
START_NODE
STOP_NODE
DESTROY_NODE
CLOSE_NODE
proc readValue*[T: InputFile | InputDir | OutPath | OutDir | OutFile](
r: var JsonReader, val: var T
@ -82,18 +82,13 @@ proc readValue*(r: var JsonReader, val: var EthAddress) =
type NodeLifecycleRequest* = object
operation: NodeLifecycleMsgType
configJson: cstring
onDestroy: proc() {.gcsafe.}
proc createShared*(
T: type NodeLifecycleRequest,
op: NodeLifecycleMsgType,
configJson: cstring = "",
onDestroy: proc() {.gcsafe.} = nil,
T: type NodeLifecycleRequest, op: NodeLifecycleMsgType, configJson: cstring = ""
): ptr type T =
var ret = createShared(T)
ret[].operation = op
ret[].configJson = configJson.alloc()
ret[].onDestroy = onDestroy
return ret
proc destroyShared(self: ptr NodeLifecycleRequest) =
@ -184,10 +179,9 @@ proc process*(
except Exception as e:
error "Failed to STOP_NODE.", error = e.msg
return err(e.msg)
of DESTROY_NODE:
of CLOSE_NODE:
try:
await codex[].close()
self.onDestroy()
except Exception as e:
error "Failed to STOP_NODE.", error = e.msg
return err(e.msg)

View File

@ -180,6 +180,10 @@ int codex_stop(void* ctx,
CodexCallback callback,
void* userData);
int codex_close(void* ctx,
CodexCallback callback,
void* userData);
// Destroys an instance of a codex node created with codex_new
int codex_destroy(void* ctx,
CodexCallback callback,

View File

@ -246,30 +246,28 @@ proc codex_peer_debug(
return callback.okOrError(res, userData)
proc codex_destroy(
proc codex_close(
ctx: ptr CodexContext, callback: CodexCallback, userData: pointer
): cint {.dynlib, exportc.} =
initializeLibrary()
checkLibcodexParams(ctx, callback, userData)
let destroySignal = ThreadSignalPtr.new().valueOr:
return callback.error("failed to create destroy signal", userData)
proc onDestroy() {.gcsafe.} =
discard destroySignal.fireSync()
let reqContent = NodeLifecycleRequest.createShared(
NodeLifecycleMsgType.DESTROY_NODE, onDestroy = onDestroy
)
let reqContent = NodeLifecycleRequest.createShared(NodeLifecycleMsgType.CLOSE_NODE)
var res = codex_context.sendRequestToCodexThread(
ctx, RequestType.LIFECYCLE, reqContent, callback, userData
)
if res.isErr:
return callback.error(res.error, userData)
discard destroySignal.waitSync()
return callback.okOrError(res, userData)
res = codex_context.destroyCodexContext(ctx)
proc codex_destroy(
ctx: ptr CodexContext, callback: CodexCallback, userData: pointer
): cint {.dynlib, exportc.} =
initializeLibrary()
checkLibcodexParams(ctx, callback, userData)
let res = codex_context.destroyCodexContext(ctx)
if res.isErr:
return callback.error(res.error, userData)