From febb6dc2ffe023de2ddb8b1b4a60b5bf105b947d Mon Sep 17 00:00:00 2001 From: atauov Date: Tue, 31 Mar 2026 18:00:57 +0500 Subject: [PATCH] - ctx object get/set by request --- statefun/function_type.go | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/statefun/function_type.go b/statefun/function_type.go index 5a655833..51b2cc15 100644 --- a/statefun/function_type.go +++ b/statefun/function_type.go @@ -203,8 +203,8 @@ func (ft *FunctionType) workerTaskExecutor(id string, msg FunctionTypeMsg) { GetFunctionContext: func() *easyjson.JSON { return ft.getContext(ft.name + "." + id) }, SetFunctionContext: func(context *easyjson.JSON) { ft.setContext(ft.name+"."+id, context) }, SetContextExpirationAfter: func(after time.Duration) { ft.setContextExpirationAfter(ft.name+"."+id, after) }, - GetObjectContext: func() *easyjson.JSON { return ft.getContext(id) }, - SetObjectContext: func(context *easyjson.JSON) { ft.setContext(id, context) }, + GetObjectContext: func() *easyjson.JSON { return ft.getObjectContext(id) }, + SetObjectContext: func(context *easyjson.JSON) { ft.setObjectContext(id, context) }, GetObjectImplTypes: func() (types []string, err error) { return ft.getObjectImplTypes(id) }, Domain: ft.runtime.Domain, Self: sfPlugins.StatefunAddress{Typename: ft.name, ID: id}, @@ -457,6 +457,30 @@ func (ft *FunctionType) setContext(keyValueID string, context *easyjson.JSON) { } } +func (ft *FunctionType) getObjectContext(keyValueID string) *easyjson.JSON { + response, err := ft.runtime.Request(sfPlugins.AutoRequestSelect, "functions.graph.api.vertex.read", keyValueID, nil, nil) + if err == nil { + body := response.GetByPath("data.body") // response is OpMsg: {"status":..., "data":{"body":{...}}} + return &body + } + j := easyjson.NewJSONObject() + return &j +} + +func (ft *FunctionType) setObjectContext(keyValueID string, context *easyjson.JSON) { + if context == nil { + payload := easyjson.NewJSONObject() + payload.SetByPath("op_time", easyjson.NewJSON(system.GetCurrentTimeNs())) + system.MsgOnErrorReturn(ft.runtime.Request(sfPlugins.AutoRequestSelect, "functions.graph.api.vertex.delete", keyValueID, &payload, nil)) + } else { + payload := easyjson.NewJSONObject() + payload.SetByPath("op_time", easyjson.NewJSON(system.GetCurrentTimeNs())) + payload.SetByPath("replace", easyjson.NewJSON(true)) + payload.SetByPath("body", *context) + system.MsgOnErrorReturn(ft.runtime.Request(sfPlugins.AutoRequestSelect, "functions.graph.api.vertex.update", keyValueID, &payload, nil)) + } +} + // Negative duration removes expiration func (ft *FunctionType) setContextExpirationAfter(keyValueID string, after time.Duration) { if j, err := ft.runtime.Domain.cache.GetValueJSON(keyValueID); err == nil {