Compare commits
32 Commits
a564621d80
...
prod
| Author | SHA1 | Date | |
|---|---|---|---|
| 54bf7ce86b | |||
|
|
b9c3094d63 | ||
|
|
e94ca96c42 | ||
| 821d60021b | |||
|
|
a3182947a1 | ||
| 1512dd4575 | |||
|
|
357418c66c | ||
|
|
3cdc41f864 | ||
| b899d44148 | |||
|
|
35dcc8390c | ||
|
|
7a99b36b00 | ||
|
|
db88db8b20 | ||
|
|
cc2875563f | ||
|
|
4538137ce4 | ||
| 3f42b1e393 | |||
|
|
6567a1dc35 | ||
|
|
ff040e1a6d | ||
|
|
4d2e0198f3 | ||
| ecc9bef172 | |||
|
|
bc9c8ad85d | ||
| a9f14411ce | |||
|
|
f3c832065a | ||
|
|
2219b4f4df | ||
|
|
911496ffba | ||
|
|
f4e5bc107d | ||
|
|
36e60a03dd | ||
|
|
6d9d3cb1df | ||
|
|
53736302b3 | ||
|
|
bc411ea644 | ||
|
|
64bee3c59f | ||
| a4a3176cb0 | |||
| bf152e6d13 |
@@ -6,16 +6,17 @@ services:
|
|||||||
image: mathwave/sprint-repo:queues
|
image: mathwave/sprint-repo:queues
|
||||||
networks:
|
networks:
|
||||||
- queues-development
|
- queues-development
|
||||||
|
- monitoring
|
||||||
|
- mongo-development
|
||||||
environment:
|
environment:
|
||||||
MONGO_HOST: "mongo.develop.sprinthub.ru"
|
|
||||||
MONGO_PASSWORD: $MONGO_PASSWORD_DEV
|
MONGO_PASSWORD: $MONGO_PASSWORD_DEV
|
||||||
REDIS_HOST: "redis.develop.sprinthub.ru"
|
|
||||||
REDIS_PASSWORD: $REDIS_PASSWORD_DEV
|
|
||||||
STAGE: "development"
|
STAGE: "development"
|
||||||
deploy:
|
deploy:
|
||||||
mode: replicated
|
mode: replicated
|
||||||
restart_policy:
|
restart_policy:
|
||||||
condition: any
|
condition: any
|
||||||
|
placement:
|
||||||
|
constraints: [node.labels.stage == development]
|
||||||
update_config:
|
update_config:
|
||||||
parallelism: 1
|
parallelism: 1
|
||||||
order: start-first
|
order: start-first
|
||||||
@@ -23,3 +24,7 @@ services:
|
|||||||
networks:
|
networks:
|
||||||
queues-development:
|
queues-development:
|
||||||
external: true
|
external: true
|
||||||
|
monitoring:
|
||||||
|
external: true
|
||||||
|
mongo-development:
|
||||||
|
external: true
|
||||||
|
|||||||
@@ -6,16 +6,17 @@ services:
|
|||||||
image: mathwave/sprint-repo:queues
|
image: mathwave/sprint-repo:queues
|
||||||
networks:
|
networks:
|
||||||
- queues
|
- queues
|
||||||
|
- monitoring
|
||||||
|
- mongo
|
||||||
environment:
|
environment:
|
||||||
MONGO_HOST: "mongo.sprinthub.ru"
|
|
||||||
MONGO_PASSWORD: $MONGO_PASSWORD_PROD
|
MONGO_PASSWORD: $MONGO_PASSWORD_PROD
|
||||||
REDIS_HOST: "redis.sprinthub.ru"
|
|
||||||
REDIS_PASSWORD: $REDIS_PASSWORD_PROD
|
|
||||||
STAGE: "production"
|
STAGE: "production"
|
||||||
deploy:
|
deploy:
|
||||||
mode: replicated
|
mode: replicated
|
||||||
restart_policy:
|
restart_policy:
|
||||||
condition: any
|
condition: any
|
||||||
|
placement:
|
||||||
|
constraints: [node.labels.stage == production]
|
||||||
update_config:
|
update_config:
|
||||||
parallelism: 1
|
parallelism: 1
|
||||||
order: start-first
|
order: start-first
|
||||||
@@ -23,3 +24,7 @@ services:
|
|||||||
networks:
|
networks:
|
||||||
queues:
|
queues:
|
||||||
external: true
|
external: true
|
||||||
|
monitoring:
|
||||||
|
external: true
|
||||||
|
mongo:
|
||||||
|
external: true
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ on:
|
|||||||
jobs:
|
jobs:
|
||||||
build:
|
build:
|
||||||
name: Build
|
name: Build
|
||||||
runs-on: [ dev ]
|
runs-on: [dev]
|
||||||
steps:
|
steps:
|
||||||
- name: login
|
- name: login
|
||||||
run: docker login -u mathwave -p ${{ secrets.DOCKERHUB_PASSWORD }}
|
run: docker login -u mathwave -p ${{ secrets.DOCKERHUB_PASSWORD }}
|
||||||
@@ -21,7 +21,7 @@ jobs:
|
|||||||
run: docker build -t mathwave/sprint-repo:queues .
|
run: docker build -t mathwave/sprint-repo:queues .
|
||||||
push:
|
push:
|
||||||
name: Push
|
name: Push
|
||||||
runs-on: [ dev ]
|
runs-on: [dev]
|
||||||
needs: build
|
needs: build
|
||||||
steps:
|
steps:
|
||||||
- name: push
|
- name: push
|
||||||
@@ -41,4 +41,4 @@ jobs:
|
|||||||
env:
|
env:
|
||||||
MONGO_PASSWORD_DEV: ${{ secrets.MONGO_PASSWORD_DEV }}
|
MONGO_PASSWORD_DEV: ${{ secrets.MONGO_PASSWORD_DEV }}
|
||||||
REDIS_PASSWORD_DEV: ${{ secrets.REDIS_PASSWORD_DEV }}
|
REDIS_PASSWORD_DEV: ${{ secrets.REDIS_PASSWORD_DEV }}
|
||||||
run: docker stack deploy --with-registry-auth -c ./.deploy/deploy-dev.yaml infra-development
|
run: docker stack deploy --with-registry-auth -c ./.deploy/deploy-dev.yaml queues-development
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ on:
|
|||||||
jobs:
|
jobs:
|
||||||
build:
|
build:
|
||||||
name: Build
|
name: Build
|
||||||
runs-on: [ dev ]
|
runs-on: [dev]
|
||||||
steps:
|
steps:
|
||||||
- name: login
|
- name: login
|
||||||
run: docker login -u mathwave -p ${{ secrets.DOCKERHUB_PASSWORD }}
|
run: docker login -u mathwave -p ${{ secrets.DOCKERHUB_PASSWORD }}
|
||||||
@@ -21,7 +21,7 @@ jobs:
|
|||||||
run: docker build -t mathwave/sprint-repo:queues .
|
run: docker build -t mathwave/sprint-repo:queues .
|
||||||
push:
|
push:
|
||||||
name: Push
|
name: Push
|
||||||
runs-on: [ dev ]
|
runs-on: [dev]
|
||||||
needs: build
|
needs: build
|
||||||
steps:
|
steps:
|
||||||
- name: push
|
- name: push
|
||||||
@@ -41,4 +41,4 @@ jobs:
|
|||||||
env:
|
env:
|
||||||
MONGO_PASSWORD_PROD: ${{ secrets.MONGO_PASSWORD_PROD }}
|
MONGO_PASSWORD_PROD: ${{ secrets.MONGO_PASSWORD_PROD }}
|
||||||
REDIS_PASSWORD_PROD: ${{ secrets.REDIS_PASSWORD_PROD }}
|
REDIS_PASSWORD_PROD: ${{ secrets.REDIS_PASSWORD_PROD }}
|
||||||
run: docker stack deploy --with-registry-auth -c ./.deploy/deploy-prod.yaml infra
|
run: docker stack deploy --with-registry-auth -c ./.deploy/deploy-prod.yaml queues
|
||||||
|
|||||||
@@ -1,8 +1,15 @@
|
|||||||
package routers
|
package routers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"os"
|
||||||
tasks "queues-go/app/storage/mongo/collections"
|
tasks "queues-go/app/storage/mongo/collections"
|
||||||
|
"strconv"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type TaskResponse struct {
|
type TaskResponse struct {
|
||||||
@@ -15,19 +22,36 @@ type TakeResponse struct {
|
|||||||
Task *TaskResponse `json:"task"`
|
Task *TaskResponse `json:"task"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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) {
|
func Take(r *http.Request) (interface{}, int) {
|
||||||
queue := r.Header.Get("queue")
|
queue := r.Header.Get("queue")
|
||||||
// mutex := redis.Sync.NewMutex(fmt.Sprintf("lock_queues_%s", queue))
|
mutex, ok := MutexMap[queue]
|
||||||
// for {
|
if !ok {
|
||||||
// err := mutex.Lock()
|
mutex = &sync.Mutex{}
|
||||||
// if err != nil {
|
MutexMap[queue] = mutex
|
||||||
// time.Sleep(time.Millisecond * 5)
|
}
|
||||||
// } else {
|
mutex.Lock()
|
||||||
// break
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
task, err := tasks.Take(queue)
|
task, err := tasks.Take(queue)
|
||||||
// mutex.Unlock()
|
mutex.Unlock()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, http.StatusInternalServerError
|
return nil, http.StatusInternalServerError
|
||||||
}
|
}
|
||||||
@@ -41,6 +65,8 @@ func Take(r *http.Request) (interface{}, int) {
|
|||||||
Attempt: task.Attempts,
|
Attempt: task.Attempts,
|
||||||
Payload: task.Payload,
|
Payload: task.Payload,
|
||||||
}
|
}
|
||||||
|
now := time.Now()
|
||||||
|
go sendLatency(now, int(now.Sub(task.PutAt).Milliseconds()))
|
||||||
}
|
}
|
||||||
|
|
||||||
return response, http.StatusOK
|
return response, http.StatusOK
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ import (
|
|||||||
var Database mongo.Database
|
var Database mongo.Database
|
||||||
|
|
||||||
func Connect() {
|
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))
|
client, err := mongo.Connect(context.TODO(), options.Client().ApplyURI(connectionString))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import (
|
|||||||
"go.mongodb.org/mongo-driver/bson"
|
"go.mongodb.org/mongo-driver/bson"
|
||||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||||
"go.mongodb.org/mongo-driver/mongo"
|
"go.mongodb.org/mongo-driver/mongo"
|
||||||
|
"go.mongodb.org/mongo-driver/mongo/options"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Task struct {
|
type Task struct {
|
||||||
@@ -32,6 +33,14 @@ type InsertedTask struct {
|
|||||||
Attempts int `bson:"attempts"`
|
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 {
|
func Add(task InsertedTask) error {
|
||||||
_, err := collection().InsertOne(context.TODO(), task)
|
_, err := collection().InsertOne(context.TODO(), task)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -62,7 +71,19 @@ func Take(queue string) (*Task, error) {
|
|||||||
if task == nil {
|
if task == nil {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
_, err = collection().UpdateByID(context.TODO(), task.Id, bson.M{"$set": bson.M{"taken_at": now, "attempts": task.Attempts + 1}})
|
_, err = collection().UpdateByID(
|
||||||
|
context.TODO(),
|
||||||
|
task.Id,
|
||||||
|
bson.M{
|
||||||
|
"$set": bson.M{
|
||||||
|
"taken_at": now,
|
||||||
|
"attempts": task.Attempts + 1,
|
||||||
|
"available_from": now.Add(
|
||||||
|
time.Duration(task.SecondsToExecute+task.Attempts) * time.Second,
|
||||||
|
),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
println("Error updaing")
|
println("Error updaing")
|
||||||
println(err.Error())
|
println(err.Error())
|
||||||
@@ -78,6 +99,7 @@ func findTask(queue string, now time.Time) (*Task, error) {
|
|||||||
"queue": queue,
|
"queue": queue,
|
||||||
"available_from": bson.M{"$lte": now},
|
"available_from": bson.M{"$lte": now},
|
||||||
},
|
},
|
||||||
|
options.Find().SetSort(bson.D{{Key: "available_from", Value: 1}}).SetLimit(1),
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
println("Error find")
|
println("Error find")
|
||||||
@@ -94,14 +116,7 @@ func findTask(queue string, now time.Time) (*Task, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, task := range results {
|
for _, task := range results {
|
||||||
if task.TakenAt == nil {
|
return &task, nil
|
||||||
return &task, nil
|
|
||||||
}
|
|
||||||
takenAt := *task.TakenAt
|
|
||||||
|
|
||||||
if takenAt.Add(time.Second * time.Duration(task.SecondsToExecute)).Before(now) {
|
|
||||||
return &task, nil
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|||||||
60
main.go
60
main.go
@@ -1,15 +1,39 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"os"
|
||||||
"queues-go/app/routers"
|
"queues-go/app/routers"
|
||||||
client "queues-go/app/storage/mongo"
|
client "queues-go/app/storage/mongo"
|
||||||
"queues-go/app/storage/redis"
|
tasks "queues-go/app/storage/mongo/collections"
|
||||||
|
"strconv"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func writeMetric(timestamp time.Time, endpoint string, statusCode int, responseTime int, method string) {
|
||||||
|
loc, _ := time.LoadLocation("Europe/Moscow")
|
||||||
|
s := fmt.Sprintf(
|
||||||
|
`{"timestamp":"%s","service":"queues","environment":"%s","endpoint":"%s","status_code":%s,"response_time":%s,"method":"%s"}`,
|
||||||
|
timestamp.In(loc).Format("2006-01-02T15:04:05Z"),
|
||||||
|
os.Getenv("STAGE"),
|
||||||
|
endpoint,
|
||||||
|
strconv.Itoa(statusCode),
|
||||||
|
strconv.Itoa(responseTime),
|
||||||
|
method,
|
||||||
|
)
|
||||||
|
data := []byte(s)
|
||||||
|
r := bytes.NewReader(data)
|
||||||
|
_, err := http.Post("http://monitoring:1237/api/v1/metrics/endpoint", "application/json", r)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Error sending metrics %s", err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func handlerWrapper(f func(*http.Request) (interface{}, int)) func(http.ResponseWriter, *http.Request) {
|
func handlerWrapper(f func(*http.Request) (interface{}, int)) func(http.ResponseWriter, *http.Request) {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
start := time.Now()
|
start := time.Now()
|
||||||
@@ -25,16 +49,48 @@ func handlerWrapper(f func(*http.Request) (interface{}, int)) func(http.Response
|
|||||||
} else {
|
} else {
|
||||||
w.WriteHeader(status)
|
w.WriteHeader(status)
|
||||||
}
|
}
|
||||||
|
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))
|
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() {
|
func main() {
|
||||||
client.Connect()
|
client.Connect()
|
||||||
redis.Connect()
|
routers.MutexMap = make(map[string]*sync.Mutex)
|
||||||
http.HandleFunc("/api/v1/take", handlerWrapper(routers.Take))
|
http.HandleFunc("/api/v1/take", handlerWrapper(routers.Take))
|
||||||
http.HandleFunc("/api/v1/finish", handlerWrapper(routers.Finish))
|
http.HandleFunc("/api/v1/finish", handlerWrapper(routers.Finish))
|
||||||
http.HandleFunc("/api/v1/put", handlerWrapper(routers.Put))
|
http.HandleFunc("/api/v1/put", handlerWrapper(routers.Put))
|
||||||
|
http.HandleFunc("/api/v1/metric", metricProxy)
|
||||||
log.Printf("Server started")
|
log.Printf("Server started")
|
||||||
|
go writeCount()
|
||||||
http.ListenAndServe(":1239", nil)
|
http.ListenAndServe(":1239", nil)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user