From 888df466c63384ea917a790a792eda31d943ee5b Mon Sep 17 00:00:00 2001 From: Brian Ginsburg Date: Fri, 22 Nov 2024 10:56:35 -0800 Subject: [PATCH] refactor: Move log writer creation to helper function --- pkg/solver/store/memory/store.go | 12 +++--------- pkg/solver/store/store.go | 16 ++++++++++++++++ 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/pkg/solver/store/memory/store.go b/pkg/solver/store/memory/store.go index 5a45c92b..55472a24 100644 --- a/pkg/solver/store/memory/store.go +++ b/pkg/solver/store/memory/store.go @@ -2,7 +2,6 @@ package store import ( "fmt" - "os" "strings" "sync" @@ -22,15 +21,10 @@ type SolverStoreMemory struct { } func NewSolverStoreMemory() (*SolverStoreMemory, error) { - logWriters := make(map[string]jsonl.Writer) - kinds := []string{"job_offers", "resource_offers", "deals", "decisions", "results"} - for k := range kinds { - logfile, err := os.OpenFile(fmt.Sprintf("/var/tmp/lilypad_%s.jsonl", kinds[k]), os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0644) - if err != nil { - return nil, err - } - logWriters[kinds[k]] = jsonl.NewWriter(logfile) + logWriters, err := store.GetLogWriters(kinds) + if err != nil { + return nil, err } return &SolverStoreMemory{ diff --git a/pkg/solver/store/store.go b/pkg/solver/store/store.go index 74f519f8..0b9ee881 100644 --- a/pkg/solver/store/store.go +++ b/pkg/solver/store/store.go @@ -2,8 +2,10 @@ package store import ( "fmt" + "os" "github.com/lilypad-tech/lilypad/pkg/data" + "github.com/lilypad-tech/lilypad/pkg/jsonl" ) type GetJobOffersQuery struct { @@ -79,3 +81,17 @@ type SolverStore interface { func GetMatchID(resourceOffer string, jobOffer string) string { return fmt.Sprintf("%s-%s", resourceOffer, jobOffer) } + +func GetLogWriters(kinds []string) (map[string]jsonl.Writer, error) { + logWriters := make(map[string]jsonl.Writer) + + for k := range kinds { + logfile, err := os.OpenFile(fmt.Sprintf("/var/tmp/lilypad_%s.jsonl", kinds[k]), os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0644) + if err != nil { + return nil, err + } + logWriters[kinds[k]] = jsonl.NewWriter(logfile) + } + + return logWriters, nil +}