forked from aws/aws-lambda-runtime-interface-emulator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathruntime_command.go
More file actions
57 lines (44 loc) · 1.3 KB
/
runtime_command.go
File metadata and controls
57 lines (44 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package runtimecmd
import (
"context"
"fmt"
"io"
"os"
"os/exec"
"path"
"syscall"
)
// CustomRuntimeCmd wraps exec.Cmd
type CustomRuntimeCmd struct {
*exec.Cmd
}
// NewCustomRuntimeCmd returns a new CustomRuntimeCmd
func NewCustomRuntimeCmd(ctx context.Context, bootstrapCmd []string, dir string, env []string, runtimeLogWriter io.Writer, extraFiles []*os.File) *CustomRuntimeCmd {
cmd := exec.CommandContext(ctx, bootstrapCmd[0], bootstrapCmd[1:]...)
cmd.Dir = dir
cmd.Stdout = runtimeLogWriter
cmd.Stderr = runtimeLogWriter
cmd.Env = env
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
if len(extraFiles) > 0 {
cmd.ExtraFiles = extraFiles
}
return &CustomRuntimeCmd{cmd}
}
// Name returnes runtime executable name
func (cmd *CustomRuntimeCmd) Name() string {
return path.Base(cmd.Path)
}
// Pid returns the pid of a started runtime process
func (cmd *CustomRuntimeCmd) Pid() int {
return cmd.Process.Pid
}
// Wait waits for the started customer runtime process to exit
func (cmd *CustomRuntimeCmd) Wait() error {
if err := cmd.Cmd.Wait(); err != nil {
return fmt.Errorf("Runtime exited with error: %v", err)
}
return fmt.Errorf("Runtime exited without providing a reason")
}