Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds a revision field to the catalog contents, the raw file, and the heartbeats #23

Merged
merged 2 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions internal/catalogserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,13 @@ func (srv *CatalogServer) Start() error {
func (srv *CatalogServer) startHeartbeatEmitter() {
ticker := time.NewTicker(heartbeatIntervalSeconds * time.Second)
// publish one immediately
_ = srv.globalServiceClient.PublishHeartbeat(srv.nctx, srv.library.Name)
_ = srv.globalServiceClient.PublishHeartbeat(srv.nctx, srv.library)

go func() {
for {
select {
case <-ticker.C:
_ = srv.globalServiceClient.PublishHeartbeat(srv.nctx, srv.library.Name)
_ = srv.globalServiceClient.PublishHeartbeat(srv.nctx, srv.library)
case <-srv.hbQuit:
ticker.Stop()
close(srv.hbQuit)
Expand Down
6 changes: 4 additions & 2 deletions internal/globalservice/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"time"

"github.com/nats-io/nats.go"
"github.com/synadia-labs/natster/internal/medialibrary"
"github.com/synadia-labs/natster/internal/models"
)

Expand Down Expand Up @@ -146,10 +147,11 @@ func (c *Client) PublishEvent(eventType string, catalog string, target string, r
return c.nc.Flush()
}

func (c *Client) PublishHeartbeat(nctx *models.NatsterContext, catalog string) error {
func (c *Client) PublishHeartbeat(nctx *models.NatsterContext, library *medialibrary.MediaLibrary) error {
hb := models.Heartbeat{
AccountKey: nctx.AccountPublicKey,
Catalog: catalog,
Catalog: library.Name,
Revision: library.LastModified,
}
hbBytes, _ := json.Marshal(&hb)
err := c.nc.Publish("natster.global.heartbeats.put", hbBytes)
Expand Down
15 changes: 11 additions & 4 deletions internal/medialibrary/library.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ import (
"path"
"path/filepath"
"slices"
"time"
)

type MediaLibrary struct {
Name string `json:"name"`
RootDir string `json:"root_dir"`
Description string `json:"description"`
Entries []*MediaEntry `json:"entries"`
Name string `json:"name"`
RootDir string `json:"root_dir"`
Description string `json:"description"`
LastModified int64 `json:"last_modified"`
Entries []*MediaEntry `json:"entries"`
}

type MediaEntry struct {
Expand Down Expand Up @@ -51,10 +53,15 @@ func Load(name string) (*MediaLibrary, error) {
if err != nil {
return nil, err
}
if library.LastModified == 0 {
library.LastModified = time.Now().UTC().Unix()
_ = library.Save()
}
return &library, nil
}

func (library *MediaLibrary) Save() error {
library.LastModified = time.Now().UTC().Unix()
natsterHome, err := getNatsterHome()
if err != nil {
return err
Expand Down
1 change: 1 addition & 0 deletions internal/models/global_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const (
type Heartbeat struct {
Catalog string `json:"catalog"`
AccountKey string `json:"account_key"`
Revision int64 `json:"revision"`
}

// Events are emitted by the natster server process
Expand Down
Loading