-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d658912
commit 18e9d97
Showing
5 changed files
with
132 additions
and
1 deletion.
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,27 @@ | ||
package scheduler | ||
|
||
import ( | ||
"context" | ||
"log/slog" | ||
"time" | ||
|
||
"github.com/Pineapple217/mb/pkg/database" | ||
) | ||
|
||
func CleanCache(q *database.Queries) { | ||
slog.Info("Starting clean databases chaches") | ||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*30) | ||
defer cancel() | ||
|
||
c, err := q.RemoveUnusedYoutubeCache(ctx) | ||
if err != nil { | ||
slog.Warn("Failed to clean database cache", "type", "youtube", "error", err) | ||
} | ||
slog.Info("Cleaned cache", "type", "youtube", "count", c) | ||
|
||
c, err = q.RemoveUnusedSpotifyCache(ctx) | ||
if err != nil { | ||
slog.Warn("Failed to clean database cache", "type", "spotify", "error", err) | ||
} | ||
slog.Info("Cleaned cache", "type", "spotify", "count", c) | ||
} |
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,43 @@ | ||
package scheduler | ||
|
||
import ( | ||
"sync" | ||
"time" | ||
) | ||
|
||
type Scheduler struct { | ||
wg *sync.WaitGroup | ||
doneCh chan bool | ||
} | ||
|
||
func NewScheduler() Scheduler { | ||
var wg sync.WaitGroup | ||
done := make(chan bool) | ||
return Scheduler{ | ||
wg: &wg, | ||
doneCh: done, | ||
} | ||
} | ||
|
||
func (s Scheduler) Stop() { | ||
close(s.doneCh) | ||
s.wg.Wait() | ||
} | ||
|
||
func (s Scheduler) Schedule(t time.Duration, f func()) { | ||
s.wg.Add(1) | ||
ticker := time.NewTicker(t) | ||
go f() | ||
go func() { | ||
for { | ||
select { | ||
case <-ticker.C: | ||
f() | ||
case <-s.doneCh: | ||
ticker.Stop() | ||
s.wg.Done() | ||
return | ||
} | ||
} | ||
}() | ||
} |