Skip to content

Commit

Permalink
Merge 8eb5e2e into caf9c0d
Browse files Browse the repository at this point in the history
  • Loading branch information
brotherlogic committed Apr 6, 2019
2 parents caf9c0d + 8eb5e2e commit ef503ca
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 0 deletions.
30 changes: 30 additions & 0 deletions cdprocessor.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,25 @@ type prodRipper struct {
dial func(server, host string) (*grpc.ClientConn, error)
}

type master interface {
GetRipped(ctx context.Context, req *pb.GetRippedRequest) (*pb.GetRippedResponse, error)
}

type prodMaster struct {
dial func(server string) (*grpc.ClientConn, error)
}

func (p *prodMaster) GetRipped(ctx context.Context, req *pb.GetRippedRequest) (*pb.GetRippedResponse, error) {
conn, err := p.dial("cdprocessor")
if err != nil {
return nil, err
}
defer conn.Close()

client := pb.NewCDProcessorClient(conn)
return client.GetRipped(ctx, req)
}

func (s *Server) resolve() string {
return s.Registry.Identifier
}
Expand Down Expand Up @@ -210,6 +229,7 @@ type Server struct {
ripper ripper
mp3dir string
forceCheck bool
master master
}

// Init builds the server
Expand All @@ -220,6 +240,7 @@ func Init(dir string, mp3dir string) *Server {
getter: &prodGetter{},
dir: dir,
mp3dir: mp3dir,
master: &prodMaster{},
}
s.rc = &prodRc{dial: s.DialMaster}
s.io = &prodIo{dir: dir, log: s.Log}
Expand Down Expand Up @@ -306,6 +327,14 @@ func (s *Server) GetState() []*pbg.State {
missing = int(miss.GetRelease().Id)
}

id := int32(0)
if !s.Registry.Master {
r, err := s.findMissing(context.Background())
if err != nil {
id = r.Id
}
}

return []*pbg.State{
&pbg.State{Key: "count", Value: int64(len(r.Ripped))},
&pbg.State{Key: "missing", Value: int64(len(m.Missing))},
Expand All @@ -316,6 +345,7 @@ func (s *Server) GetState() []*pbg.State {
&pbg.State{Key: "flacs", Value: flacs},
&pbg.State{Key: "mp3rips", Value: s.ripCount},
&pbg.State{Key: "flacrips", Value: s.flacCount},
&pbg.State{Key: "missing_rip", Value: int64(id)},
}
}

Expand Down
23 changes: 23 additions & 0 deletions cdprocessorutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,29 @@ import (
"github.com/brotherlogic/recordcollection/recordutils"
)

func (s *Server) findMissing(ctx context.Context) (*pbcdp.Rip, error) {
localRip, err := s.GetRipped(ctx, &pbcdp.GetRippedRequest{})
ripped, err := s.master.GetRipped(ctx, &pbcdp.GetRippedRequest{})
if err != nil {
return nil, err
}

for _, rip := range ripped.GetRipped() {
found := false
for _, local := range localRip.GetRipped() {
if local.Id == rip.Id {
found = true
}
}

if !found {
return rip, nil
}
}

return nil, nil
}

// verifies the status of the ripped cd
func (s *Server) verify(ctx context.Context, ID int32) error {
record, err := s.getter.getRecord(ctx, ID)
Expand Down
42 changes: 42 additions & 0 deletions cdprocessorutils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,26 @@ import (
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

pb "github.com/brotherlogic/cdprocessor/proto"
pbgd "github.com/brotherlogic/godiscogs"
pbrc "github.com/brotherlogic/recordcollection/proto"
)

type testMaster struct {
fail bool
}

func (p *testMaster) GetRipped(ctx context.Context, req *pb.GetRippedRequest) (*pb.GetRippedResponse, error) {
if p.fail {
return nil, fmt.Errorf("Built to fail")
}
return &pb.GetRippedResponse{
Ripped: []*pb.Rip{
&pb.Rip{Id: 123},
},
}, nil
}

type testGetter struct {
fail bool
updates int
Expand Down Expand Up @@ -212,3 +228,29 @@ func TestExpand(t *testing.T) {
t.Errorf("Poor expansion: %v", s)
}
}

func TestFindMissing(t *testing.T) {
s := InitTestServer("testdata/")
s.master = &testMaster{}

missing, err := s.findMissing(context.Background())

if err != nil {
t.Errorf("Failed: %v", err)
}

if missing == nil {
t.Errorf("Oops")
}
}

func TestFindMissingFail(t *testing.T) {
s := InitTestServer("testdata/")
s.master = &testMaster{fail: true}

missing, err := s.findMissing(context.Background())

if err == nil {
t.Errorf("Did not fail: %v", missing)
}
}

0 comments on commit ef503ca

Please sign in to comment.