Skip to content
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

Hw12 13 14 15 calendar #14

Merged
merged 8 commits into from
Sep 16, 2020
Merged
Show file tree
Hide file tree
Changes from 6 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
15 changes: 13 additions & 2 deletions hw12_13_14_15_calendar/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ build:
go build -o ./bin ./...

test:
go test -race ./...
go test -race -v `go list ./... | grep -v integration-tests`

lint:
golangci-lint run ./...
Expand All @@ -19,7 +19,18 @@ run:
make build && make launch

migrate:
./scripts/migrations.sh
goose -dir ./migrations postgres "user=\${DB_USER} password=\${DB_PASSWORD} dbname=\${DB_NAME} sslmode=disable\" up

up:
docker-compose -f deployments/docker-compose.yml up -d

down:
docker-compose -f deployments/docker-compose.yml down

integration-tests:
docker-compose -f deployments/docker-compose.yml up -d --build
ginkgo ./integration-tests
docker-compose -f deployments/docker-compose.yml down

generate:
protoc \
Expand Down
4 changes: 1 addition & 3 deletions hw12_13_14_15_calendar/cmd/calendar_scheduler/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package main
import (
"context"
"flag"
"fmt"
"strconv"

"github.com/dmitryt/otus-golang-hw/hw12_13_14_15_calendar/internal/config"
"github.com/dmitryt/otus-golang-hw/hw12_13_14_15_calendar/internal/logger"
Expand Down Expand Up @@ -49,7 +47,7 @@ func main() {
qCfg := cfg.QueueConfig

producer := queue.NewProducer(
fmt.Sprintf("amqp://%s:%s@%s:%s/", qCfg.User, qCfg.Pass, qCfg.Host, strconv.Itoa(qCfg.Port)),
qCfg.URI,
qCfg.QueueName,
qCfg.ExchangeType,
qCfg.MaxReconnectAttempts,
Expand Down
21 changes: 16 additions & 5 deletions hw12_13_14_15_calendar/cmd/calendar_sender/main.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package main

import (
"context"
"flag"
"fmt"
"strconv"

"github.com/dmitryt/otus-golang-hw/hw12_13_14_15_calendar/internal/config"
"github.com/dmitryt/otus-golang-hw/hw12_13_14_15_calendar/internal/logger"
"github.com/dmitryt/otus-golang-hw/hw12_13_14_15_calendar/internal/queue"
"github.com/dmitryt/otus-golang-hw/hw12_13_14_15_calendar/internal/repository"
"github.com/dmitryt/otus-golang-hw/hw12_13_14_15_calendar/internal/sender"
"github.com/rs/zerolog/log"
)
Expand All @@ -24,6 +24,8 @@ func init() {

func main() {
flag.Parse()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

cfg, err := config.NewSender(cfgPath)
if err != nil {
Expand All @@ -34,17 +36,26 @@ func main() {
fatal(err)
}

qCfg := cfg.QueueConfig
repo := repository.NewStats(cfg.DBConfig.RepoType, cfg.DBConfig.ItemsPerQuery, cfg.DBConfig.MaxConn)
if repo == nil {
fatal(repository.ErrUnSupportedRepoType)
}

if err = repo.Connect(ctx, repository.GetSQLDSN(&cfg.DBConfig)); err != nil {
fatal(err)
}
defer repo.Close()

qCfg := cfg.QueueConfig
consumer := queue.NewConsumer(
fmt.Sprintf("amqp://%s:%s@%s:%s/", qCfg.User, qCfg.Pass, qCfg.Host, strconv.Itoa(qCfg.Port)),
qCfg.URI,
qCfg.QueueName,
qCfg.ExchangeType,
qCfg.QosPrefetchCount,
qCfg.MaxReconnectAttempts,
qCfg.ReconnectTimeoutMs,
)
app := sender.New(consumer, qCfg.ScanTimeoutMs)
app := sender.New(repo, consumer, qCfg.ScanTimeoutMs)
if err := app.Run(); err != nil {
fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion hw12_13_14_15_calendar/configs/calendar.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ dbConfig:
port: 5432
dbName: "db_calendar"
user: "db_calendar_user"
pass: "test"
password: "test"
itemsPerQuery: 50
repoType: "psql"

Expand Down
5 changes: 1 addition & 4 deletions hw12_13_14_15_calendar/configs/sender.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ logConfig:
filePath: "calendar_sender.log"

queueConfig:
user: "guest"
pass: "guest"
host: "localhost"
port: 5672
uri: "amqp://guest:guest@localhost:5672/"
queueName: "events"
scanTimeout: 10
31 changes: 31 additions & 0 deletions hw12_13_14_15_calendar/deployments/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
FROM golang:1.14-alpine AS builder

RUN apk update

# Set necessary environmet variables needed for our image
ENV GO111MODULE=on \
CGO_ENABLED=0 \
GOOS=linux

WORKDIR /build

COPY go.mod go.sum ./
RUN go mod download

# Copy the code into the container
COPY . .

ARG MAIN_FILE_PATH

# Build the application
RUN go build -o main $MAIN_FILE_PATH
RUN chmod +x main

# Build a small image
FROM scratch

COPY --from=builder /build/main /
EXPOSE 8081

# Command to run
ENTRYPOINT ["/main"]
2 changes: 2 additions & 0 deletions hw12_13_14_15_calendar/deployments/api.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
GRPC_ADDRESS=0.0.0.0:50051
HTTP_ADDRESS=0.0.0.0:50052
9 changes: 9 additions & 0 deletions hw12_13_14_15_calendar/deployments/database.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
DB_HOST=db
DB_PORT=5432
DB_USER=db_calendar_user
DB_PASSWORD=test
DB_NAME=db_calendar
DB_REPO_TYPE=psql
DB_ITEMS_PER_QUERY=50
INIT_ON_START=true
DB_MIGRATIONS_DIR=./migrations
10 changes: 10 additions & 0 deletions hw12_13_14_15_calendar/deployments/docker-compose.prod.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
version: "2.4"

services:
db:
volumes:
- pg_volume:/var/lib/postgresql/data

volumes:
pg_volume:
driver: local
85 changes: 85 additions & 0 deletions hw12_13_14_15_calendar/deployments/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
version: "2.4"
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Used this version of docker-compose to have an ability to use healthcheck together with depends_on


services:
db:
image: postgres:12
environment:
- POSTGRES_USER=db_calendar_user
- POSTGRES_PASSWORD=test
- POSTGRES_DB=db_calendar
ports:
- "5432:5432"
healthcheck:
test: ["CMD", "pg_isready", "-U", "postgres"]
interval: 10s
timeout: 10s
retries: 3
start_period: 20s
queue:
build:
context: ./rabbitmq
environment:
- RABBITMQ_DEFAULT_USER=guest
- RABBITMQ_DEFAULT_PASS=guest
healthcheck:
test: [ "CMD", "nc", "-z", "localhost", "5672" ]
interval: 10s
timeout: 10s
retries: 3
start_period: 20s
ports:
- "5672:5672"
- "15672:15672"
calendar:
build:
context: ../
dockerfile: ./deployments/Dockerfile
args:
- MAIN_FILE_PATH=cmd/calendar/main.go
env_file:
- database.env
- logger.env
- api.env
environment:
- PORT=8081
volumes:
- ../migrations:/migrations
ports:
- "8888:50052"
depends_on:
db:
condition: service_healthy
calendar_scheduler:
build:
context: ../
dockerfile: ./deployments/Dockerfile
args:
- MAIN_FILE_PATH=cmd/calendar_scheduler/main.go
env_file:
- database.env
- logger.env
- queue.env
volumes:
- ../migrations:/migrations
depends_on:
db:
condition: service_healthy
queue:
condition: service_healthy
calendar_sender:
build:
context: ../
dockerfile: ./deployments/Dockerfile
args:
- MAIN_FILE_PATH=cmd/calendar_sender/main.go
env_file:
- database.env
- logger.env
- queue.env
volumes:
- ../migrations:/migrations
depends_on:
db:
condition: service_healthy
queue:
condition: service_healthy
2 changes: 2 additions & 0 deletions hw12_13_14_15_calendar/deployments/logger.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
LOG_LEVEL=debug
LOG_FILE=calendar.log
3 changes: 3 additions & 0 deletions hw12_13_14_15_calendar/deployments/queue.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
QUEUE_URI=amqp://guest:guest@queue:5672/
QUEUE_NAME=events
QUEUE_SCAN_TIMEOUT_MS=10000
3 changes: 3 additions & 0 deletions hw12_13_14_15_calendar/deployments/rabbitmq/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
FROM rabbitmq:3-management
RUN apt-get update
RUN apt-get install -y netcat
9 changes: 8 additions & 1 deletion hw12_13_14_15_calendar/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,24 @@ require (
github.com/golang/protobuf v1.4.2
github.com/grpc-ecosystem/go-grpc-middleware v1.2.1
github.com/grpc-ecosystem/grpc-gateway v1.14.7
github.com/heetch/confita v0.9.2
github.com/ilyakaznacheev/cleanenv v1.2.5
github.com/imdario/mergo v0.3.11
github.com/jmoiron/sqlx v1.2.0
github.com/kr/text v0.2.0 // indirect
github.com/lib/pq v1.8.0
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
github.com/onsi/ginkgo v1.14.1
github.com/onsi/gomega v1.10.1
github.com/pkg/errors v0.9.1 // indirect
github.com/pressly/goose v2.6.0+incompatible
github.com/rs/zerolog v1.15.0
github.com/streadway/amqp v1.0.0
github.com/stretchr/testify v1.6.1
golang.org/x/sys v0.0.0-20200909081042-eff7692f9009 // indirect
golang.org/x/text v0.3.3 // indirect
google.golang.org/appengine v1.6.1 // indirect
google.golang.org/genproto v0.0.0-20200815001618-f69a88009b70
google.golang.org/grpc v1.31.0
google.golang.org/protobuf v1.25.0
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect
)
Loading