diff --git a/.dockerignore b/.dockerignore deleted file mode 100644 index 817f140..0000000 --- a/.dockerignore +++ /dev/null @@ -1,3 +0,0 @@ -build -deploy -docs diff --git a/build/Dockerfile b/build/Dockerfile index 51d548a..0608953 100644 --- a/build/Dockerfile +++ b/build/Dockerfile @@ -12,12 +12,12 @@ RUN go mod download COPY . ./ # Build -RUN CGO_ENABLED=1 GOOS=linux go build -o /zdravko cmd/zdravko/main.go +RUN CGO_ENABLED=1 GOOS=linux go build -o /bin/zdravko cmd/zdravko/main.go ### # Final production -FROM gcr.io/distroless/base-debian12:nonroot as production -COPY --from=builder /zdravko /zdravko +FROM debian:12-slim as production +COPY --from=builder /bin/zdravko /bin/zdravko COPY LICENSE /LICENSE COPY README.md /README.md diff --git a/cmd/zdravko/main.go b/cmd/zdravko/main.go index c0ae0f7..ddf6269 100644 --- a/cmd/zdravko/main.go +++ b/cmd/zdravko/main.go @@ -86,10 +86,12 @@ func main() { for sig := range c { log.Printf("Received signal: %v", sig) for _, s := range servers { - println("Stopping", s.Name()) - err := s.Stop() - if err != nil { - log.Fatalf("Unable to stop server %s: %v", s.Name(), err) + if s != nil { + println("Stopping", s.Name()) + err := s.Stop() + if err != nil { + log.Fatalf("Unable to stop server %s: %v", s.Name(), err) + } } } } diff --git a/deploy/Dockerfile b/deploy/Dockerfile index 7b889fe..d6da698 100644 --- a/deploy/Dockerfile +++ b/deploy/Dockerfile @@ -1,11 +1,14 @@ # syntax=docker/dockerfile:1 -ARG ALPINE_VERSION=3.14 +ARG ZDRAVKO_VERSION=main + +FROM ghcr.io/mentos1386/zdravko:${ZDRAVKO_VERSION} +RUN apt-get update -y \ + && apt-get install -y ca-certificates fuse3 sqlite3 -FROM alpine:${ALPINE_VERSION} as temporal -RUN apk add ca-certificates fuse3 sqlite COPY --from=flyio/litefs:0.5 /usr/local/bin/litefs /usr/local/bin/litefs -COPY litefs.yaml /etc/litefs.yaml -COPY --from=ghcr.io/mentos1386/zdravko /zdravko /zdravko +COPY deploy/litefs.yaml /etc/litefs.yml +COPY deploy/entrypoint.sh /entrypoint.sh +RUN chmod +x /entrypoint.sh -ENTRYPOINT litefs mount +ENTRYPOINT ["/entrypoint.sh"] diff --git a/deploy/entrypoint.sh b/deploy/entrypoint.sh new file mode 100755 index 0000000..7fc58a2 --- /dev/null +++ b/deploy/entrypoint.sh @@ -0,0 +1,14 @@ +#!/bin/sh + +PROCESS=${FLY_PROCESS_GROUP} + +if [ "$PROCESS" = "server" ]; then + echo "Starting server process" + exec litefs mount -- $@ +elif [ "$PROCESS" = "worker" ]; then + echo "Starting worker process" + exec $@ +else + echo "Unknown process" + exit 1 +fi diff --git a/deploy/fly.toml b/deploy/fly.toml index 3b27f88..24b6617 100644 --- a/deploy/fly.toml +++ b/deploy/fly.toml @@ -5,6 +5,7 @@ primary_region = 'waw' [build] dockerfile = "Dockerfile" + context = "deploy" [env] ROOT_URL = 'https://zdravko.mnts.dev' @@ -17,8 +18,8 @@ primary_region = 'waw' TEMPORAL_SERVER_HOST = 'server.process.zdravko.internal:7233' [processes] - server = "zdravko --temporal=true --server=true --worker=false" - worker = "zdravko --temporal=false --server=false --worker=true" + server = "/zdravko --temporal=true --server=true --worker=false" + worker = "/zdravko --temporal=false --server=false --worker=true" [[mounts]] source = "zdravko_data" diff --git a/deploy/litefs.yaml b/deploy/litefs.yaml index 9fd51dd..f5f57f4 100644 --- a/deploy/litefs.yaml +++ b/deploy/litefs.yaml @@ -7,9 +7,6 @@ fuse: data: dir: "/var/lib/litefs" -exec: - - cmd: "/zdravko --temporal=true --server=true --worker=false" - lease: type: "consul" diff --git a/internal/activities/healthcheck.go b/internal/activities/healthcheck.go index e618148..6b1b9ad 100644 --- a/internal/activities/healthcheck.go +++ b/internal/activities/healthcheck.go @@ -6,16 +6,16 @@ import ( "net/http" ) -type HealtcheckHttpActivityParam struct { +type HealtcheckHttpParam struct { Url string Method string } -type HealthcheckHttpActivityResult struct { +type HealthcheckHttpResult struct { StatusCode int } -func HealthcheckHttpActivityDefinition(ctx context.Context, param HealtcheckHttpActivityParam) (*HealthcheckHttpActivityResult, error) { +func HealthcheckHttp(ctx context.Context, param HealtcheckHttpParam) (*HealthcheckHttpResult, error) { if param.Method == "" { param.Method = "GET" } @@ -38,5 +38,40 @@ func HealthcheckHttpActivityDefinition(ctx context.Context, param HealtcheckHttp log.Printf("HealthcheckHttpActivityDefinition produced statuscode %d for url %s", response.StatusCode, param.Url) - return &HealthcheckHttpActivityResult{StatusCode: response.StatusCode}, nil + return &HealthcheckHttpResult{StatusCode: response.StatusCode}, nil +} + +type HealtcheckHttpAddToHistoryParam struct { + Id string + Success bool + StatusCode int +} + +type HealthcheckHttpAddToHistoryResult struct { +} + +func HealthcheckHttpWriteResult(ctx context.Context, param HealtcheckHttpParam) (*HealthcheckHttpResult, error) { + if param.Method == "" { + param.Method = "GET" + } + + var ( + response *http.Response + err error + ) + + switch param.Method { + case "GET": + response, err = http.Get(param.Url) + case "POST": + response, err = http.Post(param.Url, "application/json", nil) + } + + if err != nil { + return nil, err + } + + log.Printf("HealthcheckHttpActivityDefinition produced statuscode %d for url %s", response.StatusCode, param.Url) + + return &HealthcheckHttpResult{StatusCode: response.StatusCode}, nil } diff --git a/internal/workflows/healthcheck.go b/internal/workflows/healthcheck.go index 8c25455..4aac2c7 100644 --- a/internal/workflows/healthcheck.go +++ b/internal/workflows/healthcheck.go @@ -19,13 +19,13 @@ func HealthcheckHttpWorkflowDefinition(ctx workflow.Context, param HealthcheckHt } ctx = workflow.WithActivityOptions(ctx, options) - activityParam := activities.HealtcheckHttpActivityParam{ + activityParam := activities.HealtcheckHttpParam{ Url: param.Url, Method: param.Method, } - var result *activities.HealthcheckHttpActivityResult - err := workflow.ExecuteActivity(ctx, activities.HealthcheckHttpActivityDefinition, activityParam).Get(ctx, &result) + var result *activities.HealthcheckHttpResult + err := workflow.ExecuteActivity(ctx, activities.HealthcheckHttp, activityParam).Get(ctx, &result) if err != nil { return err } diff --git a/pkg/worker/worker.go b/pkg/worker/worker.go index ed4f027..820e8ac 100644 --- a/pkg/worker/worker.go +++ b/pkg/worker/worker.go @@ -37,7 +37,7 @@ func (w *Worker) Start() error { w.worker.RegisterWorkflow(workflows.HealthcheckHttpWorkflowDefinition) // Register Activities - w.worker.RegisterActivity(activities.HealthcheckHttpActivityDefinition) + w.worker.RegisterActivity(activities.HealthcheckHttp) return w.worker.Run(worker.InterruptCh()) }