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
3435func 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