forked from hay-kot/homebox
-
Notifications
You must be signed in to change notification settings - Fork 0
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 (hay-kot#362)
* fix potential memory leak with time.After * add new background service to manage scheduled notifications * update docs * remove old js reference * closes hay-kot#278 * tidy
- Loading branch information
1 parent
54f57f8
commit 552cb1e
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.