Skip to content

Commit

Permalink
Feat/add cron job (#178)
Browse files Browse the repository at this point in the history
* feat: add lambda

* feat: add cron job entry point

* feat: deploy to lambda using github actions

---------

Co-authored-by: Eyo Chen <eyo.chen@amazingtalker.com>
  • Loading branch information
eyo-chen and Eyo Chen authored Nov 17, 2024
1 parent 4cc723a commit 0d59138
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 0 deletions.
61 changes: 61 additions & 0 deletions .github/workflows/cd-lambda.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
name: Let's Deploy Cron Lambda

on:
workflow_run:
workflows: ["Let's Go CI"]
types:
- completed
branches: [main]

jobs:
deploy-lambda:
if: |
github.event.workflow_run.conclusion == 'success' &&
github.event.workflow_run.head_branch == 'main'
runs-on: ubuntu-latest

steps:
- name: Checkout Source
uses: actions/checkout@v4

- name: Get changed files
id: changed-files
uses: tj-actions/changed-files@v42

- name: Check if relevant files changed
id: check-changes
run: |
RELEVANT=false
for file in ${{ steps.changed-files.outputs.all_changed_files }}; do
if [[ $file == cmd/cron/* ]]; then
RELEVANT=true
break
fi
done
if [ "$RELEVANT" = "false" ]; then
echo "No relevant files changed, skipping deployment"
exit 78 # Special exit code that GitHub Actions recognizes as neutral
fi
- name: Setup Go
uses: actions/setup-go@v5
with:
go-version: '1.22.x'

- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v4
with:
aws-access-key-id: ${{ secrets.AWS_KEY }}
aws-secret-access-key: ${{ secrets.AWS_SECRET }}
aws-region: ${{ secrets.AWS_REGION }}

- name: Build Lambda Function
run: |
GOOS=linux GOARCH=amd64 go build -o bootstrap cmd/cron/main.go
zip go-lambda.zip bootstrap
- name: Deploy to Lambda
run: |
aws lambda update-function-code \
--function-name ${{ secrets.AWS_LAMBDA_FUNCTION_NAME }} \
--zip-file fileb://go-lambda.zip
67 changes: 67 additions & 0 deletions cmd/cron/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package main

import (
"context"
"database/sql"
"fmt"
"os"
"time"

"github.com/aws/aws-lambda-go/lambda"
adapter "github.com/eyo-chen/expense-tracker-go/internal/adapter"
"github.com/eyo-chen/expense-tracker-go/internal/usecase/monthlytrans"
"github.com/eyo-chen/expense-tracker-go/pkg/logger"
_ "github.com/go-sql-driver/mysql"
)

func main() {
lambda.Start(handleRequest)
}

func handleRequest(ctx context.Context) error {
logger.Register()

logger.Info("Connecting to database...")
mysqlDB, err := newMysqlDB()
if err != nil {
logger.Error("Unable to connect to mysql database", "error", err)
return err
}
defer mysqlDB.Close()

// Setup adapter and usecase
adapter := adapter.New(mysqlDB, nil, nil, nil, "")
monthlyTransUC := monthlytrans.New(adapter.MonthlyTrans, adapter.Transaction)

// Execute monthly transaction aggregation for previous month
previousMonth := time.Now().AddDate(0, -1, 0)
if err := monthlyTransUC.Create(ctx, previousMonth); err != nil {
logger.Error("Failed to create monthly transaction aggregation", "error", err)
return err
}

logger.Info("Successfully created monthly transaction aggregation", "month", previousMonth.Format(time.DateOnly))
return nil
}

func newMysqlDB() (*sql.DB, error) {
config := map[string]string{
"host": os.Getenv("DB_HOST"),
"port": os.Getenv("DB_PORT"),
"name": os.Getenv("DB_NAME"),
"user": os.Getenv("DB_USER"),
"password": os.Getenv("DB_PASSWORD"),
}

dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?parseTime=true", config["user"], config["password"], config["host"], config["port"], config["name"])
db, err := sql.Open("mysql", dsn)
if err != nil {
return nil, err
}

if err = db.Ping(); err != nil {
return nil, err
}

return db, nil
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ require (
github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161 // indirect
github.com/Microsoft/go-winio v0.6.2 // indirect
github.com/Nvveen/Gotty v0.0.0-20120604004816-cd527374f1e5 // indirect
github.com/aws/aws-lambda-go v1.47.0 // indirect
github.com/aws/aws-sdk-go-v2 v1.31.0 // indirect
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.6.5 // indirect
github.com/aws/aws-sdk-go-v2/config v1.27.36 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ github.com/Microsoft/go-winio v0.6.2 h1:F2VQgta7ecxGYO8k3ZZz3RS8fVIXVxONVUPlNERo
github.com/Microsoft/go-winio v0.6.2/go.mod h1:yd8OoFMLzJbo9gZq8j5qaps8bJ9aShtEA8Ipt1oGCvU=
github.com/Nvveen/Gotty v0.0.0-20120604004816-cd527374f1e5 h1:TngWCqHvy9oXAN6lEVMRuU21PR1EtLVZJmdB18Gu3Rw=
github.com/Nvveen/Gotty v0.0.0-20120604004816-cd527374f1e5/go.mod h1:lmUJ/7eu/Q8D7ML55dXQrVaamCz2vxCfdQBasLZfHKk=
github.com/aws/aws-lambda-go v1.47.0 h1:0H8s0vumYx/YKs4sE7YM0ktwL2eWse+kfopsRI1sXVI=
github.com/aws/aws-lambda-go v1.47.0/go.mod h1:dpMpZgvWx5vuQJfBt0zqBha60q7Dd7RfgJv23DymV8A=
github.com/aws/aws-sdk-go-v2 v1.30.5 h1:mWSRTwQAb0aLE17dSzztCVJWI9+cRMgqebndjwDyK0g=
github.com/aws/aws-sdk-go-v2 v1.30.5/go.mod h1:CT+ZPWXbYrci8chcARI3OmI/qgd+f6WtuLOoaIA8PR0=
github.com/aws/aws-sdk-go-v2 v1.31.0 h1:3V05LbxTSItI5kUqNwhJrrrY1BAXxXt0sN0l72QmG5U=
Expand Down

0 comments on commit 0d59138

Please sign in to comment.