This repository has been archived by the owner on Jun 12, 2024. It is now read-only.
generated from hay-kot/go-web-template
-
-
Notifications
You must be signed in to change notification settings - Fork 222
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: use notifiers on schedule (#362)
* fix potential memory leak with time.After * add new background service to manage scheduled notifications * update docs * remove old js reference * closes #278 * tidy
- Loading branch information
Showing
16 changed files
with
201 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package services | ||
|
||
import ( | ||
"context" | ||
"strings" | ||
"time" | ||
|
||
"github.com/containrrr/shoutrrr" | ||
"github.com/hay-kot/homebox/backend/internal/data/repo" | ||
"github.com/hay-kot/homebox/backend/internal/data/types" | ||
"github.com/rs/zerolog/log" | ||
) | ||
|
||
type BackgroundService struct { | ||
repos *repo.AllRepos | ||
} | ||
|
||
func (svc *BackgroundService) SendNotifiersToday(ctx context.Context) error { | ||
// Get All Groups | ||
groups, err := svc.repos.Groups.GetAllGroups(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
today := types.DateFromTime(time.Now()) | ||
|
||
for i := range groups { | ||
group := groups[i] | ||
|
||
entries, err := svc.repos.MaintEntry.GetScheduled(ctx, group.ID, today) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if len(entries) == 0 { | ||
log.Debug(). | ||
Str("group_name", group.Name). | ||
Str("group_id", group.ID.String()). | ||
Msg("No scheduled maintenance for today") | ||
continue | ||
} | ||
|
||
notifiers, err := svc.repos.Notifiers.GetByGroup(ctx, group.ID) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
urls := make([]string, len(notifiers)) | ||
for i := range notifiers { | ||
urls[i] = notifiers[i].URL | ||
} | ||
|
||
bldr := strings.Builder{} | ||
|
||
bldr.WriteString("Homebox Maintenance for (") | ||
bldr.WriteString(today.String()) | ||
bldr.WriteString("):\n") | ||
|
||
for i := range entries { | ||
entry := entries[i] | ||
bldr.WriteString(" - ") | ||
bldr.WriteString(entry.Name) | ||
bldr.WriteString("\n") | ||
} | ||
|
||
var sendErrs []error | ||
for i := range urls { | ||
err := shoutrrr.Send(urls[i], bldr.String()) | ||
|
||
if err != nil { | ||
sendErrs = append(sendErrs, err) | ||
} | ||
} | ||
|
||
if len(sendErrs) > 0 { | ||
return sendErrs[0] | ||
} | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.