This repository has been archived on 2022-05-25. You can view files and clone it, but cannot push or open issues or pull requests.
elon-eats-my-tweets/daemon/daemon.go

31 lines
806 B
Go

package daemon
import (
"context"
"time"
"git.netflux.io/rob/elon-eats-my-tweets/twitterapi"
"go.uber.org/zap"
)
// Run blocks until the provided context is terminated.
func Run(ctx context.Context, store twitterapi.Store, apiClient twitterapi.APIClient, logger *zap.SugaredLogger) error {
tweetPersister := TweetPersister{store: store, apiClient: apiClient, logger: logger}
ticker := time.NewTicker(time.Second * 10)
defer ticker.Stop()
for {
select {
case <-ticker.C:
if n, err := tweetPersister.PersistTweets(ctx); err != nil {
logger.With("err", err, "inserted", n).Error("error updating tweets")
} else {
logger.With("inserted", n).Info("tweets updated")
}
case <-ctx.Done():
logger.With("err", ctx.Err()).Info("context complete, exiting")
return nil
}
}
}