-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollector.go
76 lines (69 loc) · 2.14 KB
/
collector.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package main
import (
"encoding/json"
"github.com/onyxium-strategies/onyxium-strategy-worker/models"
log "github.com/sirupsen/logrus"
"net/http"
"time"
)
type CollectorBody struct {
Name string `json:"name"`
UserId string `json:"userId"`
Tree []interface{} `json:"tree"`
}
// Collects requests from the frontend, and place strategy in the database
func StrategyCreateCollector(w http.ResponseWriter, r *http.Request) {
var collector CollectorBody
err := json.NewDecoder(r.Body).Decode(&collector)
if err != nil {
respondWithError(w, http.StatusNotFound, err.Error())
return
}
binaryTree, err := parseBinaryTree(collector.Tree)
if err != nil {
respondWithError(w, 400, err.Error())
log.Infof("Bad request parseBinaryTree, responded with error msg: %s", err)
return
}
binaryTree.SetIdsForBinarySearch()
strategy, err := models.NewStrategy(collector.Name, collector.UserId, binaryTree)
if err != nil {
respondWithError(w, 400, err.Error())
log.Infof("Bad request StrategyCreate, responded with error msg: %s", err)
}
err = env.DataStore.StrategyCreate(strategy)
if err != nil {
respondWithError(w, 400, err.Error())
}
log.Info("Strategy created")
// And let the user know their work request was created.
payload := map[string]interface{}{
"id": strategy.Id,
"name": strategy.Name,
"status": strategy.Status,
"state": strategy.State,
"createdAt": strategy.CreatedAt.Unix(),
"updatedAt": strategy.UpdatedAt.Unix(),
"tree": strategy.Tree.ToKaryArray(),
}
respondWithJSON(w, http.StatusOK, payload)
}
func IdleStategyCollector() {
// Puts work into the WorkQueue
go func() {
for {
strategies, err := env.DataStore.StrategiesGetIdle()
if len(strategies) > 0 {
log.Info("Dispatching idle strategies")
}
if err != nil {
log.Fatal(err)
}
for _, strategy := range strategies {
log.Infof("strategy: %s", strategy.Id.Hex())
WorkQueue <- strategy
}
time.Sleep(time.Second) // TODO: check if a second has passed instead of waiting one second. Of nog beter is eigenlijk gwn een signaaltje krijgen als er nieuwe data is of wijzigingen in de db
}
}()
}