mirror of
https://github.com/idanoo/autobrr
synced 2025-07-23 16:59:12 +00:00
feat(notifications): on update available (#352)
This commit is contained in:
parent
b4589243de
commit
2a3a839081
11 changed files with 326 additions and 18 deletions
|
@ -78,14 +78,13 @@ const (
|
|||
type NotificationEvent string
|
||||
|
||||
const (
|
||||
//NotificationEventAppUpdateAvailable NotificationEvent = "APP_UPDATE_AVAILABLE"
|
||||
|
||||
NotificationEventPushApproved NotificationEvent = "PUSH_APPROVED"
|
||||
NotificationEventPushRejected NotificationEvent = "PUSH_REJECTED"
|
||||
NotificationEventPushError NotificationEvent = "PUSH_ERROR"
|
||||
NotificationEventIRCDisconnected NotificationEvent = "IRC_DISCONNECTED"
|
||||
NotificationEventIRCReconnected NotificationEvent = "IRC_RECONNECTED"
|
||||
NotificationEventTest NotificationEvent = "TEST"
|
||||
NotificationEventAppUpdateAvailable NotificationEvent = "APP_UPDATE_AVAILABLE"
|
||||
NotificationEventPushApproved NotificationEvent = "PUSH_APPROVED"
|
||||
NotificationEventPushRejected NotificationEvent = "PUSH_REJECTED"
|
||||
NotificationEventPushError NotificationEvent = "PUSH_ERROR"
|
||||
NotificationEventIRCDisconnected NotificationEvent = "IRC_DISCONNECTED"
|
||||
NotificationEventIRCReconnected NotificationEvent = "IRC_RECONNECTED"
|
||||
NotificationEventTest NotificationEvent = "TEST"
|
||||
)
|
||||
|
||||
type NotificationEventArr []NotificationEvent
|
||||
|
|
|
@ -128,7 +128,9 @@ func (s *service) registerSenders() {
|
|||
|
||||
// Send notifications
|
||||
func (s *service) Send(event domain.NotificationEvent, payload domain.NotificationPayload) {
|
||||
s.log.Debug().Msgf("sending notification for %v", string(event))
|
||||
if len(s.senders) > 0 {
|
||||
s.log.Debug().Msgf("sending notification for %v", string(event))
|
||||
}
|
||||
|
||||
go func() {
|
||||
for _, sender := range s.senders {
|
||||
|
|
51
internal/scheduler/jobs.go
Normal file
51
internal/scheduler/jobs.go
Normal file
|
@ -0,0 +1,51 @@
|
|||
package scheduler
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/autobrr/autobrr/internal/domain"
|
||||
"github.com/autobrr/autobrr/internal/notification"
|
||||
"github.com/autobrr/autobrr/pkg/version"
|
||||
|
||||
"github.com/rs/zerolog"
|
||||
)
|
||||
|
||||
type CheckUpdatesJob struct {
|
||||
Name string
|
||||
Log zerolog.Logger
|
||||
Version string
|
||||
NotifSvc notification.Service
|
||||
|
||||
lastCheckVersion string
|
||||
}
|
||||
|
||||
func (j *CheckUpdatesJob) Run() {
|
||||
v := version.Checker{
|
||||
Owner: "autobrr",
|
||||
Repo: "autobrr",
|
||||
}
|
||||
|
||||
newAvailable, newVersion, err := v.CheckNewVersion(context.TODO(), j.Version)
|
||||
if err != nil {
|
||||
j.Log.Error().Err(err).Msg("could not check for new release")
|
||||
return
|
||||
}
|
||||
|
||||
if newAvailable {
|
||||
j.Log.Info().Msgf("a new release has been found: %v Consider updating.", newVersion)
|
||||
|
||||
// this is not persisted so this can trigger more than once
|
||||
// lets check if we have different versions between runs
|
||||
if newVersion != j.lastCheckVersion {
|
||||
j.NotifSvc.Send(domain.NotificationEventAppUpdateAvailable, domain.NotificationPayload{
|
||||
Subject: "New update available!",
|
||||
Message: newVersion,
|
||||
Event: domain.NotificationEventAppUpdateAvailable,
|
||||
Timestamp: time.Now(),
|
||||
})
|
||||
}
|
||||
|
||||
j.lastCheckVersion = newVersion
|
||||
}
|
||||
}
|
|
@ -1,7 +1,10 @@
|
|||
package scheduler
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/autobrr/autobrr/internal/logger"
|
||||
"github.com/autobrr/autobrr/internal/notification"
|
||||
"github.com/autobrr/autobrr/pkg/errors"
|
||||
|
||||
"github.com/robfig/cron/v3"
|
||||
|
@ -17,15 +20,19 @@ type Service interface {
|
|||
}
|
||||
|
||||
type service struct {
|
||||
log zerolog.Logger
|
||||
cron *cron.Cron
|
||||
log zerolog.Logger
|
||||
version string
|
||||
notificationSvc notification.Service
|
||||
|
||||
cron *cron.Cron
|
||||
jobs map[string]cron.EntryID
|
||||
}
|
||||
|
||||
func NewService(log logger.Logger) Service {
|
||||
func NewService(log logger.Logger, version string, notificationSvc notification.Service) Service {
|
||||
return &service{
|
||||
log: log.With().Str("module", "scheduler").Logger(),
|
||||
log: log.With().Str("module", "scheduler").Logger(),
|
||||
version: version,
|
||||
notificationSvc: notificationSvc,
|
||||
cron: cron.New(cron.WithChain(
|
||||
cron.Recover(cron.DefaultLogger),
|
||||
)),
|
||||
|
@ -36,15 +43,35 @@ func NewService(log logger.Logger) Service {
|
|||
func (s *service) Start() {
|
||||
s.log.Debug().Msg("scheduler.Start")
|
||||
|
||||
// start scheduler
|
||||
s.cron.Start()
|
||||
|
||||
// init jobs
|
||||
go s.addAppJobs()
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (s *service) addAppJobs() {
|
||||
time.Sleep(5 * time.Second)
|
||||
|
||||
checkUpdates := &CheckUpdatesJob{
|
||||
Name: "app-check-updates",
|
||||
Log: s.log.With().Str("job", "app-check-updates").Logger(),
|
||||
Version: s.version,
|
||||
NotifSvc: s.notificationSvc,
|
||||
lastCheckVersion: "",
|
||||
}
|
||||
|
||||
s.AddJob(checkUpdates, "2 */6 * * *", "app-check-updates")
|
||||
}
|
||||
|
||||
func (s *service) Stop() {
|
||||
s.log.Debug().Msg("scheduler.Stop")
|
||||
s.cron.Stop()
|
||||
return
|
||||
}
|
||||
|
||||
func (s *service) AddJob(job cron.Job, interval string, identifier string) (int, error) {
|
||||
|
||||
id, err := s.cron.AddJob(interval, cron.NewChain(
|
||||
|
@ -88,3 +115,14 @@ func (s *service) RemoveJobByIdentifier(id string) error {
|
|||
|
||||
return nil
|
||||
}
|
||||
|
||||
type GenericJob struct {
|
||||
Name string
|
||||
Log zerolog.Logger
|
||||
|
||||
callback func()
|
||||
}
|
||||
|
||||
func (j *GenericJob) Run() {
|
||||
j.callback()
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue