feat(process): add Core task for run execution
This commit is contained in:
parent
206b9a1f52
commit
0546b42ce3
3 changed files with 50 additions and 7 deletions
|
|
@ -4,6 +4,15 @@ import "time"
|
|||
|
||||
// --- ACTION messages (broadcast via Core.ACTION) ---
|
||||
|
||||
// TaskProcessRun requests synchronous command execution through Core.PERFORM.
|
||||
// The handler returns the combined command output on success.
|
||||
type TaskProcessRun struct {
|
||||
Command string
|
||||
Args []string
|
||||
Dir string
|
||||
Env []string
|
||||
}
|
||||
|
||||
// ActionProcessStarted is broadcast when a process begins execution.
|
||||
type ActionProcessStarted struct {
|
||||
ID string
|
||||
|
|
|
|||
33
service.go
33
service.go
|
|
@ -30,10 +30,11 @@ var (
|
|||
type Service struct {
|
||||
*core.ServiceRuntime[Options]
|
||||
|
||||
processes map[string]*Process
|
||||
mu sync.RWMutex
|
||||
bufSize int
|
||||
idCounter atomic.Uint64
|
||||
processes map[string]*Process
|
||||
mu sync.RWMutex
|
||||
bufSize int
|
||||
idCounter atomic.Uint64
|
||||
registrations sync.Once
|
||||
}
|
||||
|
||||
// Options configures the process service.
|
||||
|
|
@ -64,6 +65,11 @@ func NewService(opts Options) func(*core.Core) (any, error) {
|
|||
|
||||
// OnStartup implements core.Startable.
|
||||
func (s *Service) OnStartup(ctx context.Context) error {
|
||||
s.registrations.Do(func() {
|
||||
if s.Core() != nil {
|
||||
s.Core().RegisterTask(s.handleTask)
|
||||
}
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
@ -402,3 +408,22 @@ func (s *Service) RunWithOptions(ctx context.Context, opts RunOptions) (string,
|
|||
}
|
||||
return output, nil
|
||||
}
|
||||
|
||||
// handleTask dispatches Core.PERFORM messages for the process service.
|
||||
func (s *Service) handleTask(c *core.Core, task core.Task) core.Result {
|
||||
switch m := task.(type) {
|
||||
case TaskProcessRun:
|
||||
output, err := s.RunWithOptions(c.Context(), RunOptions{
|
||||
Command: m.Command,
|
||||
Args: m.Args,
|
||||
Dir: m.Dir,
|
||||
Env: m.Env,
|
||||
})
|
||||
if err != nil {
|
||||
return core.Result{Value: err, OK: false}
|
||||
}
|
||||
return core.Result{Value: output, OK: true}
|
||||
default:
|
||||
return core.Result{}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -391,10 +391,19 @@ func TestService_OnShutdown(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestService_OnStartup(t *testing.T) {
|
||||
t.Run("returns nil", func(t *testing.T) {
|
||||
svc, _ := newTestService(t)
|
||||
t.Run("registers process.run task", func(t *testing.T) {
|
||||
svc, c := newTestService(t)
|
||||
|
||||
err := svc.OnStartup(context.Background())
|
||||
assert.NoError(t, err)
|
||||
require.NoError(t, err)
|
||||
|
||||
result := c.PERFORM(TaskProcessRun{
|
||||
Command: "echo",
|
||||
Args: []string{"action-run"},
|
||||
})
|
||||
|
||||
require.True(t, result.OK)
|
||||
assert.Contains(t, result.Value.(string), "action-run")
|
||||
})
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue