Compare commits
19 Commits
prod
...
15563aa75d
| Author | SHA1 | Date | |
|---|---|---|---|
| 15563aa75d | |||
| 56add999c6 | |||
| d99d766621 | |||
| 261c850077 | |||
| 04c384add9 | |||
| 41590ee88f | |||
| ad1a0aab30 | |||
| 7a3e65d109 | |||
| 71db409b15 | |||
| 153943b3ed | |||
| b17ed7e7a9 | |||
| a564621d80 | |||
| 59869821f3 | |||
| d9dcfc1a3c | |||
| 932c9b609e | |||
| 71e40b932c | |||
| 39829a7348 | |||
| 9d9c11cf11 | |||
| 75a59c3e5c |
@@ -7,8 +7,8 @@ services:
|
||||
networks:
|
||||
- queues-development
|
||||
- monitoring
|
||||
- mongo-development
|
||||
environment:
|
||||
MONGO_HOST: "mongo.develop.sprinthub.ru"
|
||||
MONGO_PASSWORD: $MONGO_PASSWORD_DEV
|
||||
STAGE: "development"
|
||||
deploy:
|
||||
@@ -26,5 +26,3 @@ networks:
|
||||
external: true
|
||||
monitoring:
|
||||
external: true
|
||||
mongo-development:
|
||||
external: true
|
||||
|
||||
@@ -7,8 +7,8 @@ services:
|
||||
networks:
|
||||
- queues
|
||||
- monitoring
|
||||
- mongo
|
||||
environment:
|
||||
MONGO_HOST: "mongo.sprinthub.ru"
|
||||
MONGO_PASSWORD: $MONGO_PASSWORD_PROD
|
||||
STAGE: "production"
|
||||
deploy:
|
||||
@@ -26,5 +26,3 @@ networks:
|
||||
external: true
|
||||
monitoring:
|
||||
external: true
|
||||
mongo:
|
||||
external: true
|
||||
|
||||
@@ -1,15 +1,9 @@
|
||||
package routers
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
tasks "queues-go/app/storage/mongo/collections"
|
||||
"strconv"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
type TaskResponse struct {
|
||||
@@ -24,24 +18,6 @@ type TakeResponse struct {
|
||||
|
||||
var MutexMap map[string]*sync.Mutex
|
||||
|
||||
func sendLatency(timestamp time.Time, latency int) error {
|
||||
loc, _ := time.LoadLocation("Europe/Moscow")
|
||||
|
||||
s := fmt.Sprintf(
|
||||
`{"timestamp":"%s","service":"queues","environment":"%s","name":"latency","count":%s}`,
|
||||
timestamp.In(loc).Format("2006-01-02T15:04:05Z"),
|
||||
os.Getenv("STAGE"),
|
||||
strconv.Itoa(latency),
|
||||
)
|
||||
data := []byte(s)
|
||||
r := bytes.NewReader(data)
|
||||
_, err := http.Post("http://monitoring:1237/api/v1/metrics/increment", "application/json", r)
|
||||
if err != nil {
|
||||
log.Printf("ERROR %s", err.Error())
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func Take(r *http.Request) (interface{}, int) {
|
||||
queue := r.Header.Get("queue")
|
||||
mutex, ok := MutexMap[queue]
|
||||
@@ -65,8 +41,6 @@ func Take(r *http.Request) (interface{}, int) {
|
||||
Attempt: task.Attempts,
|
||||
Payload: task.Payload,
|
||||
}
|
||||
now := time.Now()
|
||||
go sendLatency(now, int(now.Sub(task.PutAt).Milliseconds()))
|
||||
}
|
||||
|
||||
return response, http.StatusOK
|
||||
|
||||
@@ -12,7 +12,7 @@ import (
|
||||
var Database mongo.Database
|
||||
|
||||
func Connect() {
|
||||
connectionString := fmt.Sprintf("mongodb://mongo:%s@mongo:27017/", utils.GetEnv("MONGO_PASSWORD", "password"))
|
||||
connectionString := fmt.Sprintf("mongodb://mongo:%s@%s:27017/", utils.GetEnv("MONGO_PASSWORD", "password"), utils.GetEnv("MONGO_HOST", "localhost"))
|
||||
client, err := mongo.Connect(context.TODO(), options.Client().ApplyURI(connectionString))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
|
||||
@@ -33,14 +33,6 @@ type InsertedTask struct {
|
||||
Attempts int `bson:"attempts"`
|
||||
}
|
||||
|
||||
func Count() (*int64, error) {
|
||||
count, err := collection().CountDocuments(context.TODO(), bson.M{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &count, nil
|
||||
}
|
||||
|
||||
func Add(task InsertedTask) error {
|
||||
_, err := collection().InsertOne(context.TODO(), task)
|
||||
if err != nil {
|
||||
|
||||
32
main.go
32
main.go
@@ -9,7 +9,6 @@ import (
|
||||
"os"
|
||||
"queues-go/app/routers"
|
||||
client "queues-go/app/storage/mongo"
|
||||
tasks "queues-go/app/storage/mongo/collections"
|
||||
"strconv"
|
||||
"sync"
|
||||
"time"
|
||||
@@ -54,43 +53,12 @@ func handlerWrapper(f func(*http.Request) (interface{}, int)) func(http.Response
|
||||
}
|
||||
}
|
||||
|
||||
func metricProxy(w http.ResponseWriter, r *http.Request) {
|
||||
http.Post("http://monitoring:1237/api/v1/metrics/task", "application/json", r.Body)
|
||||
w.WriteHeader(202)
|
||||
}
|
||||
|
||||
func writeCount() {
|
||||
for {
|
||||
count, err := tasks.Count()
|
||||
if err != nil {
|
||||
log.Printf("Failed getting docs count: %s", err.Error())
|
||||
} else {
|
||||
loc, _ := time.LoadLocation("Europe/Moscow")
|
||||
s := fmt.Sprintf(
|
||||
`{"timestamp":"%s","service":"queues","environment":"%s","name":"tasks","count":%s}`,
|
||||
time.Now().In(loc).Format("2006-01-02T15:04:05Z"),
|
||||
os.Getenv("STAGE"),
|
||||
strconv.Itoa(int(*count)),
|
||||
)
|
||||
data := []byte(s)
|
||||
r := bytes.NewReader(data)
|
||||
_, err := http.Post("http://monitoring:1237/api/v1/metrics/increment", "application/json", r)
|
||||
if err != nil {
|
||||
log.Printf("ERROR %s", err.Error())
|
||||
}
|
||||
}
|
||||
time.Sleep(time.Second)
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
client.Connect()
|
||||
routers.MutexMap = make(map[string]*sync.Mutex)
|
||||
http.HandleFunc("/api/v1/take", handlerWrapper(routers.Take))
|
||||
http.HandleFunc("/api/v1/finish", handlerWrapper(routers.Finish))
|
||||
http.HandleFunc("/api/v1/put", handlerWrapper(routers.Put))
|
||||
http.HandleFunc("/api/v1/metric", metricProxy)
|
||||
log.Printf("Server started")
|
||||
go writeCount()
|
||||
http.ListenAndServe(":1239", nil)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user