Skip to content

Commit

Permalink
refactor: Move log writer creation to helper function
Browse files Browse the repository at this point in the history
  • Loading branch information
bgins committed Nov 22, 2024
1 parent af94db1 commit 888df46
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
12 changes: 3 additions & 9 deletions pkg/solver/store/memory/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package store

import (
"fmt"
"os"
"strings"
"sync"

Expand All @@ -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{
Expand Down
16 changes: 16 additions & 0 deletions pkg/solver/store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}

0 comments on commit 888df46

Please sign in to comment.