mirror of
https://github.com/mxpv/podsync.git
synced 2024-05-11 05:55:04 +00:00
Update next update message based on crontab
This commit is contained in:
@@ -111,6 +111,10 @@ func main() {
|
||||
updates := make(chan *config.Feed, 16)
|
||||
defer close(updates)
|
||||
|
||||
//Create Cron
|
||||
c := cron.New(cron.WithChain(cron.SkipIfStillRunning(nil)))
|
||||
m := make(map[string]cron.EntryID)
|
||||
|
||||
// Run updates listener
|
||||
group.Go(func() error {
|
||||
for {
|
||||
@@ -118,6 +122,8 @@ func main() {
|
||||
case feed := <-updates:
|
||||
if err := updater.Update(ctx, feed); err != nil {
|
||||
log.WithError(err).Errorf("failed to update feed: %s", feed.URL)
|
||||
} else {
|
||||
log.Infof("Next update of %s: %s", feed.ID, c.Entry(m[feed.ID]).Next)
|
||||
}
|
||||
case <-ctx.Done():
|
||||
return ctx.Err()
|
||||
@@ -127,23 +133,23 @@ func main() {
|
||||
|
||||
// Run cron scheduler
|
||||
group.Go(func() error {
|
||||
c := cron.New(cron.WithChain(cron.SkipIfStillRunning(nil)))
|
||||
var cronid cron.EntryID
|
||||
|
||||
for _, feed := range cfg.Feeds {
|
||||
_feed := feed
|
||||
if feed.CronSchedule == "" {
|
||||
feed.CronSchedule = fmt.Sprintf("@every %s", feed.UpdatePeriod.String())
|
||||
}
|
||||
|
||||
if _, err = c.AddFunc(_feed.CronSchedule, func() {
|
||||
log.Debugf("adding %q to update queue", _feed.URL)
|
||||
_feed := feed
|
||||
if cronid, err = c.AddFunc(_feed.CronSchedule, func() {
|
||||
log.Debugf("adding %q to update queue", _feed.ID)
|
||||
updates <- _feed
|
||||
|
||||
}); err != nil {
|
||||
log.WithError(err).Fatalf("can't create cron task for feed: %s", _feed.ID)
|
||||
}
|
||||
|
||||
log.Debugf("-> %s (update '%s')", _feed.URL, feed.CronSchedule)
|
||||
|
||||
m[_feed.ID] = cronid
|
||||
log.Debugf("-> %s (update '%s')", _feed.ID, _feed.CronSchedule)
|
||||
// Perform initial update after CLI restart
|
||||
updates <- _feed
|
||||
}
|
||||
|
||||
@@ -68,8 +68,7 @@ func (u *Updater) Update(ctx context.Context, feedConfig *config.Feed) error {
|
||||
}
|
||||
|
||||
elapsed := time.Since(started)
|
||||
nextUpdate := time.Now().Add(feedConfig.UpdatePeriod.Duration)
|
||||
log.Infof("successfully updated feed in %s, next update at %s", elapsed, nextUpdate.Format(time.Kitchen))
|
||||
log.Infof("successfully updated feed in %s", elapsed)
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user