Skip to content

Commit

Permalink
feat: add cron schedule to gobal player
Browse files Browse the repository at this point in the history
Updates buildId based on the schedule

Fixes: #22
  • Loading branch information
jj-style committed Apr 3, 2024
1 parent feaf3ec commit 0153e5d
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 38 deletions.
1 change: 1 addition & 0 deletions cmd/gobal-player-server/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ host: 0.0.0.0
port: 8080
cache:
ttl: '3s'
cronSchedule: "@every 1m"
8 changes: 5 additions & 3 deletions cmd/gobal-player-server/internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ import (
)

type Config struct {
Host string `mapstructure:"host"`
Port int `mapstructure:"port"`
Cache CacheConfig `mapstructure:"cache"`
Host string `mapstructure:"host"`
Port int `mapstructure:"port"`
Cache CacheConfig `mapstructure:"cache"`
CronSchedule string `mapstructure:"cronSchedule"`
}

type CacheConfig struct {
Expand All @@ -22,6 +23,7 @@ func NewConfig(dir string) (*Config, error) {
viper.SetDefault("host", "0.0.0.0")
viper.SetDefault("port", "8080")
viper.SetDefault("cache.ttl", "3s")
viper.SetDefault("cronSchedule", "@every 1m")

viper.SetConfigName("config")
viper.SetConfigType("yaml")
Expand Down
8 changes: 2 additions & 6 deletions cmd/gobal-player-server/internal/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,6 @@ func NewCache(config *config.Config) resty.Cache[[]byte] {
return resty.NewCache[[]byte](config.Cache.Ttl)
}

func NewGlobalPlayer(config *config.Config, cache resty.Cache[[]byte]) (globalplayer.GlobalPlayer, error) {
buildId, err := globalplayer.GetBuildId(http.DefaultClient)
if err != nil {
return nil, err
}
return globalplayer.NewClient(http.DefaultClient, buildId, cache), nil
func NewGlobalPlayer(config *config.Config, cache resty.Cache[[]byte]) (globalplayer.GlobalPlayer, func(), error) {
return globalplayer.NewClient(http.DefaultClient, cache, config.CronSchedule)
}
3 changes: 2 additions & 1 deletion cmd/gobal-player-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@ func run(ctx context.Context, config *config.Config) error {
ctx, cancel := signal.NotifyContext(ctx, os.Interrupt)
defer cancel()

server, err := InitializeServer(config)
server, cleanup, err := InitializeServer(config)
if err != nil {
return err
}
defer cleanup()

go func() {
addr := fmt.Sprintf("%s:%d", config.Host, config.Port)
Expand Down
2 changes: 1 addition & 1 deletion cmd/gobal-player-server/wire.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ import (
"github.com/jj-style/gobal-player/cmd/gobal-player-server/internal/service"
)

func InitializeServer(config *config.Config) (*server.Server, error) {
func InitializeServer(config *config.Config) (*server.Server, func(), error) {
panic(wire.Build(server.GlobalPlayerProvider, service.NewService, biz.ProviderSet, server.NewServer))
}
10 changes: 6 additions & 4 deletions cmd/gobal-player-server/wire_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 9 additions & 13 deletions cmd/gobal-player-tui/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,19 @@ func main() {
// don't expire cache in the TUI
cache := resty.NewCache[[]byte](0)

gp := globalplayer.NewClient(httpClient, viper.GetString("buildId"), cache)
gp, cleanup2, err := globalplayer.NewClient(httpClient, cache, "@every 1h")
if err != nil {
log.Error(err)
return
}
defer cleanup2()

player, cleanup, err := audioplayer.NewPlayer()
player, cleanup3, err := audioplayer.NewPlayer()
if err != nil {
log.Error(err)
return
}
defer cleanup()
defer cleanup3()

app := NewApp(gp, player, httpClient, cache)
if err := app.Run(); err != nil {
Expand All @@ -66,15 +71,6 @@ func initLogger() func() {
}

// creates a new *http.Client based on the config.
// Checks whether it has a valid token or generates a new one if not.
func newHttpClient() (*http.Client, error) {
client := &http.Client{Transport: &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: viper.GetBool("insecure")}}}
if ok := globalplayer.CheckBuildId(client, viper.GetString("buildId")); !ok {
newBuildId, err := globalplayer.GetBuildId(client)
if err != nil {
return nil, err
}
viper.Set("buildId", newBuildId)
}
return client, nil
return &http.Client{Transport: &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: viper.GetBool("insecure")}}}, nil
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ require (
github.com/gorilla/feeds v1.1.2
github.com/patrickmn/go-cache v2.1.0+incompatible
github.com/rivo/tview v0.0.0-20240225120200-5605142ca62e
github.com/robfig/cron/v3 v3.0.0
github.com/samber/lo v1.39.0
github.com/sirupsen/logrus v1.9.3
github.com/spf13/viper v1.18.2
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJ
github.com/rivo/uniseg v0.4.3/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/robfig/cron/v3 v3.0.0 h1:kQ6Cb7aHOHTSzNVNEhmp8EcWKLb4CbiMW9h9VyIhO4E=
github.com/robfig/cron/v3 v3.0.0/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog=
github.com/sagikazarmark/locafero v0.4.0 h1:HApY1R9zGo4DBgr7dqsTH/JJxLTTsOt7u6keLGt6kNQ=
Expand Down
51 changes: 41 additions & 10 deletions pkg/globalplayer/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/jj-style/gobal-player/pkg/globalplayer/models"
"github.com/jj-style/gobal-player/pkg/globalplayer/models/nextjs"
"github.com/jj-style/gobal-player/pkg/resty"
"github.com/robfig/cron/v3"
"github.com/samber/lo"
)

Expand All @@ -24,18 +25,48 @@ type GlobalPlayer interface {
}

type gpClient struct {
rc resty.Client
rc resty.Client
cron *cron.Cron
}

func NewClient(hc *http.Client, apiKey string, cache resty.Cache[[]byte]) GlobalPlayer {
baseUrlWithApiKey, _ := url.JoinPath(baseUrl, apiKey)
restClient := resty.NewClient(
resty.WithBaseUrl(baseUrlWithApiKey),
resty.WithHttpClient(hc),
resty.WithCache(cache),
)
c := &gpClient{rc: restClient}
return c
func NewClient(hc *http.Client, cache resty.Cache[[]byte], updateDuration string) (GlobalPlayer, func(), error) {

newRestClient := func() (resty.Client, error) {
buildId, err := GetBuildId(hc)
if err != nil {
return nil, err
}
baseUrlWithApiKey, _ := url.JoinPath(baseUrl, buildId)
rc := resty.NewClient(
resty.WithBaseUrl(baseUrlWithApiKey),
resty.WithHttpClient(hc),
resty.WithCache(cache),
)
return rc, nil
}

rc, err := newRestClient()
if err != nil {
return nil, func() {}, err
}

cron := cron.New()
client := &gpClient{rc: rc, cron: cron}

if updateDuration != "" {
_, err = cron.AddFunc("@every 1m", func() {
if rc, err = newRestClient(); err != nil {
client.rc = rc
}
})
if err != nil {
return nil, func() {}, err
}
}

cron.Start()

return client, func() { cron.Stop() }, nil
}

func (c *gpClient) GetStations() ([]*models.Station, error) {
Expand Down

0 comments on commit 0153e5d

Please sign in to comment.