fix
This commit is contained in:
		@@ -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 {
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										26
									
								
								main.go
									
									
									
									
									
								
							
							
						
						
									
										26
									
								
								main.go
									
									
									
									
									
								
							@@ -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"
 | 
			
		||||
@@ -58,6 +59,30 @@ func metricProxy(w http.ResponseWriter, r *http.Request) {
 | 
			
		||||
	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)
 | 
			
		||||
@@ -66,5 +91,6 @@ func main() {
 | 
			
		||||
	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