-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
222 lines (184 loc) · 5.01 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package main
import (
"encoding/json"
"net/http"
"os"
"strconv"
"github.com/gin-gonic/gin"
prettyconsole "github.com/thessem/zap-prettyconsole"
"go.uber.org/zap"
)
type Test struct {
ID int `json:"id,omitempty"`
Name string `json:"name"`
Input map[string]string `json:"input"`
ExpectedOutput interface{} `json:"expected_output"`
ExpectedStatus int `json:"expected_status"`
Timeout int `json:"timeout"`
}
type Results struct {
ID int `json:"id"`
Status int `json:"status"`
ExpectedStatus int `json:"expected_status"`
ActualStatus int `json:"actual_status"`
ExpectedOutput interface{} `json:"expected_output"`
ActualOutput interface{} `json:"actual_output"`
Timeout int `json:"timeout"`
ActualDuration int `json:"actual_duration"`
Error string `json:"error"`
}
type Handler struct {
log *zap.Logger
}
func NewHandler(log *zap.Logger) *Handler {
return &Handler{
log: log,
}
}
var (
testConfig []Test
log *zap.Logger
currentTest int = -1
)
func init() {
// Initialize logger
var err error
if os.Getenv("ENV") == "local" {
log = prettyconsole.NewLogger(zap.DebugLevel)
} else {
log, err = zap.NewProduction()
if err != nil {
panic("Failed to initialize logger: " + err.Error())
}
}
log.Info(os.Getenv("RUNPOD_TEST"))
if os.Getenv("RUNPOD_TEST") == "true" {
testFilePath := os.Getenv("RUNPOD_TEST_FILE")
data, err := os.ReadFile(testFilePath)
if err != nil {
log.Fatal("Failed to read runpod.tests.json",
zap.Error(err))
}
// Parse JSON into testConfig
if err := json.Unmarshal(data, &testConfig); err != nil {
log.Fatal("Failed to parse runpod.tests.json",
zap.Error(err))
}
log.Info("Parsed test config", zap.Any("testConfig", testConfig))
for i, test := range testConfig {
test.ID = i
}
}
}
func (h *Handler) Health(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"status": "healthy",
})
}
// RunInference handles the main inference request
func (h *Handler) RunInference(c *gin.Context) {
var request struct {
Input map[string]interface{} `json:"input"`
}
if err := c.ShouldBindJSON(&request); err != nil {
h.log.Error("Failed to parse request", zap.Error(err))
c.JSON(http.StatusBadRequest, gin.H{
"error": "Invalid request format",
})
return
}
// TODO: Add your inference logic here
c.JSON(http.StatusOK, gin.H{
"status": "success",
"output": request.Input,
})
}
// GetStatus returns the status of a job
func (h *Handler) JobTake(c *gin.Context) {
h.log.Info("Job take", zap.Int("current_test", currentTest))
currentTest++
nextTestPayload := testConfig[currentTest]
h.log.Info("Job take", zap.Any("next_test_payload", nextTestPayload))
c.JSON(http.StatusOK, gin.H{
"delayTime": 0,
"error": "",
"executionTime": nextTestPayload.Timeout,
"id": currentTest,
"input": nextTestPayload.Input,
"retries": 0,
"status": 200,
})
}
// CancelJob cancels a running job
func (h *Handler) JobDone(c *gin.Context) {
jobID := c.Param("id")
h.log.Info("Job done", zap.String("job_id", jobID))
id, err := strconv.Atoi(jobID)
if err != nil {
h.log.Error("Failed to parse job ID", zap.Error(err))
c.JSON(http.StatusBadRequest, gin.H{
"error": "Invalid job ID",
})
return
}
// Get test case for this job ID
if id >= len(testConfig) {
h.log.Error("Job ID out of range", zap.Int("id", id))
c.JSON(http.StatusBadRequest, gin.H{
"error": "Invalid job ID",
})
return
}
test := testConfig[id]
var result interface{}
if err := c.BindJSON(&result); err != nil {
h.log.Error("Failed to parse request body", zap.Error(err))
c.JSON(http.StatusBadRequest, gin.H{
"error": "Invalid request body",
})
return
}
h.log.Info("Job done", zap.Any("actual result", result), zap.Any("expected result", test))
// Compare results
// testResult := Results{
// ID: id,
// ExpectedStatus: test.ExpectedStatus,
// ActualStatus: int(result["status"].(float64)),
// ExpectedOutput: test.ExpectedOutput,
// ActualOutput: result["output"],
// Timeout: test.Timeout,
// ActualDuration: int(result["executionTime"].(float64)),
// }
// if err, ok := result["error"]; ok {
// testResult.Error = err.(string)
// }
// TODO: Implement job cancellation logic
c.JSON(http.StatusOK, gin.H{
"id": jobID,
"status": "cancelled",
"message": "Job successfully cancelled",
})
}
func main() {
defer log.Sync()
log.Info("Starting server")
gin.SetMode(gin.ReleaseMode)
r := gin.New()
r.Use(gin.Recovery())
h := NewHandler(log)
r.GET("/health", h.Health)
workerAuthorized := r.Group("/v2/:model")
{
workerAuthorized.GET("/job-take/:pod_id", h.JobTake)
workerAuthorized.POST("/job-done/:pod_id/:id", h.JobDone)
}
// Get port from environment variable or use default
port := os.Getenv("PORT")
if port == "" {
port = "19981"
}
// Start server
if err := r.Run(":" + port); err != nil {
log.Fatal("Failed to start server", zap.Error(err))
}
}