Skip to content

Commit afd434e

Browse files
committed
"Add retry logic for authentication, subscriptions, content retrieval, and logging to improve error handling and resiliency."
Signed-off-by: Osmany Montero <osmontero@icloud.com>
1 parent 044d410 commit afd434e

1 file changed

Lines changed: 119 additions & 22 deletions

File tree

plugins/o365/main.go

Lines changed: 119 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"fmt"
66
"net/http"
77
"net/url"
8-
"os"
98
"runtime"
109
"strings"
1110
"sync"
@@ -52,7 +51,7 @@ func GetTenantId() string {
5251
func main() {
5352
mode := plugins.GetCfg().Env.Mode
5453
if mode != "worker" {
55-
os.Exit(0)
54+
return
5655
}
5756

5857
for i := 0; i < 2*runtime.NumCPU(); i++ {
@@ -97,6 +96,8 @@ func main() {
9796

9897
for _, group := range moduleConfig.ConfigurationGroups {
9998
go func(group types.ModuleGroup) {
99+
defer wg.Done()
100+
100101
var skip bool
101102

102103
for _, cnf := range group.Configurations {
@@ -109,8 +110,6 @@ func main() {
109110
if !skip {
110111
PullLogs(startTime, endTime, group)
111112
}
112-
113-
wg.Done()
114113
}(group)
115114
}
116115

@@ -195,14 +194,33 @@ func (o *OfficeProcessor) GetAuth() error {
195194

196195
dataBytes := []byte(data.Encode())
197196

198-
result, _, err := utils.DoReq[MicrosoftLoginResponse](requestUrl, dataBytes, http.MethodPost, headers)
199-
if err != nil {
200-
return err
201-
}
197+
// Retry logic for authentication
198+
maxRetries := 3
199+
retryDelay := 2 * time.Second
202200

203-
o.Credentials = result
201+
var result MicrosoftLoginResponse
202+
var err error
204203

205-
return nil
204+
for retry := 0; retry < maxRetries; retry++ {
205+
result, _, err = utils.DoReq[MicrosoftLoginResponse](requestUrl, dataBytes, http.MethodPost, headers)
206+
if err == nil {
207+
o.Credentials = result
208+
return nil
209+
}
210+
211+
_ = catcher.Error("error getting authentication, retrying", err, map[string]any{
212+
"retry": retry + 1,
213+
"maxRetries": maxRetries,
214+
})
215+
216+
if retry < maxRetries-1 {
217+
time.Sleep(retryDelay)
218+
// Increase delay for next retry
219+
retryDelay *= 2
220+
}
221+
}
222+
223+
return catcher.Error("all retries failed when getting authentication", err, nil)
206224
}
207225

208226
func (o *OfficeProcessor) StartSubscriptions() error {
@@ -213,12 +231,40 @@ func (o *OfficeProcessor) StartSubscriptions() error {
213231
"Authorization": fmt.Sprintf("%s %s", o.Credentials.TokenType, o.Credentials.AccessToken),
214232
}
215233

216-
_, _, err := utils.DoReq[StartSubscriptionResponse](link, []byte("{}"), http.MethodPost, headers)
217-
if err != nil {
234+
// Retry logic for starting subscriptions
235+
maxRetries := 3
236+
retryDelay := 2 * time.Second
237+
238+
var err error
239+
240+
for retry := 0; retry < maxRetries; retry++ {
241+
_, _, err = utils.DoReq[StartSubscriptionResponse](link, []byte("{}"), http.MethodPost, headers)
242+
if err == nil {
243+
break
244+
}
245+
246+
// If the subscription is already enabled, that's not an error
218247
if strings.Contains(err.Error(), "subscription is already enabled") {
219248
return nil
220249
}
221-
return err
250+
251+
_ = catcher.Error("error starting subscription, retrying", err, map[string]any{
252+
"retry": retry + 1,
253+
"maxRetries": maxRetries,
254+
"subscription": subscription,
255+
})
256+
257+
if retry < maxRetries-1 {
258+
time.Sleep(retryDelay)
259+
// Increase delay for next retry
260+
retryDelay *= 2
261+
}
262+
}
263+
264+
if err != nil {
265+
return catcher.Error("all retries failed when starting subscription", err, map[string]any{
266+
"subscription": subscription,
267+
})
222268
}
223269
}
224270

@@ -233,13 +279,38 @@ func (o *OfficeProcessor) GetContentList(subscription string, startTime string,
233279
"Authorization": fmt.Sprintf("%s %s", o.Credentials.TokenType, o.Credentials.AccessToken),
234280
}
235281

236-
respBody, status, err := utils.DoReq[[]ContentList](link, nil, http.MethodGet, headers)
237-
if err != nil || status != http.StatusOK {
238-
return []ContentList{}, err
239-
}
282+
// Retry logic for getting content list
283+
maxRetries := 3
284+
retryDelay := 2 * time.Second
285+
286+
var respBody []ContentList
287+
var status int
288+
var err error
289+
290+
for retry := 0; retry < maxRetries; retry++ {
291+
respBody, status, err = utils.DoReq[[]ContentList](link, nil, http.MethodGet, headers)
292+
if err == nil && status == http.StatusOK {
293+
return respBody, nil
294+
}
295+
296+
_ = catcher.Error("error getting content list, retrying", err, map[string]any{
297+
"retry": retry + 1,
298+
"maxRetries": maxRetries,
299+
"subscription": subscription,
300+
"status": status,
301+
})
240302

241-
return respBody, nil
303+
if retry < maxRetries-1 {
304+
time.Sleep(retryDelay)
305+
// Increase delay for next retry
306+
retryDelay *= 2
307+
}
308+
}
242309

310+
return []ContentList{}, catcher.Error("all retries failed when getting content list", err, map[string]any{
311+
"subscription": subscription,
312+
"status": status,
313+
})
243314
}
244315

245316
func (o *OfficeProcessor) GetContentDetails(url string) (ContentDetailsResponse, error) {
@@ -248,12 +319,38 @@ func (o *OfficeProcessor) GetContentDetails(url string) (ContentDetailsResponse,
248319
"Authorization": fmt.Sprintf("%s %s", o.Credentials.TokenType, o.Credentials.AccessToken),
249320
}
250321

251-
respBody, _, err := utils.DoReq[ContentDetailsResponse](url, nil, http.MethodGet, headers)
252-
if err != nil {
253-
return ContentDetailsResponse{}, err
322+
// Retry logic for getting content details
323+
maxRetries := 3
324+
retryDelay := 2 * time.Second
325+
326+
var respBody ContentDetailsResponse
327+
var status int
328+
var err error
329+
330+
for retry := 0; retry < maxRetries; retry++ {
331+
respBody, status, err = utils.DoReq[ContentDetailsResponse](url, nil, http.MethodGet, headers)
332+
if err == nil {
333+
return respBody, nil
334+
}
335+
336+
_ = catcher.Error("error getting content details, retrying", err, map[string]any{
337+
"retry": retry + 1,
338+
"maxRetries": maxRetries,
339+
"url": url,
340+
"status": status,
341+
})
342+
343+
if retry < maxRetries-1 {
344+
time.Sleep(retryDelay)
345+
// Increase delay for next retry
346+
retryDelay *= 2
347+
}
254348
}
255349

256-
return respBody, nil
350+
return ContentDetailsResponse{}, catcher.Error("all retries failed when getting content details", err, map[string]any{
351+
"url": url,
352+
"status": status,
353+
})
257354
}
258355

259356
func (o *OfficeProcessor) GetLogs(startTime string, endTime string) []string {

0 commit comments

Comments
 (0)