Revert "Add preliminary support for persisting pre-saved history entries remotely"

This reverts commit ff98a7907c. That commit is incomplete since it doesn't include support for the continous deletion of pre-saved history entries as soon as they
finish running. Support for this will require a good bit more work/thought, so reverting for and keeping this code in the git history.
This commit is contained in:
David Dworken
2023-09-21 12:39:20 -07:00
parent ff98a7907c
commit 66916c27cb
8 changed files with 42 additions and 76 deletions

View File

@@ -55,7 +55,6 @@ func maybeUploadSkippedHistoryEntries(ctx context.Context) error {
// Upload the missing entries
db := hctx.GetDb(ctx)
// TODO: There is a bug here because MissedUploadTimestamp is going to be a second or two after the history entry that needs to be uploaded
query := fmt.Sprintf("after:%s", time.Unix(config.MissedUploadTimestamp, 0).Format("2006-01-02"))
entries, err := lib.Search(ctx, db, query, 0)
if err != nil {
@@ -82,21 +81,6 @@ func maybeUploadSkippedHistoryEntries(ctx context.Context) error {
return nil
}
func handlePotentialUploadFailure(err error, config *hctx.ClientConfig) {
if err != nil {
if lib.IsOfflineError(err) {
hctx.GetLogger().Infof("Failed to remotely persist hishtory entry because we failed to connect to the remote server! This is likely because the device is offline, but also could be because the remote server is having reliability issues. Original error: %v", err)
if !config.HaveMissedUploads {
config.HaveMissedUploads = true
config.MissedUploadTimestamp = time.Now().Unix()
lib.CheckFatalError(hctx.SetConfig(*config))
}
} else {
lib.CheckFatalError(err)
}
}
}
func presaveHistoryEntry(ctx context.Context) {
config := hctx.GetConf(ctx)
if !config.IsEnabled {
@@ -135,13 +119,12 @@ func presaveHistoryEntry(ctx context.Context) {
lib.CheckFatalError(err)
db.Commit()
// And persist it remotely
if !config.IsOffline {
jsonValue, err := lib.EncryptAndMarshal(config, []*data.HistoryEntry{entry})
lib.CheckFatalError(err)
_, err = lib.ApiPost("/api/v1/submit?source_device_id="+config.DeviceId, "application/json", jsonValue)
handlePotentialUploadFailure(err, &config)
}
// Note that we aren't persisting these half-entries remotely,
// since they should be updated with the rest of the information very soon. If they
// are never updated (e.g. due to a long-running command that never finishes), then
// they will only be available on this device. That isn't perfect since it means
// history entries can get out of sync, but it is probably good enough.
// TODO: Consider improving this
}
func saveHistoryEntry(ctx context.Context) {
@@ -192,7 +175,6 @@ func saveHistoryEntry(ctx context.Context) {
jsonValue, err := lib.EncryptAndMarshal(config, []*data.HistoryEntry{entry})
lib.CheckFatalError(err)
w, err := lib.ApiPost("/api/v1/submit?source_device_id="+config.DeviceId, "application/json", jsonValue)
handlePotentialUploadFailure(err, &config)
if err == nil {
submitResponse := shared.SubmitResponse{}
err := json.Unmarshal(w, &submitResponse)
@@ -201,6 +183,17 @@ func saveHistoryEntry(ctx context.Context) {
}
shouldCheckForDeletionRequests = submitResponse.HaveDeletionRequests
shouldCheckForDumpRequests = submitResponse.HaveDumpRequests
} else {
if lib.IsOfflineError(err) {
hctx.GetLogger().Infof("Failed to remotely persist hishtory entry because we failed to connect to the remote server! This is likely because the device is offline, but also could be because the remote server is having reliability issues. Original error: %v", err)
if !config.HaveMissedUploads {
config.HaveMissedUploads = true
config.MissedUploadTimestamp = time.Now().Unix()
lib.CheckFatalError(hctx.SetConfig(config))
}
} else {
lib.CheckFatalError(err)
}
}
}