Skip to content

Commit dc2b9a8

Browse files
committed

File tree

4 files changed

+58
-27
lines changed

4 files changed

+58
-27
lines changed

apicontext/context.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package apicontext
2+
3+
import (
4+
"context"
5+
"github.com/cloudimpl/next-coder-sdk/polycode"
6+
)
7+
8+
func FromContext(ctx context.Context) (polycode.ApiContext, error) {
9+
value := ctx.Value("polycode.context")
10+
if value == nil {
11+
return polycode.ApiContext{}, polycode.ErrContextNotFound
12+
}
13+
14+
return value.(polycode.ApiContext), nil
15+
}

polycode/context.go

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package polycode
22

33
import (
44
"context"
5-
"os"
65
"time"
76
)
87

@@ -78,6 +77,33 @@ func (wc WorkflowContext) Service(serviceId string) (RemoteService, error) {
7877
return RemoteService{ctx: wc.ctx, sessionId: wc.sessionId, serviceId: serviceId, serviceClient: wc.serviceClient}, nil
7978
}
8079

81-
func (wc WorkflowContext) LocalService() (RemoteService, error) {
82-
return RemoteService{ctx: wc.ctx, sessionId: wc.sessionId, serviceId: os.Getenv("polycode_SERVICE_ID"), serviceClient: wc.serviceClient}, nil
80+
type ApiContext struct {
81+
ctx context.Context
82+
sessionId string
83+
serviceClient *ServiceClient
84+
config AppConfig
85+
}
86+
87+
func (wc ApiContext) AppConfig() AppConfig {
88+
return wc.config
89+
}
90+
91+
func (wc ApiContext) Deadline() (deadline time.Time, ok bool) {
92+
return wc.ctx.Deadline()
93+
}
94+
95+
func (wc ApiContext) Done() <-chan struct{} {
96+
return wc.ctx.Done()
97+
}
98+
99+
func (wc ApiContext) Err() error {
100+
return wc.ctx.Err()
101+
}
102+
103+
func (wc ApiContext) Value(key any) any {
104+
return wc.ctx.Value(key)
105+
}
106+
107+
func (wc ApiContext) Service(serviceId string) (RemoteService, error) {
108+
return RemoteService{ctx: wc.ctx, sessionId: wc.sessionId, serviceId: serviceId, serviceClient: wc.serviceClient}, nil
83109
}

polycode/task.go

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,13 @@ type RouteData struct {
3131
}
3232

3333
type TaskStartEvent struct {
34-
Id string `json:"id"`
35-
SessionId string `json:"sessionId"`
36-
EntryPoint string `json:"entryPoint"`
37-
Input TaskInput `json:"input"`
34+
Id string `json:"id"`
35+
SessionId string `json:"sessionId"`
36+
TenantId string `json:"tenantId"`
37+
ServiceName string `json:"serviceName"`
38+
PartitionKey string `json:"partitionKey"`
39+
EntryPoint string `json:"entryPoint"`
40+
Input TaskInput `json:"input"`
3841
}
3942

4043
type TaskCompleteEvent struct {
@@ -239,7 +242,8 @@ func runTask(ctx context.Context, event any) (evt *TaskCompleteEvent) {
239242
config: appConfig,
240243
}
241244

242-
println(fmt.Sprintf("client: exec workflow %s with session id %s", it.EntryPoint, it.SessionId))
245+
println(fmt.Sprintf("client: service %s exec workflow %s with session id %s", it.ServiceName,
246+
it.EntryPoint, it.SessionId))
243247
ret, err = service.ExecuteWorkflow(workflowCtx, it.EntryPoint, inputObj)
244248
} else {
245249
srvCtx := ServiceContext{
@@ -249,7 +253,8 @@ func runTask(ctx context.Context, event any) (evt *TaskCompleteEvent) {
249253
config: appConfig,
250254
}
251255

252-
println(fmt.Sprintf("client: exec service %s with session id %s", it.EntryPoint, it.SessionId))
256+
println(fmt.Sprintf("client: service %s exec handler %s with session id %s", it.ServiceName,
257+
it.EntryPoint, it.SessionId))
253258
ret, err = service.ExecuteService(srvCtx, it.EntryPoint, inputObj)
254259
}
255260

@@ -276,15 +281,15 @@ func runTask(ctx context.Context, event any) (evt *TaskCompleteEvent) {
276281
return errorToTaskComplete(ErrBadRequest)
277282
}
278283

279-
workflowCtx := WorkflowContext{
284+
apiCtx := ApiContext{
280285
ctx: ctx,
281286
sessionId: it.SessionId,
282287
serviceClient: serviceClient,
283288
config: appConfig,
284289
}
285-
wkfCtx := context.WithValue(ctx, "polycode.context", workflowCtx)
290+
newCtx := context.WithValue(ctx, "polycode.context", apiCtx)
286291

287-
req, err := ConvertToHttpRequest(wkfCtx, it.Request)
292+
req, err := ConvertToHttpRequest(newCtx, it.Request)
288293
if err != nil {
289294
println("client: failed to convert api request")
290295
return errorToTaskComplete(err)

workflowcontext/context.go

Lines changed: 0 additions & 15 deletions
This file was deleted.

0 commit comments

Comments
 (0)