Skip to content

Commit cd0d23d

Browse files
grphilArtem Titov
authored andcommitted
Merge pull request #5 from invoker
Invoker base
2 parents 0fa5903 + b183c0a commit cd0d23d

31 files changed

+1281
-19
lines changed

common/config/config.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package config
2+
3+
import (
4+
"os"
5+
6+
"github.com/xorcare/pointer"
7+
"gopkg.in/yaml.v3"
8+
)
9+
10+
type Config struct {
11+
Port int `yaml:"Port"`
12+
Host *string `yaml:"Host,omitempty"` // leave empty for localhost
13+
14+
LogPath *string `yaml:"LogPath,omitempty"`
15+
LogLevel *int `yaml:"LogLevel,omitempty"`
16+
17+
Invoker *InvokerConfig `yaml:"Invoker,omitempty"`
18+
// TODO: Add instances here
19+
20+
DB DBConfig `yaml:"DB"`
21+
// if instance is set up on server, leave connection empty
22+
MasterConnection *Connection `yaml:"MasterConnection,omitempty"`
23+
StorageConnection *Connection `yaml:"StorageConnection,omitempty"`
24+
}
25+
26+
func ReadConfig(configPath string) *Config {
27+
content, err := os.ReadFile(configPath)
28+
if err != nil {
29+
panic(err)
30+
}
31+
32+
config := new(Config)
33+
err = yaml.Unmarshal(content, config)
34+
if err != nil {
35+
panic(err)
36+
}
37+
38+
fillInConfig(config)
39+
40+
return config
41+
}
42+
43+
func fillInConfig(config *Config) {
44+
if config.Host == nil {
45+
config.Host = pointer.String("localhost")
46+
}
47+
48+
fillInConnections(config)
49+
}

common/config/connection.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package config
2+
3+
type Connection struct {
4+
Address string `yaml:"Address"`
5+
// TODO: Add authentification
6+
}
7+
8+
func fillInConnections(config *Config) {
9+
// TODO: Add auto connection creation if master or storageconn are set up
10+
}

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: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package invokerconn
2+
3+
import (
4+
"testing_system/common/db/models"
5+
)
6+
7+
// This will be changed in later commits
8+
9+
const (
10+
JobTypeCompile = iota
11+
JobTypeTest
12+
)
13+
14+
type Job struct {
15+
SubmitID uint `json:"submitID" binding:"required"`
16+
JobType int `json:"jobType" binding:"required"`
17+
Test int `json:"test"`
18+
19+
Submit *models.Submission `json:"submit,omitempty"`
20+
Problem *models.Problem `json:"problem,omitempty"`
21+
}

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: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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, bool) {
70+
if len(r.fileNames) == 0 {
71+
return "", false
72+
}
73+
return filepath.Join(r.R.BaseFolder, r.fileNames[0]), true
74+
}
75+
76+
func (r *ResponseFiles) Get(fileName string) (string, bool) {
77+
for _, f := range r.fileNames {
78+
if fileName == f {
79+
return filepath.Join(r.R.BaseFolder, f), true
80+
}
81+
}
82+
return "", false
83+
}
84+
85+
func (r *ResponseFiles) CleanUp() {
86+
if r.Error != nil {
87+
return
88+
}
89+
if len(r.R.BaseFolder) == 0 {
90+
return
91+
}
92+
err := os.RemoveAll(r.R.BaseFolder)
93+
if err != nil {
94+
logger.Panic("Can not remove resource folder, error: %s", err.Error())
95+
}
96+
}

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)