diff --git a/cmd/localstack/codearchive.go b/cmd/localstack/codearchive.go index ef781e0..ceb6dee 100644 --- a/cmd/localstack/codearchive.go +++ b/cmd/localstack/codearchive.go @@ -41,6 +41,7 @@ func DownloadCodeArchive(url string, targetPath string) error { // create tmp directory // empty string will make use of the default tmp directory tmpDir, err := os.MkdirTemp("", "localstack-code-archive") + defer os.RemoveAll(tmpDir) if err != nil { return err } diff --git a/cmd/localstack/custom_interop.go b/cmd/localstack/custom_interop.go index 5ed7895..96966b3 100644 --- a/cmd/localstack/custom_interop.go +++ b/cmd/localstack/custom_interop.go @@ -36,9 +36,9 @@ const ( Error LocalStackStatus = "error" ) -func (l *LocalStackAdapter) SendStatus(status LocalStackStatus) error { +func (l *LocalStackAdapter) SendStatus(status LocalStackStatus, payload []byte) error { status_url := fmt.Sprintf("%s/status/%s/%s", l.UpstreamEndpoint, l.RuntimeId, status) - _, err := http.Post(status_url, "text", bytes.NewReader([]byte{})) + _, err := http.Post(status_url, "application/json", bytes.NewReader(payload)) if err != nil { return err } @@ -195,7 +195,7 @@ func NewCustomInteropServer(lsOpts *LsOpts, delegate rapidcore.InteropServer, lo func (c *CustomInteropServer) StartAcceptingDirectInvokes() error { log.Traceln("Function called") - err := c.localStackAdapter.SendStatus(Ready) + err := c.localStackAdapter.SendStatus(Ready, []byte{}) if err != nil { return err } @@ -209,6 +209,10 @@ func (c *CustomInteropServer) SendResponse(invokeID string, contentType string, func (c *CustomInteropServer) SendErrorResponse(invokeID string, response *interop.ErrorResponse) error { log.Traceln("Function called") + err := c.localStackAdapter.SendStatus(Error, response.Payload) + if err != nil { + return err + } return c.delegate.SendErrorResponse(invokeID, response) } diff --git a/cmd/localstack/main.go b/cmd/localstack/main.go index 0a2bf45..c98cf75 100644 --- a/cmd/localstack/main.go +++ b/cmd/localstack/main.go @@ -6,8 +6,6 @@ import ( "context" log "github.com/sirupsen/logrus" "go.amzn.com/lambda/rapidcore" - "net/http" - _ "net/http/pprof" "os" "runtime/debug" "strconv" @@ -27,7 +25,7 @@ type LsOpts struct { InitLogLevel string EdgePort string EnableXRayTelemetry string - PostInvokeWaitMS string + PostInvokeWaitMS string } func GetEnvOrDie(env string) string { @@ -55,7 +53,7 @@ func InitLsOpts() *LsOpts { EnableDnsServer: os.Getenv("LOCALSTACK_ENABLE_DNS_SERVER"), EnableXRayTelemetry: os.Getenv("LOCALSTACK_ENABLE_XRAY_TELEMETRY"), LocalstackIP: os.Getenv("LOCALSTACK_HOSTNAME"), - PostInvokeWaitMS: os.Getenv("LOCALSTACK_POST_INVOKE_WAIT_MS"), + PostInvokeWaitMS: os.Getenv("LOCALSTACK_POST_INVOKE_WAIT_MS"), } } @@ -113,7 +111,7 @@ func main() { // download code archive if env variable is set if err := DownloadCodeArchives(lsOpts.CodeArchives); err != nil { - log.Fatal("Failed to download code archives") + log.Fatal("Failed to download code archives: " + err.Error()) } // parse CLI args @@ -168,6 +166,10 @@ func main() { if len(handler) > 0 { sandbox.SetHandler(handler) } + exitChan := make(chan struct{}) + sandbox.AddShutdownFunc(func() { + exitChan <- struct{}{} + }) // initialize all flows and start runtime API go sandbox.Create() @@ -183,10 +185,5 @@ func main() { // start runtime init go InitHandler(sandbox, GetEnvOrDie("AWS_LAMBDA_FUNCTION_VERSION"), int64(invokeTimeoutSeconds)) // TODO: replace this with a custom init - // TODO: make the tracing server optional - // start blocking with the tracing server - err = http.ListenAndServe("0.0.0.0:"+lsOpts.InitTracingPort, http.DefaultServeMux) - if err != nil { - log.Fatal("Failed to start debug server") - } + <-exitChan }