Skip to content

Commit

Permalink
Merge branch 'main' into staging
Browse files Browse the repository at this point in the history
  • Loading branch information
karootplx authored Oct 17, 2024
2 parents 7ec31bc + 407a99b commit 3faf8be
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 23 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Dojo Subnet API
# Dojo Worker API

Repository for our Dojo Subnet APIs. Check request.REST file for developed APIs to test out.(make sure that REST client extension is already installed)
Extension ID: humao.rest-client
Expand Down
5 changes: 3 additions & 2 deletions cmd/seed/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ package main

import (
"context"
"dojo-api/db"
"dojo-api/pkg/orm"
"encoding/json"
"io"
"os"
"path/filepath"
"time"

"dojo-api/db"
"dojo-api/pkg/orm"

"github.com/rs/zerolog/log"
"github.com/steebchen/prisma-client-go/runtime/types"
)
Expand Down
15 changes: 8 additions & 7 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@ package main

import (
"context"
"dojo-api/pkg/api"
"dojo-api/pkg/cache"
"dojo-api/pkg/orm"
"dojo-api/utils"
"net/http"
"os"
"os/signal"
"strings"
"syscall"
"time"

"dojo-api/pkg/api"
"dojo-api/pkg/cache"
"dojo-api/pkg/orm"
"dojo-api/utils"

_ "dojo-api/docs"

"github.com/gin-contrib/cors"
Expand All @@ -23,9 +24,9 @@ import (
ginSwagger "github.com/swaggo/gin-swagger"
)

// @title Dojo Worker API
// @version 1.0
// @description This is the worker API for the Dojo project.
// @title Dojo Worker API
// @version 1.0
// @description This is the worker API for the Dojo project.

func main() {
loadEnvVars()
Expand Down
4 changes: 2 additions & 2 deletions pkg/api/controllers.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,14 +173,14 @@ func CreateTasksController(c *gin.Context) {
}

taskService := task.NewTaskService()
tasks, errors := taskService.CreateTasks(requestBody, minerUser.ID)
tasks, errors := taskService.CreateTasksWithTimeout(requestBody, minerUser.ID, 60*time.Second)

log.Info().Msg("Tasks created successfully")
if len(tasks) == 0 {
c.AbortWithStatusJSON(http.StatusBadRequest, defaultErrorResponse(errors))
return
}

log.Info().Msg("Tasks created successfully")
taskIds := make([]string, 0, len(tasks))
for _, task := range tasks {
taskIds = append(taskIds, task.ID)
Expand Down
4 changes: 2 additions & 2 deletions pkg/blockchain/substrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ func (s *SubstrateService) GetMaxUID(subnetId int) (int, error) {
return 0, err
}

s.cache.SetWithExpire(CacheKeyMaxUID, valueStr, 24*7*time.Hour)
s.cache.SetWithExpire(CacheKeyMaxUID, valueStr, 5*BlockTimeInSeconds*time.Second)
return maxUID, nil
}

Expand Down Expand Up @@ -419,7 +419,7 @@ func (s *SubstrateService) RuntimeSpec() (*RuntimeSpec, error) {
return nil, err
}

s.cache.SetWithExpire(CacheKeyRuntimeSpec, string(body), 12*time.Hour)
s.cache.SetWithExpire(CacheKeyRuntimeSpec, string(body), 24*time.Hour)
return &runtimeSpec, nil
}

Expand Down
54 changes: 45 additions & 9 deletions pkg/task/task_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@ package task

import (
"context"
"dojo-api/db"
"dojo-api/pkg/orm"
"dojo-api/pkg/sandbox"
"dojo-api/utils"
"encoding/json"
"errors"
"fmt"
Expand All @@ -16,6 +12,11 @@ import (
"strconv"
"time"

"dojo-api/db"
"dojo-api/pkg/orm"
"dojo-api/pkg/sandbox"
"dojo-api/utils"

"github.com/gin-gonic/gin"
"github.com/rs/zerolog/log"
)
Expand Down Expand Up @@ -201,11 +202,41 @@ func IsValidCriteriaType(criteriaType CriteriaType) bool {
}
}

// create task
func (s *TaskService) CreateTasks(request CreateTaskRequest, minerUserId string) ([]*db.TaskModel, []error) {
ctxWithTimeout, cancel := context.WithTimeout(context.Background(), 3*time.Second)
func (s *TaskService) CreateTasksWithTimeout(request CreateTaskRequest, minerUserId string, timeout time.Duration) ([]*db.TaskModel, []error) {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()

type result struct {
tasks []*db.TaskModel
errs []error
}

resultChan := make(chan result, 1)

go func() {
tasks, errs := s.CreateTasks(ctx, request, minerUserId)
resultChan <- result{tasks: tasks, errs: errs}
}()

select {
case <-ctx.Done():
if ctx.Err() == context.DeadlineExceeded {
log.Error().Dur("timeout", timeout).Msg("CreateTasks timed out due to deadline")
return nil, []error{fmt.Errorf("operation timed out after %v", timeout)}
}
log.Error().Err(ctx.Err()).Msg("Context canceled while creating tasks")
return nil, []error{ctx.Err()}
case res := <-resultChan:
if len(res.tasks) == 0 && len(res.errs) == 0 {
log.Warn().Msg("No tasks created and no errors reported")
return nil, []error{fmt.Errorf("no tasks were created and no errors were reported")}
}
return res.tasks, res.errs
}
}

// create task
func (s *TaskService) CreateTasks(ctx context.Context, request CreateTaskRequest, minerUserId string) ([]*db.TaskModel, []error) {
tasks := make([]*db.TaskModel, 0)
errors := make([]error, 0)

Expand Down Expand Up @@ -249,7 +280,7 @@ func (s *TaskService) CreateTasks(request CreateTaskRequest, minerUserId string)
taskToCreate.TotalReward = &request.TotalRewards
}

task, err := taskORM.CreateTask(ctxWithTimeout, taskToCreate, minerUserId)
task, err := taskORM.CreateTask(ctx, taskToCreate, minerUserId)
if err != nil {
log.Error().Msgf("Error creating task: %v", err)
errors = append(errors, err)
Expand Down Expand Up @@ -510,7 +541,6 @@ func ValidateTaskData(taskData TaskData) error {
return fmt.Errorf("invalid completion format: %v", taskresponse.Completion)
}
}

}

if len(taskData.Criteria) == 0 {
Expand Down Expand Up @@ -684,6 +714,12 @@ func (t *TaskService) GetCompletedTaskMap(ctx context.Context, workerId string)
}

func ProcessRequestBody(c *gin.Context) (CreateTaskRequest, error) {
// set max memory to 64 MB
if err := c.Request.ParseMultipartForm(64 << 20); err != nil {
log.Error().Err(err).Msg("Failed to parse multipart form")
return CreateTaskRequest{}, fmt.Errorf("failed to parse form: %w", err)
}

var reqbody CreateTaskRequest
title := c.PostForm("title")
body := c.PostForm("body")
Expand Down

0 comments on commit 3faf8be

Please sign in to comment.