Skip to content

feat(containers): long running background task #95

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions containers/long-running-task-go/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
FROM golang:1.23-alpine3.21 AS builder

WORKDIR /app

COPY go.* ./
RUN go mod download

COPY . ./

RUN go build -o server ./cmd/server

FROM alpine:3.21

COPY --from=builder /app/server /app/server

CMD ["/app/server"]
11 changes: 11 additions & 0 deletions containers/long-running-task-go/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Long Running Task

This example showcases running a long background task in a container during the 15m retention period.

## Deploying

This example can be deployed using the Scaleway CLI:

```bash
scw container deploy region=pl-waw
```
117 changes: 117 additions & 0 deletions containers/long-running-task-go/cmd/server/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package main

import (
"context"
"errors"
"log/slog"
"net/http"
"os"
"os/signal"
"sync"
"syscall"
"time"

"github.com/google/uuid"
)

var (
signalChan = make(chan os.Signal, 1)
wg sync.WaitGroup

disableGracefulShutdown = os.Getenv("DISABLE_GRACEFUL_SHUTDOWN") != ""

longRunningJobDuration = os.Getenv("LONG_RUNNING_JOB_DURATION")
parsedLongRunningJobDuration time.Duration
defaultLongRunningJobDuration = 14 * time.Minute
)

func main() {
slog.SetDefault(slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
AddSource: true,
})))

if longRunningJobDuration != "" {
var err error

parsedLongRunningJobDuration, err = time.ParseDuration(longRunningJobDuration)
if err != nil {
slog.Error("failed to parse LONG_RUNNING_JOB_DURATION", "error", err)
os.Exit(1)
}

slog.Info("LONG_RUNNING_JOB_DURATION set", "duration", parsedLongRunningJobDuration)
} else {
parsedLongRunningJobDuration = defaultLongRunningJobDuration
slog.Info("LONG_RUNNING_JOB_DURATION not set, using default", "duration", parsedLongRunningJobDuration)
}

srv := &http.Server{
Addr: ":8080",
Handler: http.HandlerFunc(handler),
}

// Handle SIGTERM.
if !disableGracefulShutdown {
signal.Notify(signalChan, syscall.SIGTERM)
}

// Start HTTP server.
if disableGracefulShutdown {
slog.Info("server started")

if err := srv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
slog.Error("server failed to start", "error", err)
os.Exit(1)
}
} else {
go func() {
slog.Info("server started")

if err := srv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
slog.Error("server failed to start", "error", err)
os.Exit(1)
}
}()

sig := <-signalChan
slog.Info("received signal", "signal", sig)

// This should not take a lot of time because the handler is non-blocking.
if err := srv.Shutdown(context.Background()); err != nil {
slog.Error("server failed to shutdown", "error", err)
}

// This is where we wait for the long running tasks to finish.
wg.Wait()
}
}

func handler(w http.ResponseWriter, _ *http.Request) {
wg.Add(1)

taskID := uuid.NewString()
go longBackgroundTask(taskID, parsedLongRunningJobDuration)

w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte("Started long background task: " + taskID))
}

func longBackgroundTask(taskID string, duration time.Duration) {
defer wg.Done()

slog.Info("long background task started", "taskID", taskID, "duration", duration)

sleepTime := 1 * time.Second
numIterations := int(duration / sleepTime)

for i := range numIterations {
slog.Info("long background task running...",
"iteration", i,
"total", numIterations,
"taskID", taskID)

time.Sleep(sleepTime)
}

slog.Info("long background task finished", "taskID", taskID)
}
7 changes: 7 additions & 0 deletions containers/long-running-task-go/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module github.com/scaleway/serverless-examples/containers/long-running-task-go

go 1.23

toolchain go1.23.4

require github.com/google/uuid v1.6.0
2 changes: 2 additions & 0 deletions containers/long-running-task-go/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
Loading