Skip to content

Commit 73b93c1

Browse files
authored
Refresh config, data per run (and allow partial data posting) (#167)
* Allow partial data posting When one datagather fails, warn rather than fail and send the rest of the data. Signed-off-by: Charlie Egan <[email protected]> * Refresh data and config on each period This will make sure that new data is loaded and that the config is also refreshed from disk each time the agent runs. Signed-off-by: Charlie Egan <[email protected]>
1 parent cb7733c commit 73b93c1

File tree

1 file changed

+47
-31
lines changed

1 file changed

+47
-31
lines changed

pkg/agent/run.go

Lines changed: 47 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"log"
1010
"net/url"
1111
"os"
12+
"strings"
1213
"time"
1314

1415
"github.com/jetstack/preflight/api"
@@ -34,6 +35,13 @@ var CredentialsPath string
3435
func Run(cmd *cobra.Command, args []string) {
3536
ctx := context.Background()
3637

38+
for {
39+
gatherAndPostData(ctx)
40+
time.Sleep(time.Duration(Period) * time.Second)
41+
}
42+
}
43+
44+
func gatherAndPostData(ctx context.Context) {
3745
file, err := os.Open(ConfigFilePath)
3846
if err != nil {
3947
log.Fatalf("Failed to load config file for agent from: %s", ConfigFilePath)
@@ -132,10 +140,13 @@ func Run(cmd *cobra.Command, args []string) {
132140
// Fetch from all datagatherers
133141
now := time.Now()
134142
readings := []*api.DataReading{}
143+
failedDataGatherers := []string{}
135144
for k, dg := range dataGatherers {
136145
i, err := dg.Fetch()
137146
if err != nil {
138-
log.Fatalf("Error fetching with DataGatherer %q: %s", k, err)
147+
log.Printf("Error fetching with DataGatherer %q: %s", k, err)
148+
failedDataGatherers = append(failedDataGatherers, k)
149+
continue
139150
}
140151

141152
log.Printf("Gathered data for %q:\n", k)
@@ -148,37 +159,42 @@ func Run(cmd *cobra.Command, args []string) {
148159
})
149160
}
150161

151-
for {
152-
log.Println("Running Agent...")
153-
log.Println("Posting data to ", baseURL)
154-
if config.OrganizationID == "" {
155-
data, err := json.Marshal(readings)
156-
if err != nil {
157-
log.Fatalf("Cannot marshal readings: %+v", err)
158-
}
159-
path := config.Endpoint.Path
160-
if path == "" {
161-
path = "/api/v1/datareadings"
162-
}
163-
res, err := preflightClient.Post(path, bytes.NewBuffer(data))
164-
if code := res.StatusCode; code < 200 || code >= 300 {
165-
errorContent := ""
166-
body, _ := ioutil.ReadAll(res.Body)
167-
if err == nil {
168-
errorContent = string(body)
169-
}
170-
defer res.Body.Close()
171-
172-
log.Fatalf("Received response with status code %d. Body: %s", code, errorContent)
173-
}
174-
} else {
175-
err = preflightClient.PostDataReadings(config.OrganizationID, readings)
176-
// TODO: handle errors gracefully: e.g. handle retries when it is possible
177-
if err != nil {
178-
log.Fatalf("Post to server failed: %+v", err)
162+
if len(failedDataGatherers) > 0 {
163+
log.Printf(
164+
"Warning, the following DataGatherers failed, %s. Their data is not being sent.",
165+
strings.Join(failedDataGatherers, ", "),
166+
)
167+
}
168+
169+
log.Println("Running Agent...")
170+
log.Println("Posting data to ", baseURL)
171+
if config.OrganizationID == "" {
172+
data, err := json.Marshal(readings)
173+
if err != nil {
174+
log.Fatalf("Cannot marshal readings: %+v", err)
175+
}
176+
path := config.Endpoint.Path
177+
if path == "" {
178+
path = "/api/v1/datareadings"
179+
}
180+
res, err := preflightClient.Post(path, bytes.NewBuffer(data))
181+
if code := res.StatusCode; code < 200 || code >= 300 {
182+
errorContent := ""
183+
body, _ := ioutil.ReadAll(res.Body)
184+
if err == nil {
185+
errorContent = string(body)
179186
}
187+
defer res.Body.Close()
188+
189+
log.Fatalf("Received response with status code %d. Body: %s", code, errorContent)
190+
}
191+
} else {
192+
err = preflightClient.PostDataReadings(config.OrganizationID, readings)
193+
// TODO: handle errors gracefully: e.g. handle retries when it is possible
194+
if err != nil {
195+
log.Fatalf("Post to server failed: %+v", err)
180196
}
181-
log.Println("Data sent successfully.")
182-
time.Sleep(time.Duration(Period) * time.Second)
183197
}
198+
199+
log.Println("Data sent successfully.")
184200
}

0 commit comments

Comments
 (0)