mirror of
https://github.com/logos-storage/logos-storage-nim.git
synced 2026-01-05 23:13:09 +00:00
Split close and destroy in separate functions
This commit is contained in:
parent
d3d2347e8f
commit
b78d8d76fb
@ -31,7 +31,7 @@ type NodeLifecycleMsgType* = enum
|
|||||||
CREATE_NODE
|
CREATE_NODE
|
||||||
START_NODE
|
START_NODE
|
||||||
STOP_NODE
|
STOP_NODE
|
||||||
DESTROY_NODE
|
CLOSE_NODE
|
||||||
|
|
||||||
proc readValue*[T: InputFile | InputDir | OutPath | OutDir | OutFile](
|
proc readValue*[T: InputFile | InputDir | OutPath | OutDir | OutFile](
|
||||||
r: var JsonReader, val: var T
|
r: var JsonReader, val: var T
|
||||||
@ -82,18 +82,13 @@ proc readValue*(r: var JsonReader, val: var EthAddress) =
|
|||||||
type NodeLifecycleRequest* = object
|
type NodeLifecycleRequest* = object
|
||||||
operation: NodeLifecycleMsgType
|
operation: NodeLifecycleMsgType
|
||||||
configJson: cstring
|
configJson: cstring
|
||||||
onDestroy: proc() {.gcsafe.}
|
|
||||||
|
|
||||||
proc createShared*(
|
proc createShared*(
|
||||||
T: type NodeLifecycleRequest,
|
T: type NodeLifecycleRequest, op: NodeLifecycleMsgType, configJson: cstring = ""
|
||||||
op: NodeLifecycleMsgType,
|
|
||||||
configJson: cstring = "",
|
|
||||||
onDestroy: proc() {.gcsafe.} = nil,
|
|
||||||
): ptr type T =
|
): ptr type T =
|
||||||
var ret = createShared(T)
|
var ret = createShared(T)
|
||||||
ret[].operation = op
|
ret[].operation = op
|
||||||
ret[].configJson = configJson.alloc()
|
ret[].configJson = configJson.alloc()
|
||||||
ret[].onDestroy = onDestroy
|
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
proc destroyShared(self: ptr NodeLifecycleRequest) =
|
proc destroyShared(self: ptr NodeLifecycleRequest) =
|
||||||
@ -184,10 +179,9 @@ proc process*(
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
error "Failed to STOP_NODE.", error = e.msg
|
error "Failed to STOP_NODE.", error = e.msg
|
||||||
return err(e.msg)
|
return err(e.msg)
|
||||||
of DESTROY_NODE:
|
of CLOSE_NODE:
|
||||||
try:
|
try:
|
||||||
await codex[].close()
|
await codex[].close()
|
||||||
self.onDestroy()
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
error "Failed to STOP_NODE.", error = e.msg
|
error "Failed to STOP_NODE.", error = e.msg
|
||||||
return err(e.msg)
|
return err(e.msg)
|
||||||
|
|||||||
@ -180,6 +180,10 @@ int codex_stop(void* ctx,
|
|||||||
CodexCallback callback,
|
CodexCallback callback,
|
||||||
void* userData);
|
void* userData);
|
||||||
|
|
||||||
|
int codex_close(void* ctx,
|
||||||
|
CodexCallback callback,
|
||||||
|
void* userData);
|
||||||
|
|
||||||
// Destroys an instance of a codex node created with codex_new
|
// Destroys an instance of a codex node created with codex_new
|
||||||
int codex_destroy(void* ctx,
|
int codex_destroy(void* ctx,
|
||||||
CodexCallback callback,
|
CodexCallback callback,
|
||||||
|
|||||||
@ -246,30 +246,28 @@ proc codex_peer_debug(
|
|||||||
|
|
||||||
return callback.okOrError(res, userData)
|
return callback.okOrError(res, userData)
|
||||||
|
|
||||||
proc codex_destroy(
|
proc codex_close(
|
||||||
ctx: ptr CodexContext, callback: CodexCallback, userData: pointer
|
ctx: ptr CodexContext, callback: CodexCallback, userData: pointer
|
||||||
): cint {.dynlib, exportc.} =
|
): cint {.dynlib, exportc.} =
|
||||||
initializeLibrary()
|
initializeLibrary()
|
||||||
checkLibcodexParams(ctx, callback, userData)
|
checkLibcodexParams(ctx, callback, userData)
|
||||||
|
|
||||||
let destroySignal = ThreadSignalPtr.new().valueOr:
|
let reqContent = NodeLifecycleRequest.createShared(NodeLifecycleMsgType.CLOSE_NODE)
|
||||||
return callback.error("failed to create destroy signal", userData)
|
|
||||||
|
|
||||||
proc onDestroy() {.gcsafe.} =
|
|
||||||
discard destroySignal.fireSync()
|
|
||||||
|
|
||||||
let reqContent = NodeLifecycleRequest.createShared(
|
|
||||||
NodeLifecycleMsgType.DESTROY_NODE, onDestroy = onDestroy
|
|
||||||
)
|
|
||||||
var res = codex_context.sendRequestToCodexThread(
|
var res = codex_context.sendRequestToCodexThread(
|
||||||
ctx, RequestType.LIFECYCLE, reqContent, callback, userData
|
ctx, RequestType.LIFECYCLE, reqContent, callback, userData
|
||||||
)
|
)
|
||||||
if res.isErr:
|
if res.isErr:
|
||||||
return callback.error(res.error, userData)
|
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:
|
if res.isErr:
|
||||||
return callback.error(res.error, userData)
|
return callback.error(res.error, userData)
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user