Skip to content

Commit bd37a4c

Browse files
wip
1 parent 6fbdf2e commit bd37a4c

File tree

3 files changed

+155
-0
lines changed

3 files changed

+155
-0
lines changed

cmd/worker/main.go

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
package main
2+
3+
import (
4+
"bytes"
5+
"encoding/json"
6+
log "github.com/sirupsen/logrus"
7+
"io"
8+
"net/http"
9+
"os"
10+
"strings"
11+
)
12+
13+
// everything that was previously "stop_container" or similar => now its an internal "worker re-initialization"
14+
15+
func main() {
16+
// SETUP
17+
// os.Args
18+
runtimesEnv := os.Getenv("COMPATIBLE_RUNTIMES")
19+
archsEnv := os.Getenv("COMPATIBLE_ARCHITECTURES")
20+
lsEndpoint := "http://" + os.Getenv("LOCALSTACK_HOSTNAME") + ":" + os.Getenv("EDGE_PORT")
21+
22+
// REGISTRATION
23+
registrationInfo := RegistrationInfo{
24+
CompatibleRuntimes: strings.Split(runtimesEnv, ";"),
25+
CompatibleArchitectures: strings.Split(archsEnv, ";"),
26+
}
27+
workerId, err := registerWorker(lsEndpoint, registrationInfo)
28+
if err != nil {
29+
log.Fatal(err)
30+
}
31+
defer func() {
32+
deregisterWorker(workerId)
33+
}()
34+
35+
// LOOP
36+
cmdChan := setupCommandChannel()
37+
for cmd := range cmdChan {
38+
// switch based on event
39+
40+
switch cmd {
41+
case "INIT":
42+
reinitializeExecutionEnvironment()
43+
44+
startRapid()
45+
}
46+
47+
}
48+
49+
}
50+
51+
func deregisterWorker(id string) {
52+
// TODO
53+
}
54+
55+
func startRapid() {
56+
// TODO
57+
}
58+
59+
func setupCommandChannel() chan string {
60+
// TODO
61+
}
62+
63+
func registerWorker(endpoint string, info RegistrationInfo) (string, error) {
64+
marshalled, err := json.Marshal(info)
65+
if err != nil {
66+
return "", err
67+
}
68+
post, err := http.Post(endpoint+"/register", "application/json", bytes.NewReader(marshalled))
69+
if err != nil {
70+
return "", err
71+
}
72+
content, err := io.ReadAll(post.Body)
73+
if err != nil {
74+
return "", err
75+
}
76+
var response RegisterResponse
77+
if err = json.Unmarshal(content, &response); err != nil {
78+
return "", err
79+
}
80+
return response.WorkerId, nil
81+
}
82+
83+
func reinitializeExecutionEnvironment() {
84+
// clear environment from side-effects (e.g. /tmp, /var/task, /opt, ...)
85+
// reset environment variables
86+
}
87+
88+
func clearDirectory(clearPath string) error {
89+
// TODO
90+
return nil
91+
}
92+
93+
// POST /worker
94+
95+
// GET /worker/{id}/command (blocking call, don't set a timeout)
96+
97+
// POST /worker/{id}/error
98+
// POST /worker/{id}/exit
99+
100+
type RegistrationInfo struct {
101+
CompatibleRuntimes []string `json:"compatibleRuntimes"`
102+
CompatibleArchitectures []string `json:"compatibleArchitectures"`
103+
}
104+
105+
type RegisterResponse struct {
106+
WorkerId string `json:"workerId"`
107+
}
108+
109+
type InitEvent struct {
110+
Environment map[string]string `json:"environment"`
111+
}

worker-debugging/Dockerfile

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
FROM public.ecr.aws/lambda/python:3.9
2+
3+
# networking tools
4+
RUN yum install -y iproute lsof iputils bind-utils
5+
# process management
6+
RUN yum install -y htop psmisc procps-ng lsof strace
7+
# misc
8+
RUN yum install -y vim pstree ncdu
9+
10+
# add /usr/sbin since some packages install binaries here and the image doesn't have it in path by default
11+
ENV PATH=/var/lang/bin:/usr/local/bin:/usr/bin/:/bin:/opt/bin:/usr/sbin
12+
13+
COPY app.py ${LAMBDA_TASK_ROOT}
14+
15+
CMD ["app.handler"]
16+
17+
ENV RUNTIME_COMPATIBLITY="python3.9"
18+
19+
ENTRYPOINT ["/var/rapid/worker", "/var/rapid/init"]

worker-debugging/docker-compose.yaml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
version: "3.8"
2+
3+
services:
4+
5+
localstack-worker-python:
6+
image: localstack/lambda-worker:python3.9
7+
environment:
8+
LOCALSTACK_HOSTNAME: "localhost"
9+
EDGE_PORT: 4566
10+
# COMPATIBLE_RUNTIMES: "python3.9;python3.8;python3.7"
11+
# COMPATIBLE_ARCHITECTURES: "arm64"
12+
13+
localstack:
14+
container_name: "${LOCALSTACK_DOCKER_NAME-localstack_main}"
15+
image: localstack/localstack
16+
ports:
17+
- "127.0.0.1:4566:4566" # LocalStack Gateway
18+
- "127.0.0.1:4510-4559:4510-4559" # external services port range
19+
environment:
20+
- DEBUG=${DEBUG-}
21+
- LAMBDA_EXECUTOR=${LAMBDA_EXECUTOR-}
22+
- DOCKER_HOST=unix:///var/run/docker.sock
23+
volumes:
24+
- "${LOCALSTACK_VOLUME_DIR:-./volume}:/var/lib/localstack"
25+
- "/var/run/docker.sock:/var/run/docker.sock"

0 commit comments

Comments
 (0)