Add the possibility to run Miniflux as a cronjob

This commit is contained in:
Frédéric Guillot 2023-06-24 22:06:48 -07:00
parent c3250257b1
commit c85b19098d
3 changed files with 28 additions and 2 deletions

View file

@ -11,6 +11,7 @@ import (
"miniflux.app/database" "miniflux.app/database"
"miniflux.app/locale" "miniflux.app/locale"
"miniflux.app/logger" "miniflux.app/logger"
feedHandler "miniflux.app/reader/handler"
"miniflux.app/storage" "miniflux.app/storage"
"miniflux.app/ui/static" "miniflux.app/ui/static"
"miniflux.app/version" "miniflux.app/version"
@ -28,6 +29,7 @@ const (
flagConfigFileHelp = "Load configuration file" flagConfigFileHelp = "Load configuration file"
flagConfigDumpHelp = "Print parsed configuration values" flagConfigDumpHelp = "Print parsed configuration values"
flagHealthCheckHelp = `Perform a health check on the given endpoint (the value "auto" try to guess the health check endpoint).` flagHealthCheckHelp = `Perform a health check on the given endpoint (the value "auto" try to guess the health check endpoint).`
flagCronjobHelp = "Run Miniflux as a cronjob to refresh a batch of feeds and exit"
) )
// Parse parses command line arguments. // Parse parses command line arguments.
@ -45,6 +47,7 @@ func Parse() {
flagConfigFile string flagConfigFile string
flagConfigDump bool flagConfigDump bool
flagHealthCheck string flagHealthCheck string
flagCronjob bool
) )
flag.BoolVar(&flagInfo, "info", false, flagInfoHelp) flag.BoolVar(&flagInfo, "info", false, flagInfoHelp)
@ -61,6 +64,7 @@ func Parse() {
flag.StringVar(&flagConfigFile, "c", "", flagConfigFileHelp) flag.StringVar(&flagConfigFile, "c", "", flagConfigFileHelp)
flag.BoolVar(&flagConfigDump, "config-dump", false, flagConfigDumpHelp) flag.BoolVar(&flagConfigDump, "config-dump", false, flagConfigDumpHelp)
flag.StringVar(&flagHealthCheck, "healthcheck", "", flagHealthCheckHelp) flag.StringVar(&flagHealthCheck, "healthcheck", "", flagHealthCheckHelp)
flag.BoolVar(&flagCronjob, "cronjob", false, flagCronjobHelp)
flag.Parse() flag.Parse()
cfg := config.NewParser() cfg := config.NewParser()
@ -187,5 +191,21 @@ func Parse() {
createAdmin(store) createAdmin(store)
} }
if flagCronjob {
jobs, err := store.NewBatch(config.Opts.BatchSize())
if err != nil {
logger.Error("[Cronjob] %v", err)
}
logger.Info("[Cronjob]] Processing %d jobs", len(jobs))
for _, job := range jobs {
if err := feedHandler.RefreshFeed(store, job.UserID, job.FeedID); err != nil {
logger.Error("[Cronjob] Refreshing the feed #%d returned this error: %v", job.FeedID, err)
}
}
return
}
startDaemon(store) startDaemon(store)
} }

View file

@ -6,13 +6,19 @@ miniflux \- Minimalist and opinionated feed reader
.SH SYNOPSIS .SH SYNOPSIS
\fBminiflux\fR [-vic] [-create-admin] [-debug] [-flush-sessions] [-info] [-migrate] \fBminiflux\fR [-vic] [-create-admin] [-debug] [-flush-sessions] [-info] [-migrate]
[-reset-feed-errors] [-reset-password] [-version] [-config-file] [-config-dump] [-reset-feed-errors] [-reset-password] [-version] [-config-file]
[-config-dump] [-cronjob] [-healthcheck]
.SH DESCRIPTION .SH DESCRIPTION
\fBminiflux\fR is a minimalist and opinionated feed reader. \fBminiflux\fR is a minimalist and opinionated feed reader.
.SH OPTIONS .SH OPTIONS
.PP .PP
.B \-cronjob
.RS 4
Run Miniflux as a cronjob to refresh a batch of feeds and exit\&.
.RE
.PP
.B \-c .B \-c
.RS 4 .RS 4
Load configuration file\&. Load configuration file\&.

View file

@ -38,10 +38,10 @@ func Serve(store *storage.Storage, pool *worker.Pool) {
func feedScheduler(store *storage.Storage, pool *worker.Pool, frequency, batchSize int) { func feedScheduler(store *storage.Storage, pool *worker.Pool, frequency, batchSize int) {
for range time.Tick(time.Duration(frequency) * time.Minute) { for range time.Tick(time.Duration(frequency) * time.Minute) {
jobs, err := store.NewBatch(batchSize) jobs, err := store.NewBatch(batchSize)
logger.Info("[Scheduler:Feed] Pushing %d jobs to the queue", len(jobs))
if err != nil { if err != nil {
logger.Error("[Scheduler:Feed] %v", err) logger.Error("[Scheduler:Feed] %v", err)
} else { } else {
logger.Debug("[Scheduler:Feed] Pushing %d jobs", len(jobs))
pool.Push(jobs) pool.Push(jobs)
} }
} }