2023-06-19 23:42:47 +02:00
|
|
|
// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
2018-11-12 00:32:48 +01:00
|
|
|
|
|
|
|
package scheduler // import "miniflux.app/service/scheduler"
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"miniflux.app/config"
|
|
|
|
"miniflux.app/logger"
|
2020-09-28 01:01:06 +02:00
|
|
|
"miniflux.app/metric"
|
2020-09-13 05:04:06 +02:00
|
|
|
"miniflux.app/model"
|
2018-11-12 00:32:48 +01:00
|
|
|
"miniflux.app/storage"
|
|
|
|
"miniflux.app/worker"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Serve starts the internal scheduler.
|
2019-06-02 03:18:09 +02:00
|
|
|
func Serve(store *storage.Storage, pool *worker.Pool) {
|
2018-11-12 00:54:19 +01:00
|
|
|
logger.Info(`Starting scheduler...`)
|
2019-09-15 20:47:39 +02:00
|
|
|
|
|
|
|
go feedScheduler(
|
|
|
|
store,
|
|
|
|
pool,
|
|
|
|
config.Opts.PollingFrequency(),
|
|
|
|
config.Opts.BatchSize(),
|
|
|
|
)
|
|
|
|
|
|
|
|
go cleanupScheduler(
|
|
|
|
store,
|
|
|
|
config.Opts.CleanupFrequencyHours(),
|
|
|
|
config.Opts.CleanupArchiveReadDays(),
|
2020-09-13 05:04:06 +02:00
|
|
|
config.Opts.CleanupArchiveUnreadDays(),
|
2021-05-24 05:45:37 +02:00
|
|
|
config.Opts.CleanupArchiveBatchSize(),
|
2019-09-15 20:47:39 +02:00
|
|
|
config.Opts.CleanupRemoveSessionsDays(),
|
|
|
|
)
|
2018-11-12 00:32:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func feedScheduler(store *storage.Storage, pool *worker.Pool, frequency, batchSize int) {
|
2020-09-28 01:01:06 +02:00
|
|
|
for range time.Tick(time.Duration(frequency) * time.Minute) {
|
2018-11-12 00:32:48 +01:00
|
|
|
jobs, err := store.NewBatch(batchSize)
|
2023-06-25 07:06:48 +02:00
|
|
|
logger.Info("[Scheduler:Feed] Pushing %d jobs to the queue", len(jobs))
|
2018-11-12 00:32:48 +01:00
|
|
|
if err != nil {
|
|
|
|
logger.Error("[Scheduler:Feed] %v", err)
|
|
|
|
} else {
|
|
|
|
pool.Push(jobs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-24 05:45:37 +02:00
|
|
|
func cleanupScheduler(store *storage.Storage, frequency, archiveReadDays, archiveUnreadDays, archiveBatchSize, sessionsDays int) {
|
2020-09-28 01:01:06 +02:00
|
|
|
for range time.Tick(time.Duration(frequency) * time.Hour) {
|
2019-09-12 05:10:34 +02:00
|
|
|
nbSessions := store.CleanOldSessions(sessionsDays)
|
|
|
|
nbUserSessions := store.CleanOldUserSessions(sessionsDays)
|
2018-11-12 00:32:48 +01:00
|
|
|
logger.Info("[Scheduler:Cleanup] Cleaned %d sessions and %d user sessions", nbSessions, nbUserSessions)
|
|
|
|
|
2020-09-28 01:01:06 +02:00
|
|
|
startTime := time.Now()
|
2021-05-24 05:45:37 +02:00
|
|
|
if rowsAffected, err := store.ArchiveEntries(model.EntryStatusRead, archiveReadDays, archiveBatchSize); err != nil {
|
2020-09-13 05:04:06 +02:00
|
|
|
logger.Error("[Scheduler:ArchiveReadEntries] %v", err)
|
|
|
|
} else {
|
|
|
|
logger.Info("[Scheduler:ArchiveReadEntries] %d entries changed", rowsAffected)
|
2020-09-28 01:01:06 +02:00
|
|
|
|
|
|
|
if config.Opts.HasMetricsCollector() {
|
|
|
|
metric.ArchiveEntriesDuration.WithLabelValues(model.EntryStatusRead).Observe(time.Since(startTime).Seconds())
|
|
|
|
}
|
2020-09-13 05:04:06 +02:00
|
|
|
}
|
|
|
|
|
2020-09-28 01:01:06 +02:00
|
|
|
startTime = time.Now()
|
2021-05-24 05:45:37 +02:00
|
|
|
if rowsAffected, err := store.ArchiveEntries(model.EntryStatusUnread, archiveUnreadDays, archiveBatchSize); err != nil {
|
2020-09-13 05:04:06 +02:00
|
|
|
logger.Error("[Scheduler:ArchiveUnreadEntries] %v", err)
|
|
|
|
} else {
|
|
|
|
logger.Info("[Scheduler:ArchiveUnreadEntries] %d entries changed", rowsAffected)
|
2020-09-28 01:01:06 +02:00
|
|
|
|
|
|
|
if config.Opts.HasMetricsCollector() {
|
|
|
|
metric.ArchiveEntriesDuration.WithLabelValues(model.EntryStatusUnread).Observe(time.Since(startTime).Seconds())
|
|
|
|
}
|
2018-11-12 00:32:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|