Skip to content

Commit

Permalink
refactor: more unified style
Browse files Browse the repository at this point in the history
  • Loading branch information
siyul-park committed Oct 1, 2024
1 parent 6a7db29 commit b6cd9d9
Show file tree
Hide file tree
Showing 52 changed files with 390 additions and 414 deletions.
8 changes: 4 additions & 4 deletions cmd/pkg/cli/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,15 @@ func runApplyCommand(config ApplyConfig) func(cmd *cobra.Command, args []string)
}
}

exists, err := config.SpecStore.Load(ctx, specs...)
ok, err := config.SpecStore.Load(ctx, specs...)
if err != nil {
return err
}

var inserts []spec.Spec
var updates []spec.Spec
for _, sp := range specs {
if match := resourcebase.Match(sp, exists...); len(match) > 0 {
if match := resourcebase.Match(sp, ok...); len(match) > 0 {
sp.SetID(match[0].GetID())
updates = append(updates, sp)
} else {
Expand Down Expand Up @@ -103,15 +103,15 @@ func runApplyCommand(config ApplyConfig) func(cmd *cobra.Command, args []string)
}
}

exists, err := config.SecretStore.Load(ctx, secrets...)
ok, err := config.SecretStore.Load(ctx, secrets...)
if err != nil {
return err
}

var inserts []*secret.Secret
var updates []*secret.Secret
for _, sec := range secrets {
if match := resourcebase.Match(sec, exists...); len(match) > 0 {
if match := resourcebase.Match(sec, ok...); len(match) > 0 {
sec.SetID(match[0].GetID())
updates = append(updates, sec)
} else {
Expand Down
6 changes: 3 additions & 3 deletions cmd/pkg/cli/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ func (m *debugModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var proc *process.Process
if len(args) > 1 {
id, _ := uuid.FromString(args[1])
proc, _ = m.agent.Process(id)
proc = m.agent.Process(id)
} else {
proc = m.debugger.Process()
}
Expand All @@ -277,14 +277,14 @@ func (m *debugModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var proc *process.Process
if len(args) > 1 {
id, _ := uuid.FromString(args[1])
proc, _ = m.agent.Process(id)
proc = m.agent.Process(id)
} else {
proc = m.debugger.Process()
}

var frames []*agent.Frame
if proc != nil {
frames, _ = m.agent.Frames(proc.ID())
frames = m.agent.Frames(proc.ID())
}

if frames == nil {
Expand Down
41 changes: 5 additions & 36 deletions docs/user_extensions.md
Original file line number Diff line number Diff line change
Expand Up @@ -334,12 +334,10 @@ This code creates a new runtime environment using the provided schema, hook, spe

To integrate the runtime environment with existing services and build an executable, you need to set up the environment to run continuously or in a simpler execution mode.

### Continuous Execution

Running the runtime environment continuously allows it to handle external requests promptly. This approach is suitable for scenarios where ongoing workflow execution is needed.

```go
func main() {
ctx := context.TODO()

specStore := spec.NewStore()
secretStore := secret.NewStore()

Expand Down Expand Up @@ -372,39 +370,10 @@ func main() {
_ = r.Close()
}()

r.Listen(context.TODO())
r.Watch(ctx)
r.Load(ctx)
r.Reconcile(ctx)
}
```

This code keeps the runtime environment running and responsive to external signals. It uses `os.Signal` to listen for termination signals and safely shuts down the environment.

### Simple Execution

In some cases, you may prefer a simpler execution mode where the runtime environment runs only when needed and then exits. This approach is useful for scenarios where you don't need continuous operation.

```go
r := runtime.New(runtime.Config{
Namespace: "default",
Schema: scheme,
Hook: hook,
SpecStore: specStore,
SecretStore: secretStore,
})
defer r.Close()

r.Load(ctx) // Load all resources

symbols, _ := r.Load(ctx, &spec.Meta{
Name: "main",
})

sb := symbols[0]

in := port.NewOut()
defer in.Close()

in.Link(sb.In(node.PortIn))

payload := types.NewString(faker.Word())
payload, err := port.Call(in, payload)
```
41 changes: 5 additions & 36 deletions docs/user_extensions_kr.md
Original file line number Diff line number Diff line change
Expand Up @@ -334,12 +334,10 @@ defer r.Close()

이제 만든 런타임 환경을 기존 서비스에 통합하고, 다시 빌드하여 실행 파일을 생성해야 합니다.

### 지속 실행

런타임 환경을 지속적으로 유지하면 외부 요청에 즉시 대응할 수 있습니다. 각 런타임 환경은 독립적인 컨테이너에서 실행되며, 지속적인 워크플로우 실행이 필요한 시나리오에 적합합니다.

```go
func main() {
ctx := context.TODO()

specStore := spec.NewStore()
secretStore := secret.NewStore()

Expand Down Expand Up @@ -372,39 +370,10 @@ func main() {
_ = r.Close()
}()

r.Listen(context.TODO())
r.Watch(ctx)
r.Load(ctx)
r.Reconcile(ctx)
}
```

위 코드에서는 런타임 환경을 지속적으로 실행하여 외부 신호에 반응하도록 설정합니다. `os.Signal`을 통해 종료 신호를 수신하면 런타임 환경을 안전하게 종료합니다.

### 단순 실행

때로는 런타임 환경을 지속적으로 유지하는 대신, 필요할 때만 실행하고 종료하는 간단한 방식이 더 적합할 수 있습니다. 이럴 때는 단순 실행 방식을 사용할 수 있습니다.

```go
r := runtime.New(runtime.Config{
Namespace: "default",
Schema: scheme,
Hook: hook,
SpecStore: specStore,
SecretStore: secretStore,
})
defer r.Close()

r.Load(ctx) // 모든 리소스 로드

symbols, _ := r.Load(ctx, &spec.Meta{
Name: "main",
})

sb := symbols[0]

in := port.NewOut()
defer in.Close()

in.Link(sb.In(node.PortIn))

payload := types.NewString(faker.Word())
payload, err := port.Call(in, payload)
```
6 changes: 3 additions & 3 deletions driver/mongo/pkg/secret/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,18 +152,18 @@ func (s *Store) Swap(ctx context.Context, secrets ...*secret.Secret) (int, error
}
defer cursor.Close(ctx)

exists := make(map[uuid.UUID]*secret.Secret)
ok := make(map[uuid.UUID]*secret.Secret)
for cursor.Next(ctx) {
sec := &secret.Secret{}
if err := cursor.Decode(&sec); err != nil {
return 0, err
}
exists[sec.GetID()] = sec
ok[sec.GetID()] = sec
}

count := 0
for _, sec := range secrets {
exist, ok := exists[sec.GetID()]
exist, ok := ok[sec.GetID()]
if !ok {
continue
}
Expand Down
6 changes: 3 additions & 3 deletions driver/mongo/pkg/spec/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,18 +166,18 @@ func (s *Store) Swap(ctx context.Context, specs ...spec.Spec) (int, error) {
}
defer cursor.Close(ctx)

exists := make(map[uuid.UUID]spec.Spec)
ok := make(map[uuid.UUID]spec.Spec)
for cursor.Next(ctx) {
sp := &spec.Unstructured{}
if err := cursor.Decode(sp); err != nil {
return 0, err
}
exists[sp.GetID()] = sp
ok[sp.GetID()] = sp
}

count := 0
for _, sp := range specs {
exist, ok := exists[sp.GetID()]
exist, ok := ok[sp.GetID()]
if !ok {
continue
}
Expand Down
17 changes: 2 additions & 15 deletions ext/pkg/control/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ func (n *BlockNode) Load(hook symbol.LoadHook) error {
return err
}
}

return nil
}

Expand All @@ -123,7 +122,6 @@ func (n *BlockNode) Unload(hook symbol.UnloadHook) error {
return err
}
}

return nil
}

Expand Down Expand Up @@ -184,7 +182,6 @@ func (n *BlockNode) Close() error {
for _, outPort := range n._outPorts {
outPort.Close()
}

return nil
}

Expand All @@ -193,12 +190,7 @@ func (n *BlockNode) inbound(inPort *port.InPort, outPort *port.OutPort) port.Lis
reader := inPort.Open(proc)
writer := outPort.Open(proc)

for {
inPck, ok := <-reader.Read()
if !ok {
return
}

for inPck := range reader.Read() {
if writer.Write(inPck) == 0 {
reader.Receive(inPck)
}
Expand All @@ -211,12 +203,7 @@ func (n *BlockNode) outbound(inPort *port.InPort, outPort *port.OutPort) port.Li
reader := inPort.Open(proc)
writer := outPort.Open(proc)

for {
backPck, ok := <-writer.Receive()
if !ok {
return
}

for backPck := range writer.Receive() {
reader.Receive(backPck)
}
})
Expand Down
7 changes: 2 additions & 5 deletions ext/pkg/control/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,8 @@ func TestAddToScheme(t *testing.T) {

for _, tt := range tests {
t.Run(tt, func(t *testing.T) {
_, ok := s.KnownType(tt)
assert.True(t, ok)

_, ok = s.Codec(tt)
assert.True(t, ok)
assert.NotNil(t, s.KnownType(tt))
assert.NotNil(t, s.Codec(tt))
})
}
}
3 changes: 1 addition & 2 deletions ext/pkg/control/call.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ func (n *CallNode) In(name string) *port.InPort {
case node.PortIn:
return n.inPort
default:
return nil
}
return nil
}

// Out returns the output port with the specified name.
Expand Down Expand Up @@ -87,7 +87,6 @@ func (n *CallNode) Close() error {
}
n.errPort.Close()
n.tracer.Close()

return nil
}

Expand Down
5 changes: 2 additions & 3 deletions ext/pkg/control/fork.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ func (n *ForkNode) In(name string) *port.InPort {
case node.PortIn:
return n.inPort
default:
return nil
}
return nil
}

// Out returns the output port with the specified name.
Expand All @@ -66,16 +66,15 @@ func (n *ForkNode) Out(name string) *port.OutPort {
case node.PortErr:
return n.errPort
default:
return nil
}
return nil
}

// Close closes all ports associated with the node.
func (n *ForkNode) Close() error {
n.inPort.Close()
n.outPort.Close()
n.errPort.Close()

return nil
}

Expand Down
6 changes: 3 additions & 3 deletions ext/pkg/control/loop.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,9 @@ func (n *LoopNode) In(name string) *port.InPort {
switch name {
case node.PortIn:
return n.inPort
default:
return nil
}
return nil
}

// Out returns the output port with the specified name.
Expand All @@ -74,8 +75,8 @@ func (n *LoopNode) Out(name string) *port.OutPort {
return n.outPorts[index]
}
}
return nil
}
return nil
}

// Close closes all ports associated with the node.
Expand All @@ -86,7 +87,6 @@ func (n *LoopNode) Close() error {
}
n.errPort.Close()
n.tracer.Close()

return nil
}

Expand Down
3 changes: 1 addition & 2 deletions ext/pkg/control/nop.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ func (n *NOPNode) In(name string) *port.InPort {
case node.PortIn:
return n.inPort
default:
return nil
}
return nil
}

// Out returns nil as NOPNode does not have any output port.
Expand All @@ -59,7 +59,6 @@ func (n *NOPNode) Out(name string) *port.OutPort {
// Close closes all ports associated with the node.
func (n *NOPNode) Close() error {
n.inPort.Close()

return nil
}

Expand Down
Loading

0 comments on commit b6cd9d9

Please sign in to comment.