|
| 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 | +} |
0 commit comments