-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
293 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
// Use IntelliSense to learn about possible attributes. | ||
// Hover to view descriptions of existing attributes. | ||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 | ||
"version": "0.2.0", | ||
"configurations": [ | ||
{ | ||
"name": "Launch Server", | ||
"type": "go", | ||
"request": "launch", | ||
"mode": "auto", | ||
"program": "application/server/main.go" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,5 @@ | ||
package main | ||
|
||
import ( | ||
asynqAgent "github.com/OJ-lab/oj-lab-services/core/agent/asynq" | ||
"github.com/OJ-lab/oj-lab-services/service/business" | ||
) | ||
|
||
func main() { | ||
asynqAgent.RunSecheduler( | ||
asynqAgent.ScheduleTask{ | ||
Cronspec: "@every 1s", | ||
Task: business.NewTaskJudgerTrackAllState(), | ||
}, | ||
) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package business | ||
|
||
import ( | ||
"context" | ||
|
||
redisAgent "github.com/OJ-lab/oj-lab-services/core/agent/redis" | ||
"github.com/OJ-lab/oj-lab-services/service/model" | ||
"github.com/redis/go-redis/v9" | ||
) | ||
|
||
const ( | ||
streamName = "oj_lab_judge_stream" | ||
consumerGroupName = "oj_lab_judge_stream_consumer_group" | ||
defaultConsumerName = "oj_lab_judge_stream_consumer_default" | ||
) | ||
|
||
func init() { | ||
redisAgent := redisAgent.GetDefaultRedisClient() | ||
_, err := redisAgent.XGroupCreateMkStream(context.Background(), streamName, consumerGroupName, "0").Result() | ||
if err != nil && err != redis.Nil && err.Error() != "BUSYGROUP Consumer Group name already exists" { | ||
panic(err) | ||
} | ||
} | ||
|
||
func AddTaskToStream(ctx context.Context, task *model.JudgeTask) (*string, error) { | ||
redisAgent := redisAgent.GetDefaultRedisClient() | ||
id, err := redisAgent.XAdd(ctx, &redis.XAddArgs{ | ||
Stream: streamName, | ||
Values: task.ToStringMap(), | ||
}).Result() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &id, err | ||
} | ||
|
||
func GetTaskFromStream(ctx context.Context, consumer string) (*model.JudgeTask, error) { | ||
redisAgent := redisAgent.GetDefaultRedisClient() | ||
if consumer == "" { | ||
consumer = defaultConsumerName | ||
} | ||
result, err := redisAgent.XReadGroup(ctx, &redis.XReadGroupArgs{ | ||
Group: consumerGroupName, | ||
Consumer: consumer, | ||
Streams: []string{streamName, ">"}, | ||
Count: 1, | ||
Block: -1, | ||
}).Result() | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
if len(result) == 0 { | ||
return nil, nil | ||
} | ||
|
||
task := model.JudgeTask{} | ||
for _, message := range result[0].Messages { | ||
task = *model.JudgeTaskFromMap(message.Values) | ||
task.RedisStreamID = &message.ID | ||
} | ||
|
||
return &task, nil | ||
} | ||
|
||
func AckTaskFromStream(ctx context.Context, consumer string, streamID string) error { | ||
redisAgent := redisAgent.GetDefaultRedisClient() | ||
if consumer == "" { | ||
consumer = defaultConsumerName | ||
} | ||
_, err := redisAgent.XAck(ctx, streamName, consumerGroupName, streamID).Result() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
// ! Deprected | ||
|
||
package business | ||
|
||
import ( | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.