Compare commits
24 Commits
41590ee88f
...
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 | ||
| a4a3176cb0 | |||
| bf152e6d13 |
@@ -6,13 +6,17 @@ services:
|
||||
image: mathwave/sprint-repo:queues
|
||||
networks:
|
||||
- queues-development
|
||||
- monitoring
|
||||
- mongo-development
|
||||
environment:
|
||||
MONGO_HOST: "mongo.develop.sprinthub.ru"
|
||||
MONGO_PASSWORD: $MONGO_PASSWORD_DEV
|
||||
STAGE: "development"
|
||||
deploy:
|
||||
mode: replicated
|
||||
restart_policy:
|
||||
condition: any
|
||||
placement:
|
||||
constraints: [node.labels.stage == development]
|
||||
update_config:
|
||||
parallelism: 1
|
||||
order: start-first
|
||||
@@ -20,3 +24,7 @@ services:
|
||||
networks:
|
||||
queues-development:
|
||||
external: true
|
||||
monitoring:
|
||||
external: true
|
||||
mongo-development:
|
||||
external: true
|
||||
|
||||
@@ -6,13 +6,17 @@ services:
|
||||
image: mathwave/sprint-repo:queues
|
||||
networks:
|
||||
- queues
|
||||
- monitoring
|
||||
- mongo
|
||||
environment:
|
||||
MONGO_HOST: "mongo.sprinthub.ru"
|
||||
MONGO_PASSWORD: $MONGO_PASSWORD_PROD
|
||||
STAGE: "production"
|
||||
deploy:
|
||||
mode: replicated
|
||||
restart_policy:
|
||||
condition: any
|
||||
placement:
|
||||
constraints: [node.labels.stage == production]
|
||||
update_config:
|
||||
parallelism: 1
|
||||
order: start-first
|
||||
@@ -20,3 +24,7 @@ services:
|
||||
networks:
|
||||
queues:
|
||||
external: true
|
||||
monitoring:
|
||||
external: true
|
||||
mongo:
|
||||
external: true
|
||||
|
||||
@@ -9,7 +9,7 @@ on:
|
||||
jobs:
|
||||
build:
|
||||
name: Build
|
||||
runs-on: [ dev ]
|
||||
runs-on: [dev]
|
||||
steps:
|
||||
- name: login
|
||||
run: docker login -u mathwave -p ${{ secrets.DOCKERHUB_PASSWORD }}
|
||||
@@ -21,18 +21,11 @@ jobs:
|
||||
run: docker build -t mathwave/sprint-repo:queues .
|
||||
push:
|
||||
name: Push
|
||||
runs-on: [ dev ]
|
||||
runs-on: [dev]
|
||||
needs: build
|
||||
steps:
|
||||
- name: push
|
||||
run: docker push mathwave/sprint-repo:queues
|
||||
create_dir:
|
||||
name: Create dir
|
||||
runs-on: [ dev ]
|
||||
needs: build
|
||||
steps:
|
||||
- name: create_dir
|
||||
run: mkdir /sprint-data/queues-mongo || true
|
||||
deploy-dev:
|
||||
name: Deploy dev
|
||||
runs-on: [prod]
|
||||
|
||||
@@ -9,7 +9,7 @@ on:
|
||||
jobs:
|
||||
build:
|
||||
name: Build
|
||||
runs-on: [ dev ]
|
||||
runs-on: [dev]
|
||||
steps:
|
||||
- name: login
|
||||
run: docker login -u mathwave -p ${{ secrets.DOCKERHUB_PASSWORD }}
|
||||
@@ -21,18 +21,11 @@ jobs:
|
||||
run: docker build -t mathwave/sprint-repo:queues .
|
||||
push:
|
||||
name: Push
|
||||
runs-on: [ dev ]
|
||||
runs-on: [dev]
|
||||
needs: build
|
||||
steps:
|
||||
- name: push
|
||||
run: docker push mathwave/sprint-repo:queues
|
||||
create_dir:
|
||||
name: Create dir
|
||||
runs-on: [ prod ]
|
||||
needs: build
|
||||
steps:
|
||||
- name: create_dir
|
||||
run: mkdir /sprint-data/queues-mongo || true
|
||||
deploy-prod:
|
||||
name: Deploy prod
|
||||
runs-on: [prod]
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
)
|
||||
|
||||
type Task struct {
|
||||
@@ -32,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 {
|
||||
@@ -62,7 +71,19 @@ func Take(queue string) (*Task, error) {
|
||||
if task == 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 {
|
||||
println("Error updaing")
|
||||
println(err.Error())
|
||||
@@ -78,6 +99,7 @@ func findTask(queue string, now time.Time) (*Task, error) {
|
||||
"queue": queue,
|
||||
"available_from": bson.M{"$lte": now},
|
||||
},
|
||||
options.Find().SetSort(bson.D{{Key: "available_from", Value: 1}}).SetLimit(1),
|
||||
)
|
||||
if err != nil {
|
||||
println("Error find")
|
||||
@@ -94,14 +116,7 @@ func findTask(queue string, now time.Time) (*Task, error) {
|
||||
}
|
||||
|
||||
for _, task := range results {
|
||||
if task.TakenAt == nil {
|
||||
return &task, nil
|
||||
}
|
||||
takenAt := *task.TakenAt
|
||||
|
||||
if takenAt.Add(time.Second * time.Duration(task.SecondsToExecute)).Before(now) {
|
||||
return &task, nil
|
||||
}
|
||||
return &task, nil
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
56
main.go
56
main.go
@@ -1,15 +1,39 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"queues-go/app/routers"
|
||||
client "queues-go/app/storage/mongo"
|
||||
tasks "queues-go/app/storage/mongo/collections"
|
||||
"strconv"
|
||||
"sync"
|
||||
"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) {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
start := time.Now()
|
||||
@@ -25,16 +49,48 @@ 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)
|
||||
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() {
|
||||
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