Skip to content

Commit e026a34

Browse files
committed
refactor: mongo update
1 parent f0eb735 commit e026a34

File tree

4 files changed

+99
-3
lines changed

4 files changed

+99
-3
lines changed

.air.toml

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
root = "."
2+
testdata_dir = "testdata"
3+
tmp_dir = "tmp"
4+
5+
[build]
6+
args_bin = []
7+
bin = "./tmp/main"
8+
cmd = "go build -o ./tmp/main ."
9+
delay = 1000
10+
exclude_dir = ["assets", "tmp", "vendor", "testdata"]
11+
exclude_file = []
12+
exclude_regex = ["_test.go"]
13+
exclude_unchanged = false
14+
follow_symlink = false
15+
full_bin = ""
16+
include_dir = []
17+
include_ext = ["go", "tpl", "tmpl", "html"]
18+
include_file = []
19+
kill_delay = "0s"
20+
log = "build-errors.log"
21+
poll = false
22+
poll_interval = 0
23+
post_cmd = []
24+
pre_cmd = []
25+
rerun = false
26+
rerun_delay = 500
27+
send_interrupt = false
28+
stop_on_error = false
29+
30+
[color]
31+
app = ""
32+
build = "yellow"
33+
main = "magenta"
34+
runner = "green"
35+
watcher = "cyan"
36+
37+
[log]
38+
main_only = false
39+
silent = false
40+
time = false
41+
42+
[misc]
43+
clean_on_exit = false
44+
45+
[proxy]
46+
app_port = 0
47+
enabled = false
48+
proxy_port = 0
49+
50+
[screen]
51+
clear_on_rebuild = false
52+
keep_scroll = true

internal/services/mongo.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"time"
66

7+
"go.mongodb.org/mongo-driver/bson"
78
"go.mongodb.org/mongo-driver/v2/mongo"
89
)
910

@@ -25,6 +26,20 @@ func (s *MongoService) Insert(collection string, item interface{}) (interface{},
2526

2627
}
2728

29+
func (s *MongoService) Update(collection string, filter map[string]any, item map[string]interface{}) (interface{}, error) {
30+
coll := s.Client.Database(s.DatabaseName).Collection(collection)
31+
bsonFilter := bson.M(filter)
32+
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
33+
defer cancel()
34+
35+
result, err := coll.UpdateOne(ctx, bsonFilter, item)
36+
if err != nil {
37+
return nil, err
38+
}
39+
return result.UpsertedID, nil
40+
41+
}
42+
2843
func (s *MongoService) First(collection string, filter interface{}, result interface{}) error {
2944
coll := s.Client.Database(s.DatabaseName).Collection(collection)
3045
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)

internal/services/monitoring/health_check.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,19 @@ func NewHealthChecker(client *http.Client) *HealthChecker {
5252
func (h *HealthChecker) Check(data *HealthData, s *models.Strategy) {
5353
if s.Options.Active && s.HasHealthCheck() {
5454
url, _ := s.HealthCheckUrl()
55-
resp, err := h.client.Get(url)
56-
if err != nil || (resp.StatusCode < 200 || resp.StatusCode > 203) {
55+
resp, _ := h.client.Get(url)
56+
if resp == nil || (resp.StatusCode < 200 || resp.StatusCode > 203) {
5757
data.Update(false)
58+
log.Printf("WARN: Health check error. Error count: %v", data.FailureCount)
5859
} else {
5960
data.Update(true)
6061
}
6162
}
63+
6264
// Max 5 unhealthy checks
6365
if data.FailureCount == 5 {
6466
s.Options.Active = false
67+
log.Printf("INFO: %s has been switched off due to failed healthchecks.\n", s.Name)
6568
}
6669
}
6770

@@ -88,7 +91,13 @@ func (h *HealthChecker) BackgroundProcess(s *services.StrategyService) {
8891
}
8992
h.mu.Unlock()
9093
val := h.healthData[strat.Name]
91-
go h.Check(val, &strat)
94+
// Only launch on active strategies
95+
h.Check(val, &strat)
96+
97+
if !strat.Options.Active {
98+
s.UpdateStrategy(&strat)
99+
100+
}
92101
}
93102
time.Sleep(time.Duration(defaultWait) * time.Second)
94103
}

internal/services/strategy.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package services
22

33
import (
4+
"fmt"
5+
"log"
6+
47
"github.com/syntaxsdev/mercury/models"
58
"go.mongodb.org/mongo-driver/bson"
69
)
@@ -24,6 +27,23 @@ func (s *StrategyService) GetAllStrategies() ([]models.Strategy, error) {
2427
return results, nil
2528
}
2629

30+
// Update Strategy
31+
func (s *StrategyService) UpdateStrategy(strat *models.Strategy) error {
32+
if strat.Name == "" {
33+
log.Println("ERROR: `Name` field does not exist.")
34+
return fmt.Errorf("`Name` does not exist.")
35+
}
36+
37+
filter := bson.M{"name": strat.Name}
38+
update := map[string]interface{}{"$set": strat}
39+
_, err := s.db.Update("strategies", filter, update)
40+
41+
if err != nil {
42+
return fmt.Errorf("ERROR: Failed to update strategy %s: %w", strat.Name, err)
43+
}
44+
return nil
45+
}
46+
2747
// // Create A Strategy
2848
// func CreateStrategy(m *MongoService) (*models.Strategy, error) {
2949
// var newStrategy models.Strategy

0 commit comments

Comments
 (0)