feat(process): add Core stdin task
This commit is contained in:
parent
ceea10fc7a
commit
227739638b
3 changed files with 50 additions and 0 deletions
12
actions.go
12
actions.go
|
|
@ -82,6 +82,18 @@ type TaskProcessOutput struct {
|
|||
ID string
|
||||
}
|
||||
|
||||
// TaskProcessInput writes data to the stdin of a managed process through Core.PERFORM.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// c.PERFORM(process.TaskProcessInput{ID: "proc-1", Input: "hello\n"})
|
||||
type TaskProcessInput struct {
|
||||
// ID identifies a managed process started by this service.
|
||||
ID string
|
||||
// Input is written verbatim to the process stdin pipe.
|
||||
Input string
|
||||
}
|
||||
|
||||
// TaskProcessList requests a snapshot of managed processes through Core.PERFORM.
|
||||
// If RunningOnly is true, only active processes are returned.
|
||||
//
|
||||
|
|
|
|||
15
service.go
15
service.go
|
|
@ -624,6 +624,21 @@ func (s *Service) handleTask(c *core.Core, task core.Task) core.Result {
|
|||
}
|
||||
|
||||
return core.Result{Value: output, OK: true}
|
||||
case TaskProcessInput:
|
||||
if m.ID == "" {
|
||||
return core.Result{Value: coreerr.E("Service.handleTask", "task process input requires an id", nil), OK: false}
|
||||
}
|
||||
|
||||
proc, err := s.Get(m.ID)
|
||||
if err != nil {
|
||||
return core.Result{Value: err, OK: false}
|
||||
}
|
||||
|
||||
if err := proc.SendInput(m.Input); err != nil {
|
||||
return core.Result{Value: err, OK: false}
|
||||
}
|
||||
|
||||
return core.Result{OK: true}
|
||||
case TaskProcessList:
|
||||
procs := s.List()
|
||||
if m.RunningOnly {
|
||||
|
|
|
|||
|
|
@ -753,6 +753,29 @@ func TestService_OnStartup(t *testing.T) {
|
|||
require.True(t, ok)
|
||||
assert.Contains(t, output, "snapshot-output")
|
||||
})
|
||||
|
||||
t.Run("registers process.input task", func(t *testing.T) {
|
||||
svc, c := newTestService(t)
|
||||
|
||||
err := svc.OnStartup(context.Background())
|
||||
require.NoError(t, err)
|
||||
|
||||
proc, err := svc.Start(context.Background(), "cat")
|
||||
require.NoError(t, err)
|
||||
|
||||
result := c.PERFORM(TaskProcessInput{
|
||||
ID: proc.ID,
|
||||
Input: "typed-through-core\n",
|
||||
})
|
||||
require.True(t, result.OK)
|
||||
|
||||
err = proc.CloseStdin()
|
||||
require.NoError(t, err)
|
||||
|
||||
<-proc.Done()
|
||||
|
||||
assert.Contains(t, proc.Output(), "typed-through-core")
|
||||
})
|
||||
}
|
||||
|
||||
func TestService_RunWithOptions(t *testing.T) {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue