31 lines
806 B
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
|
||
|
}
|
||
|
}
|
||
|
}
|