Skip to content

Commit dca3496

Browse files
committed
Add invoker base
1 parent 63b1608 commit dca3496

File tree

28 files changed

+671
-97
lines changed

28 files changed

+671
-97
lines changed

lib/config/config.go renamed to common/config/config.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@ type Config struct {
1414
LogPath *string `yaml:"LogPath,omitempty"`
1515
LogLevel *int `yaml:"LogLevel,omitempty"`
1616

17-
// TODO: Add all components here
17+
Invoker *InvokerConfig `yaml:"Invoker,omitempty"`
18+
// TODO: Add instances here
1819

20+
DB DBConfig `yaml:"DB"`
1921
// if instance is set up on server, leave connection empty
2022
MasterConnection *Connection `yaml:"MasterConnection,omitempty"`
2123
StorageConnection *Connection `yaml:"StorageConnection,omitempty"`

lib/config/connection.go renamed to common/config/connection.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ type Connection struct {
66
}
77

88
func fillInConnections(config *Config) {
9-
// TODO: Add auto connection creation if master or storage are set up
9+
// TODO: Add auto connection creation if master or storageconn are set up
1010
}

common/config/db.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package config
2+
3+
type DBConfig struct {
4+
Dsn string `yaml:"dsn"`
5+
}

common/config/invoker.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package config
2+
3+
import "github.com/xorcare/pointer"
4+
5+
type InvokerConfig struct {
6+
Instances int `yaml:"Instances"`
7+
QueueSize *int `yaml:"QueueSize,omitempty"` // default is 10
8+
9+
CacheSize uint64 `yaml:"CacheSize"`
10+
CachePath string `yaml:"CachePath"`
11+
}
12+
13+
func fillInInvokerConfig(config *InvokerConfig) {
14+
if config.QueueSize == nil {
15+
config.QueueSize = pointer.Int(10)
16+
}
17+
if len(config.CachePath) == 0 {
18+
panic("No invoker cache path specified")
19+
}
20+
if config.CacheSize == 0 {
21+
panic("No invoker cache size specified")
22+
}
23+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package invokerconn
2+
3+
import (
4+
models2 "testing_system/common/db/models"
5+
)
6+
7+
const (
8+
JobTypeCompile = iota
9+
JobTypeTest
10+
)
11+
12+
type Job struct {
13+
SubmitID uint `json:"submitID" binding:"required"`
14+
JobType int `json:"jobType" binding:"required"`
15+
Test int `json:"test"`
16+
17+
Submit *models2.Submission `json:"submit,omitempty"`
18+
Problem *models2.Problem `json:"problem,omitempty"`
19+
}

common/connectors/storageconn/resourcetype_string.go

Lines changed: 27 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package storageconn
2+
3+
import (
4+
"testing_system/common"
5+
)
6+
7+
type Connector struct {
8+
// TODO: Add storage data loader
9+
}
10+
11+
func NewStorageConnector(ts *common.TestingSystem) *Connector {
12+
return nil
13+
}
14+
15+
func (s *Connector) Download(request Request) *ResponseFiles {
16+
return nil
17+
}
18+
19+
func (s *Connector) Upload(request Request) *Response {
20+
return nil
21+
}
22+
23+
func (s *Connector) Delete(request Request) *Response {
24+
return nil
25+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package storageconn
2+
3+
//go:generate stringer -type=ResourceType
4+
5+
import (
6+
"fmt"
7+
"os"
8+
"path/filepath"
9+
"strconv"
10+
"testing_system/lib/logger"
11+
)
12+
13+
type ResourceType int
14+
15+
const (
16+
SourceCode ResourceType = iota
17+
CompiledBinary
18+
Test
19+
Checker
20+
Interactor
21+
// Will be increased
22+
)
23+
24+
type Request struct {
25+
// Should be always specified
26+
Resource ResourceType `json:"resource"`
27+
28+
// If resource is part of problem, ProblemID is used
29+
ProblemID uint64 `json:"problemID"`
30+
// If resource is part of submit, SubmitID is used
31+
SubmitID uint64 `json:"submitID"`
32+
// If resource is a test, TestID should be specified
33+
TestID uint64 `json:"testID"`
34+
35+
// For any download, BaseFolder should be specified. The files with original filenames will be placed there
36+
BaseFolder string `json:"-"`
37+
38+
// For uploads, FilePath or FilePaths should be specified (depending on whether the resource is single-file or not).
39+
// Filename will be taken from filename inside the path.
40+
FilePath string `json:"-"`
41+
FilePaths []string `json:"-"`
42+
}
43+
44+
func (s *Request) FillBaseFolder(parent string) {
45+
s.BaseFolder = filepath.Join(parent, s.Resource.String())
46+
switch s.Resource {
47+
case SourceCode, CompiledBinary:
48+
s.BaseFolder = filepath.Join(s.BaseFolder, strconv.FormatUint(s.SubmitID, 10))
49+
case Checker, Interactor:
50+
s.BaseFolder = filepath.Join(s.BaseFolder, strconv.FormatUint(s.ProblemID, 10))
51+
case Test:
52+
s.BaseFolder = filepath.Join(s.BaseFolder, fmt.Sprintf("%d-%d", s.SubmitID, s.TestID))
53+
default:
54+
logger.Panic("Can not fill base folder for storageconn request of type %s", s.Resource)
55+
}
56+
}
57+
58+
type Response struct {
59+
R Request
60+
Error error
61+
}
62+
63+
type ResponseFiles struct {
64+
Response
65+
fileNames []string
66+
Size uint64
67+
}
68+
69+
func (r *ResponseFiles) File() string {
70+
return filepath.Join(r.R.BaseFolder, r.fileNames[0])
71+
}
72+
73+
func (r *ResponseFiles) Get(fileName string) (string, bool) {
74+
for _, f := range r.fileNames {
75+
if fileName == f {
76+
return filepath.Join(r.R.BaseFolder, r.fileNames[0]), true
77+
}
78+
}
79+
return "", false
80+
}
81+
82+
func (r *ResponseFiles) CleanUp() {
83+
if r.Error != nil {
84+
return
85+
}
86+
if len(r.R.BaseFolder) == 0 {
87+
return
88+
}
89+
err := os.RemoveAll(r.R.BaseFolder)
90+
if err != nil {
91+
logger.Panic("Can not remove resource folder, error: %s", err.Error())
92+
}
93+
}

db/db.go renamed to common/db/db.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ import (
44
"gorm.io/driver/postgres"
55
"gorm.io/gorm"
66
"log/slog"
7-
"testing_system/db/models"
7+
"testing_system/common/config"
8+
"testing_system/common/db/models"
89
)
910

10-
func NewDb(config Config) (*gorm.DB, error) {
11+
func NewDB(config config.DBConfig) (*gorm.DB, error) {
1112
db, err := gorm.Open(postgres.Open(config.Dsn), &gorm.Config{})
1213
if err != nil {
1314
slog.Error("Can't open database", "dsn", config.Dsn, "err", err)
File renamed without changes.

0 commit comments

Comments
 (0)