Skip to content

Commit

Permalink
Merge pull request #149 from sonroyaalmerol/fix-m3u-caching
Browse files Browse the repository at this point in the history
Clear db before executing save to db on sync to remove stale streams
  • Loading branch information
sonroyaalmerol authored Aug 28, 2024
2 parents 2bbea6d + ff3af84 commit b13fc18
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .github/workflows/linting.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: '1.22'
go-version: '1.23'
cache: false
- name: golangci-lint
uses: golangci/golangci-lint-action@v4
Expand Down
2 changes: 2 additions & 0 deletions database/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ func (db *Instance) SaveToDb(streams []*StreamInfo) error {

pipeline := db.Redis.Pipeline()

pipeline.FlushDB(db.Ctx)

for _, s := range streams {
streamKey := fmt.Sprintf("stream:%s", s.Slug)

Expand Down
22 changes: 22 additions & 0 deletions m3u/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,24 @@ func GenerateAndCacheM3UContent(db *database.Instance, r *http.Request) string {
return content.String()
}

func ClearCache() {
debug := isDebugMode()

M3uCache.Lock()

if debug {
log.Println("[DEBUG] Clearing memory and disk M3U cache.")
}
M3uCache.data = ""
if err := DeleteCacheFile(); err != nil {
if debug {
log.Printf("[DEBUG] Cache file deletion failed: %v\n", err)
}
}

M3uCache.Unlock()
}

func Handler(w http.ResponseWriter, r *http.Request) {
db, err := database.InitializeDb()
if err != nil {
Expand Down Expand Up @@ -177,3 +195,7 @@ func ReadCacheFromFile() (string, error) {
func WriteCacheToFile(content string) error {
return os.WriteFile(cacheFilePath, []byte(content), 0644)
}

func DeleteCacheFile() error {
return os.Remove(cacheFilePath)
}
7 changes: 7 additions & 0 deletions updater/updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,15 @@ func (instance *Updater) UpdateSources(ctx context.Context) {
err := db.SaveToDb(instance.M3UParser.GetStreams())
if err != nil {
log.Printf("Background process: Error updating M3U database: %v\n", err)
log.Println("Background process: Clearing database after error in attempt to fix issue after container restart.")

if err := db.ClearDb(); err != nil {
log.Printf("Background process: Error clearing database: %v\n", err)
}
}

m3u.ClearCache()

log.Println("Background process: Updated M3U database.")
}
}

0 comments on commit b13fc18

Please sign in to comment.