Skip to content

Commit

Permalink
tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
thdxr committed Mar 2, 2024
1 parent 2242a49 commit 4bb7d0b
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 45 deletions.
1 change: 0 additions & 1 deletion examples/test/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
import "./asfasdasd";
export function handler() {}
10 changes: 6 additions & 4 deletions examples/test/sst.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@ export default $config({
removalPolicy: "retain-all",
providers: {
aws: {},
cloudflare: {},
},
};
},
async run() {
new sst.aws.Function("MyFunction", {
handler: "src/lambda.handler",
const fn = new sst.aws.Function("MyFunction", {
handler: "src/index.handler",
url: true,
});
return {};
return {
url: fn.url,
};
},
});
7 changes: 7 additions & 0 deletions pkg/platform/bridge/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,13 @@ func run() error {
return token.Error()
}

if token := mqttClient.Subscribe(prefix+"/kill", 1, func(c MQTT.Client, m MQTT.Message) {
slog.Info("received kill message")
cancel()
}); token.Wait() && token.Error() != nil {
return token.Error()
}

if token := mqttClient.Publish(prefix+"/init", 1, false, initPayload); token.Wait() && token.Error() != nil {
return token.Error()
}
Expand Down
10 changes: 1 addition & 9 deletions pkg/platform/src/config.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
import type { ProviderArgs as AWS } from "@pulumi/aws";
import type { ProviderArgs as Cloudflare } from "@pulumi/cloudflare";

export interface App {
name: string;
removalPolicy?: "remove" | "retain" | "retain-all";
providers?: {
aws?: AWS;
cloudflare?: Cloudflare & {
accountId?: string;
};
};
}

export interface Config {
app(input: { stage?: string }): App;
run(): any;
providers?: Record<string, any>;
}

export function $config(input: Config): Config {
Expand Down
13 changes: 12 additions & 1 deletion pkg/project/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,22 @@ func (p *Project) writeTypes() error {
file.WriteString("\n\n")

for name := range p.app.Providers {
file.WriteString(`import _` + name + ` from "@pulumi/` + name + `";` + "\n")
file.WriteString(`import _` + name + `, { ProviderArgs as _` + name + `Args } from "@pulumi/` + name + `";` + "\n")
}

file.WriteString("\n\n")

file.WriteString(`declare module "./src/config" {` + "\n")
file.WriteString(` interface App {` + "\n")
file.WriteString(` providers?: {` + "\n")
for name := range p.app.Providers {
file.WriteString(` ` + name + `?: _` + name + `Args;` + "\n")
}
file.WriteString(` }` + "\n")
file.WriteString(` }` + "\n")
file.WriteString(`}` + "\n")
file.WriteString("\n")

file.WriteString(`declare global {` + "\n")
for name := range p.app.Providers {
file.WriteString(` // @ts-expect-error` + "\n")
Expand Down
86 changes: 56 additions & 30 deletions pkg/server/dev/aws/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,33 +227,43 @@ func Start(
workerEnv := map[string][]string{}
builds := map[string]*runtime.BuildOutput{}

run := func(functionID string, workerID string) bool {
getBuildOutput := func(functionID string) *runtime.BuildOutput {
build := builds[functionID]
if build != nil {
return build
}
warp := complete.Warps[functionID]
if build == nil {
build, err = runtime.Build(ctx, &runtime.BuildInput{
Warp: warp,
Project: p,
Dev: true,
Links: complete.Links,
build, err = runtime.Build(ctx, &runtime.BuildInput{
Warp: warp,
Project: p,
Dev: true,
Links: complete.Links,
})
if err == nil {
bus.Publish(&FunctionBuildEvent{
FunctionID: functionID,
Errors: build.Errors,
})
} else {
bus.Publish(&FunctionBuildEvent{
FunctionID: functionID,
Errors: []string{err.Error()},
})
if err == nil {
bus.Publish(&FunctionBuildEvent{
FunctionID: functionID,
Errors: build.Errors,
})
} else {
bus.Publish(&FunctionBuildEvent{
FunctionID: functionID,
Errors: []string{err.Error()},
})
}
if err != nil || len(build.Errors) > 0 {
delete(builds, functionID)
return false
}
builds[functionID] = build
}
if err != nil || len(build.Errors) > 0 {
delete(builds, functionID)
return nil
}
builds[functionID] = build
return build
}

run := func(functionID string, workerID string) bool {
build := getBuildOutput(functionID)
if build == nil {
return false
}
warp := complete.Warps[functionID]
worker, _ := runtime.Run(ctx, &runtime.RunInput{
Server: server + workerID,
Project: p,
Expand Down Expand Up @@ -360,7 +370,16 @@ func Start(
result, _ := http.Post("http://"+server+workerID+"/runtime/init/error", "application/json", strings.NewReader(`{"errorMessage":"Function failed to build"}`))
defer result.Body.Close()
body, _ := io.ReadAll(result.Body)
slog.Info("error", "body", string(body))
slog.Info("error", "body", string(body), "status", result.StatusCode)

if result.StatusCode != 202 {
result, _ := http.Get("http://" + server + workerID + "/runtime/invocation/next")
requestID := result.Header.Get("lambda-runtime-aws-request-id")
result, _ = http.Post("http://"+server+workerID+"/runtime/invocation/"+requestID+"/error", "application/json", strings.NewReader(`{"errorMessage":"Function failed to build"}`))
defer result.Body.Close()
body, _ := io.ReadAll(result.Body)
slog.Info("error", "body", string(body), "status", result.StatusCode)
}
}
break

Expand All @@ -374,24 +393,31 @@ func Start(
delete(workers, workerID)
delete(workerEnv, workerID)
case event := <-fileChan:
functions := map[string]bool{}
slog.Info("checking if code needs to be rebuilt", "file", event.Path)
toBuild := map[string]bool{}

for workerID, info := range workers {
warp, ok := complete.Warps[info.FunctionID]
if !ok {
continue
}
slog.Info("checking rebuild", "workerID", workerID, "functionID", info.FunctionID, "path", event.Path)
if runtime.ShouldRebuild(warp.Runtime, warp.FunctionID, event.Path) {
slog.Info("rebuilding", "workerID", workerID, "functionID", info.FunctionID)
slog.Info("stopping", "workerID", workerID, "functionID", info.FunctionID)
info.Worker.Stop()
delete(builds, info.FunctionID)
functions[info.FunctionID] = true
toBuild[info.FunctionID] = true
}
}

for functionID := range toBuild {
output := getBuildOutput(functionID)
if output == nil {
delete(toBuild, functionID)
}
}

for workerID, info := range workers {
if functions[info.FunctionID] {
slog.Info("restarting", "workerID", workerID, "functionID", info.FunctionID)
if toBuild[info.FunctionID] {
run(info.FunctionID, workerID)
}
}
Expand Down

0 comments on commit 4bb7d0b

Please sign in to comment.