Skip to content

Commit

Permalink
Add logging to glue and fix callback bug.
Browse files Browse the repository at this point in the history
  • Loading branch information
mraron committed Apr 9, 2024
1 parent c4c47ac commit 1f567e8
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 15 deletions.
11 changes: 10 additions & 1 deletion cmd/glue.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/spf13/pflag"
"github.com/spf13/viper"
"io/fs"
"log/slog"
"strings"
)

Expand Down Expand Up @@ -66,7 +67,15 @@ func NewGlueCmd(v *viper.Viper) *cobra.Command {
},

RunE: func(cmd *cobra.Command, args []string) error {
g, err := glue.New(judge.NewClient("http://localhost:8888"), glue.WithDatabaseOption(cfg.Database))
conn, err := cfg.Database.Connect()
if err != nil {
return err
}
g, err := glue.New(
judge.NewClient("http://localhost:8888"),
glue.WithDatabaseOption(conn),
glue.WithLogger(slog.Default()),
)
if err != nil {
return err
}
Expand Down
37 changes: 26 additions & 11 deletions internal/glue/glue.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package glue

import (
"context"
"database/sql"
"fmt"
"github.com/mraron/njudge/internal/judge"
"github.com/mraron/njudge/internal/njudge/db"
"github.com/mraron/njudge/internal/web/helpers/config"
"io"
"log/slog"
"strconv"
"time"

Expand All @@ -18,20 +20,18 @@ import (
type Glue struct {
Judge judge.Judger

Logger *slog.Logger

Submissions njudge.Submissions
Problems njudge.Problems
SubmissionsQuery njudge.SubmissionsQuery
}

type Option func(*Glue) error

func WithDatabaseOption(cfg config.Database) Option {
func WithDatabaseOption(conn *sql.DB) Option {
return func(glue *Glue) error {
conn, err := cfg.Connect()
if err != nil {
return err
}
if err = conn.Ping(); err != nil {
if err := conn.Ping(); err != nil {
return err
}
glue.Submissions = db.NewSubmissions(conn)
Expand All @@ -41,9 +41,17 @@ func WithDatabaseOption(cfg config.Database) Option {
}
}

func WithLogger(logger *slog.Logger) Option {
return func(glue *Glue) error {
glue.Logger = logger.With("service", "glue")
return nil
}
}

func New(judge judge.Judger, opts ...Option) (*Glue, error) {
glue := &Glue{
Judge: judge,
Judge: judge,
Logger: slog.New(slog.NewJSONHandler(io.Discard, nil)),
}
for _, opt := range opts {
if err := opt(glue); err != nil {
Expand All @@ -62,6 +70,7 @@ func (g *Glue) ProcessSubmission(ctx context.Context, sub njudge.Submission) err
); err != nil {
return err
}
g.Logger.Info("submission started", "id", sub.ID)

prob, err := g.Problems.Get(ctx, sub.ProblemID)
if err != nil {
Expand All @@ -85,6 +94,10 @@ func (g *Glue) ProcessSubmission(ctx context.Context, sub njudge.Submission) err
Status: *result.Status,
Ontest: null.NewString(result.Test, true),
}
g.Logger.Info(
fmt.Sprintf("callback %d received for submission", result.Index),
"id", sub.ID,
)

return g.Submissions.Update(ctx, sub, njudge.Fields(
njudge.SubmissionFields.Verdict,
Expand All @@ -107,6 +120,8 @@ func (g *Glue) ProcessSubmission(ctx context.Context, sub njudge.Submission) err
score = float32(status.Feedback[0].Score())
}

g.Logger.Info("finished judging submission", "id", sub.ID)

sub.Verdict = njudge.Verdict(verdict)
sub.Status = *status
sub.Ontest = null.NewString("", false)
Expand All @@ -124,10 +139,10 @@ func (g *Glue) ProcessSubmission(ctx context.Context, sub njudge.Submission) err

func (g *Glue) Start(ctx context.Context) {
for {
g.Logger.Info("looking for submissions")
subs, err := g.SubmissionsQuery.GetUnstarted(ctx, 10)
if err != nil {
fmt.Println(err)
// log it
g.Logger.Error("looking for submissions", "error", err)
continue
}

Expand All @@ -138,7 +153,7 @@ func (g *Glue) Start(ctx context.Context) {
// and also a collection of judges
err := g.ProcessSubmission(ctx, s)
if err != nil {
fmt.Println(err)
g.Logger.Error("processing submission", "id", s.ID, "error", err)
return
}
}()
Expand Down
6 changes: 3 additions & 3 deletions internal/judge/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ type Submission struct {
}

type Result struct {
Index int `json:"index,omitempty"`
Test string `json:"test,omitempty"`
Index int `json:"index"`
Test string `json:"test"`
Status *problems.Status `json:"status"`
Error string `json:"error,omitempty"`
Error string `json:"error"`
}

type Server struct {
Expand Down

0 comments on commit 1f567e8

Please sign in to comment.