From 17ce0d05a374102a82d0e498ab41a0c460fb24f1 Mon Sep 17 00:00:00 2001 From: Nicolas Lopes Date: Fri, 1 Mar 2024 00:35:37 -0300 Subject: [PATCH] queue engine for deploys --- .env | 4 +++- config/config.go | 17 +++++++++++++++-- docker-compose.yml | 8 ++++++++ go.mod | 3 +++ go.sum | 6 ++++++ lib/deploy/handler.go | 21 +++++++++++++++++++++ 6 files changed, 56 insertions(+), 3 deletions(-) create mode 100644 docker-compose.yml diff --git a/.env b/.env index 4220b16..ac7c25e 100644 --- a/.env +++ b/.env @@ -1,2 +1,4 @@ AWS_BUCKET_NAME="shipthing" -AWS_REGION="us-east-1" \ No newline at end of file +AWS_REGION="us-east-1" +REDIS_ADDR="localhost:6379" +REDIS_PASSWORD="passwd" \ No newline at end of file diff --git a/config/config.go b/config/config.go index 6a788db..d6559de 100644 --- a/config/config.go +++ b/config/config.go @@ -7,11 +7,13 @@ import ( "github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/service/s3/s3manager" "github.com/joho/godotenv" + "github.com/redis/go-redis/v9" ) var ( - AwsSession *session.Session - S3Uploader *s3manager.Uploader + AwsSession *session.Session + S3Uploader *s3manager.Uploader + RedisClient *redis.Client ) func InitConfig() error { @@ -28,6 +30,7 @@ func InitConfig() error { } S3Uploader = createS3Uploader(AwsSession) + RedisClient = createRedisClient() return nil } @@ -47,3 +50,13 @@ func createAwsSession() (*session.Session, error) { func createS3Uploader(session *session.Session) *s3manager.Uploader { return s3manager.NewUploader(session) } + +func createRedisClient() *redis.Client { + redisClient := redis.NewClient(&redis.Options{ + Addr: os.Getenv("REDIS_ADDR"), + Password: os.Getenv("REDIS_PASSWORD"), + DB: 0, + }) + + return redisClient +} diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..6b2b09c --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,8 @@ +version: '3.8' +services: + redis: + image: redis:6.2-alpine + restart: always + ports: + - '6379:6379' + command: redis-server --loglevel warning --requirepass passwd \ No newline at end of file diff --git a/go.mod b/go.mod index 59529bd..39e56a8 100644 --- a/go.mod +++ b/go.mod @@ -12,8 +12,10 @@ require ( require ( github.com/bytedance/sonic v1.11.2 // indirect + github.com/cespare/xxhash/v2 v2.2.0 // indirect github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect github.com/chenzhuoyu/iasm v0.9.1 // indirect + github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect github.com/gabriel-vasile/mimetype v1.4.3 // indirect github.com/gin-contrib/sse v0.1.0 // indirect github.com/go-playground/locales v0.14.1 // indirect @@ -30,6 +32,7 @@ require ( github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/reflect2 v1.0.2 // indirect github.com/pelletier/go-toml/v2 v2.1.1 // indirect + github.com/redis/go-redis/v9 v9.5.1 // indirect github.com/rogpeppe/go-internal v1.10.0 // indirect github.com/twitchyliquid64/golang-asm v0.15.1 // indirect github.com/ugorji/go/codec v1.2.12 // indirect diff --git a/go.sum b/go.sum index 54c3d16..63b3c1f 100644 --- a/go.sum +++ b/go.sum @@ -4,6 +4,8 @@ github.com/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1 github.com/bytedance/sonic v1.10.0-rc/go.mod h1:ElCzW+ufi8qKqNW0FY314xriJhyJhuoJ3gFZdAHF7NM= github.com/bytedance/sonic v1.11.2 h1:ywfwo0a/3j9HR8wsYGWsIWl2mvRsI950HyoxiBERw5A= github.com/bytedance/sonic v1.11.2/go.mod h1:iZcSUejdk5aukTND/Eu/ivjQuEL0Cu9/rf50Hi0u/g4= +github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44= +github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/chenzhuoyu/base64x v0.0.0-20211019084208-fb5309c8db06/go.mod h1:DH46F32mSOjUmXrMHnKwZdA8wcEefY7UVqBKYGjpdQY= github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311/go.mod h1:b583jCggY9gE99b6G5LEC39OIiVsWj+R97kbl5odCEk= github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d h1:77cEq6EriyTZ0g/qfRdp61a3Uu/AWrgIq2s0ClJV1g0= @@ -15,6 +17,8 @@ github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ3 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/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78= +github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc= github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0= github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk= github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= @@ -68,6 +72,8 @@ github.com/pelletier/go-toml/v2 v2.1.1/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdU github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA= 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/redis/go-redis/v9 v9.5.1 h1:H1X4D3yHPaYrkL5X06Wh6xNVM/pX0Ft4RV0vMGvLBh8= +github.com/redis/go-redis/v9 v9.5.1/go.mod h1:hdY0cQFCN4fnSYT6TkisLufl/4W5UIXyv0b/CLO2V2M= github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs= github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ= github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog= diff --git a/lib/deploy/handler.go b/lib/deploy/handler.go index 44b650d..dc2c0c0 100644 --- a/lib/deploy/handler.go +++ b/lib/deploy/handler.go @@ -1,11 +1,13 @@ package deploy import ( + "context" "fmt" "github.com/gin-gonic/gin" "github.com/google/uuid" + "github.com/NicolasLopes7/shipthing/config" aws "github.com/NicolasLopes7/shipthing/lib/aws" fs "github.com/NicolasLopes7/shipthing/lib/fs" github "github.com/NicolasLopes7/shipthing/lib/github" @@ -45,6 +47,25 @@ func Handler(ctx *gin.Context) { fs.RemoveLocalRepo(path) + redisCtx := context.Background() + + cmd := config.RedisClient.LPush(redisCtx, "builds", deployId.String()) + if cmd.Err() != nil { + ctx.JSON(500, gin.H{ + "error": cmd.Err().Error(), + }) + return + } + + cmd = config.RedisClient.HSet(redisCtx, "status", deployId.String(), "uploaded") + + if cmd.Err() != nil { + ctx.JSON(500, gin.H{ + "error": cmd.Err().Error(), + }) + return + } + if err != nil { ctx.JSON(500, gin.H{ "error": err.Error(),