mirror of https://github.com/curusarn/resh
parent
12f106dcb7
commit
414a42f767
@ -1,49 +1,77 @@ |
|||||||
package syncconnector |
package syncconnector |
||||||
|
|
||||||
import ( |
import ( |
||||||
"github.com/curusarn/resh/internal/recordint" |
"bytes" |
||||||
|
"encoding/json" |
||||||
|
"github.com/curusarn/resh/record" |
||||||
|
"io" |
||||||
|
"net/http" |
||||||
|
"strconv" |
||||||
|
"time" |
||||||
) |
) |
||||||
|
|
||||||
func (sc SyncConnector) write(collect chan recordint.Collect) { |
func (sc SyncConnector) write() error { |
||||||
//for {
|
latestRemote, err := sc.latest() |
||||||
// func() {
|
if err != nil { |
||||||
// select {
|
return err |
||||||
// case rec := <-collect:
|
} |
||||||
// part := "2"
|
latestLocal := sc.history.LatestRecordsPerDevice() |
||||||
// if rec.Rec.PartOne {
|
remoteIsOlder := false |
||||||
// part = "1"
|
for deviceId, lastLocal := range latestLocal { |
||||||
// }
|
if lastRemote, ok := latestRemote[deviceId]; !ok { |
||||||
// sugar := h.sugar.With(
|
// Unknown deviceId on the remote - add records have to be sent
|
||||||
// "recordCmdLine", rec.Rec.CmdLine,
|
remoteIsOlder = true |
||||||
// "recordPart", part,
|
break |
||||||
// "recordShell", rec.Shell,
|
} else if lastLocal > lastRemote { |
||||||
// )
|
remoteIsOlder = true |
||||||
// sc.sugar.Debugw("Got record")
|
break |
||||||
// h.sessionsMutex.Lock()
|
} |
||||||
// defer h.sessionsMutex.Unlock()
|
} |
||||||
//
|
if !remoteIsOlder { |
||||||
// // allows nested sessions to merge records properly
|
sc.sugar.Debug("No need to sync remote, there are no newer local records") |
||||||
// mergeID := rec.SessionID + "_" + strconv.Itoa(rec.Shlvl)
|
return nil |
||||||
// sugar = sc.sugar.With("mergeID", mergeID)
|
} |
||||||
// if rec.Rec.PartOne {
|
var toSend []record.V1 |
||||||
// if _, found := h.sessions[mergeID]; found {
|
for _, r := range sc.history.DumpRaw() { |
||||||
// msg := "Got another first part of the records before merging the previous one - overwriting!"
|
t, err := strconv.ParseFloat(r.Time, 64) |
||||||
// if rec.Shell == "zsh" {
|
if err != nil { |
||||||
// sc.sugar.Warnw(msg)
|
sc.sugar.Warnw("Invalid time for record - skipping", "time", r.Time) |
||||||
// } else {
|
continue |
||||||
// sc.sugar.Infow(msg + " Unfortunately this is normal in bash, it can't be prevented.")
|
} |
||||||
// }
|
l, ok := latestRemote[r.DeviceID] |
||||||
// }
|
if ok && l >= t { |
||||||
// h.sessions[mergeID] = rec
|
continue |
||||||
// } else {
|
} |
||||||
// if part1, found := h.sessions[mergeID]; found == false {
|
sc.sugar.Infow("record is newer", "new", t, "old", l, "id", r.RecordID, "deviceid", r.DeviceID) |
||||||
// sc.sugar.Warnw("Got second part of record and nothing to merge it with - ignoring!")
|
toSend = append(toSend, r) |
||||||
// } else {
|
} |
||||||
// delete(h.sessions, mergeID)
|
|
||||||
// go h.mergeAndWriteRecord(sugar, part1, rec)
|
client := http.Client{ |
||||||
// }
|
Timeout: 3 * time.Second, |
||||||
// }
|
} |
||||||
// }
|
|
||||||
// }()
|
toSendJson, err := json.Marshal(toSend) |
||||||
//}
|
if err != nil { |
||||||
|
sc.sugar.Errorw("converting toSend to JSON failed", "err", err) |
||||||
|
return err |
||||||
|
} |
||||||
|
reqBody := bytes.NewBuffer(toSendJson) |
||||||
|
|
||||||
|
address := sc.getAddressWithPath(storeEndpoint) |
||||||
|
resp, err := client.Post(address, "application/json", reqBody) |
||||||
|
if err != nil { |
||||||
|
sc.sugar.Errorw("store request failed", "address", address, "err", err) |
||||||
|
return err |
||||||
|
} |
||||||
|
|
||||||
|
defer func(Body io.ReadCloser) { |
||||||
|
err := Body.Close() |
||||||
|
if err != nil { |
||||||
|
sc.sugar.Errorw("reader close failed", "err", err) |
||||||
|
} |
||||||
|
}(resp.Body) |
||||||
|
|
||||||
|
sc.sugar.Debugw("store call", "status", resp.Status) |
||||||
|
|
||||||
|
return nil |
||||||
} |
} |
||||||
|
|||||||
Loading…
Reference in new issue