-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1541 from ministryofjustice/MLPAB-2408-run-schedu…
…led-events MLPAB-2408: Add scheduled task schedule runner
- Loading branch information
Showing
39 changed files
with
907 additions
and
382 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package main | ||
|
||
import ( | ||
"cmp" | ||
"context" | ||
"fmt" | ||
"log/slog" | ||
"net/http" | ||
"os" | ||
"time" | ||
|
||
"github.com/aws/aws-lambda-go/lambda" | ||
"github.com/aws/aws-sdk-go-v2/config" | ||
"github.com/ministryofjustice/opg-modernising-lpa/internal/donor" | ||
"github.com/ministryofjustice/opg-modernising-lpa/internal/dynamo" | ||
"github.com/ministryofjustice/opg-modernising-lpa/internal/event" | ||
"github.com/ministryofjustice/opg-modernising-lpa/internal/localize" | ||
"github.com/ministryofjustice/opg-modernising-lpa/internal/notify" | ||
"github.com/ministryofjustice/opg-modernising-lpa/internal/scheduled" | ||
"github.com/ministryofjustice/opg-modernising-lpa/internal/search" | ||
"github.com/ministryofjustice/opg-modernising-lpa/internal/secrets" | ||
) | ||
|
||
func handleRunSchedule(ctx context.Context) error { | ||
var ( | ||
eventBusName = cmp.Or(os.Getenv("EVENT_BUS_NAME"), "default") | ||
notifyBaseURL = os.Getenv("GOVUK_NOTIFY_BASE_URL") | ||
notifyIsProduction = os.Getenv("GOVUK_NOTIFY_IS_PRODUCTION") == "1" | ||
searchEndpoint = os.Getenv("SEARCH_ENDPOINT") | ||
searchIndexName = cmp.Or(os.Getenv("SEARCH_INDEX_NAME"), "lpas") | ||
searchIndexingEnabled = os.Getenv("SEARCH_INDEXING_DISABLED") != "1" | ||
tableName = os.Getenv("LPAS_TABLE") | ||
) | ||
|
||
logger := slog.New(slog.NewJSONHandler(os.Stdout, nil). | ||
WithAttrs([]slog.Attr{ | ||
slog.String("service_name", "opg-modernising-lpa/schedule-runner"), | ||
})) | ||
|
||
cfg, err := config.LoadDefaultConfig(ctx) | ||
if err != nil { | ||
return fmt.Errorf("failed to load default config: %w", err) | ||
} | ||
|
||
secretsClient, err := secrets.NewClient(cfg, time.Hour) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
notifyApiKey, err := secretsClient.Secret(ctx, secrets.GovUkNotify) | ||
if err != nil { | ||
return fmt.Errorf("failed to get notify API secret: %w", err) | ||
} | ||
|
||
bundle, err := localize.NewBundle("./lang/en.json", "./lang/cy.json") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
notifyClient, err := notify.New(logger, notifyIsProduction, notifyBaseURL, notifyApiKey, http.DefaultClient, event.NewClient(cfg, eventBusName), bundle) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
dynamoClient, err := dynamo.NewClient(cfg, tableName) | ||
if err != nil { | ||
return fmt.Errorf("failed to create dynamodb client: %w", err) | ||
} | ||
|
||
eventClient := event.NewClient(cfg, eventBusName) | ||
|
||
searchClient, err := search.NewClient(cfg, searchEndpoint, searchIndexName, searchIndexingEnabled) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
donorStore := donor.NewStore(dynamoClient, eventClient, logger, searchClient) | ||
scheduledStore := scheduled.NewStore(dynamoClient) | ||
|
||
runner := scheduled.NewRunner(logger, scheduledStore, donorStore, notifyClient) | ||
|
||
if err := runner.Run(ctx); err != nil { | ||
logger.Error("runner error", slog.Any("err", err)) | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func main() { | ||
lambda.Start(handleRunSchedule) | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
misconfigurations: | ||
- id: AVD-DS-0002 | ||
statement: Lambda creates a docker USER with least-privilege permissions. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
FROM golang:1.23.1-alpine AS build | ||
|
||
WORKDIR /app | ||
|
||
COPY --link go.mod go.sum ./ | ||
RUN go mod download | ||
|
||
COPY --link cmd/schedule-runner ./cmd/schedule-runner | ||
COPY --link internal ./internal | ||
|
||
RUN GOOS=${TARGETOS} GOARCH=${TARGETARCH} CGO_ENABLED=0 go build -o schedule-runner ./cmd/schedule-runner | ||
|
||
FROM public.ecr.aws/lambda/provided:al2023.2024.10.14.12 AS dev | ||
|
||
WORKDIR /app | ||
|
||
COPY --from=build /app/schedule-runner /var/task/schedule-runner | ||
COPY --link lang /var/task/lang | ||
COPY --link docker/aws-lambda-rie ./aws-lambda-rie | ||
|
||
ENTRYPOINT ["./schedule-runner"] | ||
|
||
FROM public.ecr.aws/lambda/provided:al2023.2024.10.14.12 AS production | ||
|
||
WORKDIR /app | ||
COPY --link docker/install_lambda_insights.sh /app/ | ||
|
||
RUN chmod +x "/app/install_lambda_insights.sh" \ | ||
&& /app/install_lambda_insights.sh "${TARGETPLATFORM}" | ||
|
||
COPY --from=build /app/schedule-runner ./schedule-runner | ||
COPY --link lang ./lang | ||
|
||
ENTRYPOINT ["./schedule-runner"] |
Oops, something went wrong.