diff --git a/data/in_memory_repository.go b/data/in_memory_repository.go index 28ad4c5..e597403 100644 --- a/data/in_memory_repository.go +++ b/data/in_memory_repository.go @@ -30,6 +30,7 @@ const ( // uisng go-membdb instead of postgres. type InMemoryRepository struct { db *memdb.MemDB + config *config.Config } // NewInMemoryDB is the InMemoryRepository factory method. It fulfills the same @@ -44,40 +45,42 @@ func NewInMemoryDB(config *config.Config) (Repository, error) { return &InMemoryRepository{}, err } - repository := &InMemoryRepository{db} + repository := &InMemoryRepository{db, config} - config.Logger.Debug("Loading Ingredients") + repository.config.Logger.Debug("Loading Ingredients") err = repository.loadIngredients() if err != nil { - config.Logger.Debug(fmt.Sprintf("Failed to load ingredients with err %+v", err)) + repository.config.Logger.Debug(fmt.Sprintf("Failed to load ingredients with err %+v", err)) return &InMemoryRepository{}, err } - config.Logger.Debug("Loading coffees") + repository.config.Logger.Debug("Loading coffees") err = repository.loadCoffees() if err != nil { - config.Logger.Debug(fmt.Sprintf("Failed to load coffees with err %+v", err)) + repository.config.Logger.Debug(fmt.Sprintf("Failed to load coffees with err %+v", err)) return &InMemoryRepository{}, err } - config.Logger.Debug("Loading coffee ingredients") + repository.config.Logger.Debug("Loading coffee ingredients") err = repository.loadCoffeeIngredients() if err != nil { - config.Logger.Debug(fmt.Sprintf("Failed to load coffee ingredients with err %+v", err)) + repository.config.Logger.Debug(fmt.Sprintf("Failed to load coffee ingredients with err %+v", err)) return &InMemoryRepository{}, err } - config.Logger.Debug("Data loaded") + repository.config.Logger.Debug("Data loaded") return repository, nil } // Find returns all coffees from the database // Used to accept ctx opentracing.SpanContext func (r *InMemoryRepository) Find() (entities.Coffees, error) { - txn := r.db.Txn(true) + txn := r.db.Txn(false) + defer txn.Abort() iter, err := txn.Get(Coffee.String(), "id") if err != nil { + r.config.Logger.Error("coffee-service.data.InMemoryRepository.Find failed to load coffees", err) return nil, err } @@ -92,6 +95,7 @@ func (r *InMemoryRepository) Find() (entities.Coffees, error) { innerIter, err := txn.Get(CoffeeIngredient.String(), "id") if err != nil { + r.config.Logger.Error("coffee-service.data.InMemoryRepository.Find failed to load ingredients", err) return nil, err } diff --git a/debug.sh b/debug.sh new file mode 100644 index 0000000..0e01128 --- /dev/null +++ b/debug.sh @@ -0,0 +1,14 @@ +# Assumes kind installed locally +kind delete cluster --name coffee-service-debug + +make build_linux + +docker build -t coffee-service:debug . + +kind create cluster --name coffee-service-debug + +kind load docker-image coffee-service:debug --name coffee-service-debug + +kubectl apply -f ./deployments/debug/coffee-service.yaml + +kubectl get pods \ No newline at end of file diff --git a/deployments/debug/coffee-service.yaml b/deployments/debug/coffee-service.yaml new file mode 100644 index 0000000..ef8af77 --- /dev/null +++ b/deployments/debug/coffee-service.yaml @@ -0,0 +1,68 @@ +apiVersion: v1 +kind: Service +metadata: + name: coffee-service +spec: + selector: + app: coffee-service + ports: + - name: http + protocol: TCP + port: 9090 + targetPort: 9090 +--- +apiVersion: v1 +kind: ServiceAccount +metadata: + name: coffee-service +automountServiceAccountToken: true +--- +apiVersion: apps/v1 +kind: Deployment +metadata: + name: coffee-service + labels: + app: coffee-service + version: v3 +spec: + replicas: 1 + selector: + matchLabels: + app: coffee-service + template: + metadata: + labels: + app: coffee-service + annotations: + prometheus.io/scrape: "true" + prometheus.io/port: "9102" + consul.hashicorp.com/connect-inject: "true" + consul.hashicorp.com/service-meta-version: "v1" + consul.hashicorp.com/service-tags: "api" + spec: + serviceAccountName: coffee-service + containers: + - name: coffee-service + image: coffee-service:debug + ports: + - containerPort: 9090 + - containerPort: 9102 + env: + - name: "LOG_FORMAT" + value: "text" + - name: "LOG_LEVEL" + value: "DEBUG" + - name: "BIND_ADDRESS" + value: "localhost:9090" + - name: "METRICS_ADDRESS" + value: "localhost:9102" + - name: "VERSION" + value: "v3" + livenessProbe: + httpGet: + path: /health + port: 9090 + initialDelaySeconds: 15 + timeoutSeconds: 1 + periodSeconds: 10 + failureThreshold: 30 diff --git a/deployments/v3/coffee-service.yaml b/deployments/v3/coffee-service.yaml index 550d65f..935ff8a 100644 --- a/deployments/v3/coffee-service.yaml +++ b/deployments/v3/coffee-service.yaml @@ -53,9 +53,9 @@ spec: - name: "LOG_LEVEL" value: "INFO" - name: "BIND_ADDRESS" - value: "127.0.0.1:9090" + value: "localhost:9090" - name: "METRICS_ADDRESS" - value: "127.0.0.1:9102" + value: "localhost:9102" - name: "VERSION" value: "v3" livenessProbe: