Skip to content

Commit

Permalink
Health Check Function Implemented - Wakeup Call
Browse files Browse the repository at this point in the history
  • Loading branch information
lakshayman committed Feb 6, 2024
1 parent 23a0d13 commit 2427f77
Show file tree
Hide file tree
Showing 11 changed files with 454 additions and 24 deletions.
5 changes: 4 additions & 1 deletion call-profile/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,10 @@ func handler(request events.APIGatewayProxyRequest) (events.APIGatewayProxyRespo
userUrl = userUrl + "/"
}
var isServiceRunning bool
_, serviceErr := http.Get(userUrl + "health")
c := &http.Client{
Timeout: 5 * time.Second,
}
_, serviceErr := c.Get(userUrl + "health")
if serviceErr != nil {
isServiceRunning = false
} else {
Expand Down
52 changes: 46 additions & 6 deletions health-check/go.mod
Original file line number Diff line number Diff line change
@@ -1,9 +1,49 @@
module healthCheck

go 1.21.5

require (
github.com/aws/aws-lambda-go v1.28.0
github.com/stretchr/testify v1.6.1
gopkg.in/yaml.v3 v3.0.1 // indirect
cloud.google.com/go/firestore v1.14.0
firebase.google.com/go v3.13.0+incompatible
github.com/aws/aws-lambda-go v1.46.0
github.com/aws/aws-sdk-go v1.50.11
google.golang.org/api v0.162.0
)

module health

go 1.16
require (
cloud.google.com/go v0.111.0 // indirect
cloud.google.com/go/compute v1.23.3 // indirect
cloud.google.com/go/compute/metadata v0.2.3 // indirect
cloud.google.com/go/iam v1.1.5 // indirect
cloud.google.com/go/longrunning v0.5.4 // indirect
cloud.google.com/go/storage v1.30.1 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/s2a-go v0.1.7 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.3.2 // indirect
github.com/googleapis/gax-go/v2 v2.12.0 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
go.opencensus.io v0.24.0 // indirect
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.47.0 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.47.0 // indirect
go.opentelemetry.io/otel v1.22.0 // indirect
go.opentelemetry.io/otel/metric v1.22.0 // indirect
go.opentelemetry.io/otel/trace v1.22.0 // indirect
golang.org/x/crypto v0.18.0 // indirect
golang.org/x/net v0.20.0 // indirect
golang.org/x/oauth2 v0.16.0 // indirect
golang.org/x/sync v0.6.0 // indirect
golang.org/x/sys v0.16.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/time v0.5.0 // indirect
google.golang.org/appengine v1.6.8 // indirect
google.golang.org/genproto v0.0.0-20240116215550-a9fa1716bcac // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240102182953-50ed04b92917 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240125205218-1f4bbc51befe // indirect
google.golang.org/grpc v1.61.0 // indirect
google.golang.org/protobuf v1.32.0 // indirect
)
216 changes: 204 additions & 12 deletions health-check/go.sum

Large diffs are not rendered by default.

124 changes: 122 additions & 2 deletions health-check/main.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,138 @@
package main

import (
"context"
"fmt"
"log"
"os"
"net/http"
"sync"
"time"

"cloud.google.com/go/firestore"
firebase "firebase.google.com/go"

"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ssm"

"google.golang.org/api/iterator"
"google.golang.org/api/option"
)

var wg sync.WaitGroup

/*
Setting Constants Map
*/
var Constants map[string]string = map[string]string{
"ENV_DEVELOPMENT": "DEVELOPMENT",
"ENV_PRODUCTION": "PRODUCTION",
"FIRE_STORE_CRED": "firestoreCred",
}

/*
Setting Firestore Key for development/production
*/
func getFirestoreKey() string {
if os.Getenv(("environment")) == Constants["ENV_DEVELOPMENT"] {
return os.Getenv(Constants["FIRE_STORE_CRED"])
} else if os.Getenv(("environment")) == Constants["ENV_PRODUCTION"] {
var parameterName string = Constants["FIRE_STORE_CRED"]

sess := session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
}))

svc := ssm.New(sess)

results, err := svc.GetParameter(&ssm.GetParameterInput{
Name: &parameterName,
})
if err != nil {
log.Fatalf(err.Error())
}

return *results.Parameter.Value
} else {
return ""
}
}

/*
Function to initialize the firestore client
*/
func initializeFirestoreClient(ctx context.Context) (*firestore.Client, error) {
sa := option.WithCredentialsJSON([]byte(getFirestoreKey()))
app, err := firebase.NewApp(ctx, nil, sa)
if err != nil {
return nil, err
}

client, err := app.Firestore(ctx)
if err != nil {
return nil, err
}

return client, nil
}

func callProfileHealth(userUrl string) {

defer wg.Done()

httpClient := &http.Client{
Timeout: 2 * time.Second,
}
if userUrl[len(userUrl)-1] != '/' {
userUrl = userUrl + "/"
}

requestURL := fmt.Sprintf("%shealth", userUrl)
req, _ := http.NewRequest("GET", requestURL, nil)
_, err1 := httpClient.Do(req)
if err1 != nil {
fmt.Println("Service not running", err1)
}
}

func handler(request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
ctx := context.Background()
client, err := initializeFirestoreClient(ctx)

if err != nil {
return events.APIGatewayProxyResponse{}, err
}

totalProfilesCalled := 0

iter := client.Collection("users").Where("profileStatus", "==", "VERIFIED").Documents(ctx)
for {
doc, err := iter.Next()
if err == iterator.Done {
break
}
if err != nil {
log.Fatalf("Failed to iterate: %v", err)
}
if str, ok := doc.Data()["profileURL"].(string); ok {
fmt.Println(str)
totalProfilesCalled += 1
wg.Add(1)
go callProfileHealth(str)
}
}

wg.Wait()

defer client.Close()
return events.APIGatewayProxyResponse{
Body: "Awesome, Server health is good!!!",
Body: fmt.Sprintf("Total Profiles called in session is %d", totalProfilesCalled),
StatusCode: 200,
}, nil
}

func main() {
lambda.Start(handler)
}
}
9 changes: 9 additions & 0 deletions health/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require (
github.com/aws/aws-lambda-go v1.28.0
github.com/stretchr/testify v1.6.1
gopkg.in/yaml.v3 v3.0.1 // indirect
)

module health

go 1.16
23 changes: 23 additions & 0 deletions health/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/aws/aws-lambda-go v1.28.0 h1:fZiik1PZqW2IyAN4rj+Y0UBaO1IDFlsNo9Zz/XnArK4=
github.com/aws/aws-lambda-go v1.28.0/go.mod h1:jJmlefzPfGnckuHdXX7/80O3BvUUi12XOkbv4w9SGLU=
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/urfave/cli/v2 v2.2.0/go.mod h1:SE9GqnLQmjVa0iPEY0f1w3ygNIYcIJ0OKPMoW2caLfQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
18 changes: 18 additions & 0 deletions health/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package main

import (
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
)

func handler(request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {

return events.APIGatewayProxyResponse{
Body: "Awesome, Server health is good!!!",
StatusCode: 200,
}, nil
}

func main() {
lambda.Start(handler)
}
File renamed without changes.
4 changes: 3 additions & 1 deletion scripts/dev.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
cd health-check
cd health
go mod tidy
cd ../health-check
go mod tidy
cd ../verify
go mod tidy
Expand Down
2 changes: 1 addition & 1 deletion scripts/test.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cd health-check
cd health
go mod tidy
go test -v
npx kill-port 8090
Expand Down
25 changes: 24 additions & 1 deletion template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,24 @@ Globals:
baseURL: YourBaseAPIURL

Resources:
HealthFunction:
Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
Metadata:
BuildMethod: go1.x
Properties:
CodeUri: health/
Handler: bootstrap
Runtime: provided.al2023
Architectures:
- x86_64
Tracing: Active
Events:
CatchAll:
Type: Api # More info about API Event Source: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#api
Properties:
Path: /health
Method: GET

HealthCheckFunction:
Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
Metadata:
Expand All @@ -29,10 +47,15 @@ Resources:
- x86_64
Tracing: Active
Events:
ScheduledEvent:
Type: Schedule
Properties:
Schedule: cron(55 17 ? * WED *)
Enabled: True
CatchAll:
Type: Api # More info about API Event Source: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#api
Properties:
Path: /health
Path: /health-check
Method: GET

VerifyFunction:
Expand Down

0 comments on commit 2427f77

Please sign in to comment.