Compare commits

..

16 Commits

Author SHA1 Message Date
54bf7ce86b Merge pull request 'master' (#38) from master into prod
Reviewed-on: #38
2025-09-15 01:05:34 +03:00
Egor Matveev
b9c3094d63 fix
All checks were successful
Deploy Prod / Build (pull_request) Successful in 49s
Deploy Prod / Push (pull_request) Successful in 29s
Deploy Prod / Deploy prod (pull_request) Successful in 12s
2025-09-15 01:05:01 +03:00
Egor Matveev
e94ca96c42 fix
All checks were successful
Deploy Dev / Build (pull_request) Successful in 1m2s
Deploy Dev / Push (pull_request) Successful in 28s
Deploy Dev / Deploy dev (pull_request) Successful in 11s
2025-09-15 00:50:51 +03:00
821d60021b Merge pull request 'fix' (#36) from master into prod
Reviewed-on: #36
2025-07-17 23:13:27 +03:00
Egor Matveev
a3182947a1 fix
All checks were successful
Deploy Prod / Build (pull_request) Successful in 1m8s
Deploy Prod / Push (pull_request) Successful in 27s
Deploy Prod / Deploy prod (pull_request) Successful in 9s
2025-07-17 23:12:43 +03:00
1512dd4575 Merge pull request 'master' (#35) from master into prod
Reviewed-on: #35
2025-06-15 23:07:32 +03:00
Egor Matveev
357418c66c fix
All checks were successful
Deploy Prod / Deploy prod (pull_request) Successful in 11s
Deploy Dev / Build (pull_request) Successful in 40s
Deploy Dev / Push (pull_request) Successful in 34s
Deploy Dev / Deploy dev (pull_request) Successful in 13s
Deploy Prod / Build (pull_request) Successful in 41s
Deploy Prod / Push (pull_request) Successful in 26s
2025-06-15 23:00:45 +03:00
Egor Matveev
3cdc41f864 fix
All checks were successful
Deploy Dev / Build (pull_request) Successful in 40s
Deploy Dev / Push (pull_request) Successful in 26s
Deploy Dev / Deploy dev (pull_request) Successful in 12s
2025-06-15 22:51:06 +03:00
b899d44148 Merge pull request 'master' (#32) from master into prod
Reviewed-on: #32
2025-06-15 17:20:26 +03:00
Egor Matveev
35dcc8390c fix
All checks were successful
Deploy Dev / Build (pull_request) Successful in 41s
Deploy Dev / Push (pull_request) Successful in 27s
Deploy Dev / Deploy dev (pull_request) Successful in 14s
Deploy Prod / Build (pull_request) Successful in 42s
Deploy Prod / Push (pull_request) Successful in 27s
Deploy Prod / Deploy prod (pull_request) Successful in 10s
2025-06-15 17:05:18 +03:00
Egor Matveev
7a99b36b00 fix
All checks were successful
Deploy Dev / Build (pull_request) Successful in 48s
Deploy Dev / Push (pull_request) Successful in 27s
Deploy Dev / Deploy dev (pull_request) Successful in 10s
2025-06-15 16:35:32 +03:00
3f42b1e393 Merge pull request 'master' (#26) from master into prod
Reviewed-on: #26
2025-06-15 01:37:16 +03:00
ecc9bef172 Merge pull request 'fix' (#21) from master into prod
Reviewed-on: #21
2025-01-13 13:47:26 +03:00
a9f14411ce Merge pull request 'master' (#19) from master into prod
Reviewed-on: #19
2025-01-07 02:30:43 +03:00
a4a3176cb0 Merge pull request 'master' (#8) from master into prod
Reviewed-on: #8
2025-01-01 14:26:00 +03:00
bf152e6d13 Merge pull request 'master' (#6) from master into prod
Reviewed-on: #6
2024-12-31 03:04:59 +03:00
6 changed files with 74 additions and 6 deletions

View File

@@ -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,3 +26,5 @@ networks:
external: true
monitoring:
external: true
mongo-development:
external: true

View File

@@ -7,8 +7,8 @@ services:
networks:
- queues
- monitoring
- mongo
environment:
MONGO_HOST: "mongo.sprinthub.ru"
MONGO_PASSWORD: $MONGO_PASSWORD_PROD
STAGE: "production"
deploy:
@@ -26,3 +26,5 @@ networks:
external: true
monitoring:
external: true
mongo:
external: true

View File

@@ -1,9 +1,15 @@
package routers
import (
"bytes"
"fmt"
"log"
"net/http"
"os"
tasks "queues-go/app/storage/mongo/collections"
"strconv"
"sync"
"time"
)
type TaskResponse struct {
@@ -18,6 +24,24 @@ 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]
@@ -41,6 +65,8 @@ 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

View File

@@ -12,7 +12,7 @@ import (
var Database mongo.Database
func Connect() {
connectionString := fmt.Sprintf("mongodb://mongo:%s@%s:27017/", utils.GetEnv("MONGO_PASSWORD", "password"), utils.GetEnv("MONGO_HOST", "localhost"))
connectionString := fmt.Sprintf("mongodb://mongo:%s@mongo:27017/", utils.GetEnv("MONGO_PASSWORD", "password"))
client, err := mongo.Connect(context.TODO(), options.Client().ApplyURI(connectionString))
if err != nil {
panic(err)

View File

@@ -33,6 +33,14 @@ 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 {

34
main.go
View File

@@ -9,6 +9,7 @@ import (
"os"
"queues-go/app/routers"
client "queues-go/app/storage/mongo"
tasks "queues-go/app/storage/mongo/collections"
"strconv"
"sync"
"time"
@@ -48,11 +49,38 @@ func handlerWrapper(f func(*http.Request) (interface{}, int)) func(http.Response
} else {
w.WriteHeader(status)
}
// go writeMetric(start, r.URL.Path, status, int(time.Since(start).Milliseconds()), r.Method)
if r.URL.Path != "/api/v1/take" {
go writeMetric(start, r.URL.Path, status, int(time.Since(start).Milliseconds()), r.Method)
log.Printf("%s %d %s", r.URL, status, time.Since(start))
}
}
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() {
@@ -61,6 +89,8 @@ func main() {
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)
}