Skip to content

Commit

Permalink
Add more option when enqueuing a task (retention, timeout, retries) (#64
Browse files Browse the repository at this point in the history
)

* Add more option when enqueuing a task

* Add timeout and max retries as env variables
  • Loading branch information
robertjndw authored Feb 19, 2024
1 parent 921f6f1 commit 48076c2
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
7 changes: 6 additions & 1 deletion HadesAPI/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,20 @@ type HadesAPIConfig struct {
RedisConfig utils.RedisConfig
AuthKey string `env:"AUTH_KEY"`
PrometheusAddress string `env:"PROMETHEUS_ADDRESS" envDefault:""`
// How long the task should be kept for monitoring
RetentionTime uint `env:"RETENTION_IN_MIN" envDefault:"30"`
MaxRetries uint `env:"MAX_RETRIES" envDefault:"3"`
Timeout uint `env:"TIMEOUT_IN_MIN" envDefault:"0"`
}

var cfg HadesAPIConfig

func main() {
if is_debug := os.Getenv("DEBUG"); is_debug == "true" {
log.SetLevel(log.DebugLevel)
log.Warn("DEBUG MODE ENABLED")
}

var cfg HadesAPIConfig
utils.LoadConfig(&cfg)

redis_opts := asynq.RedisClientOpt{Addr: cfg.RedisConfig.Addr, Password: cfg.RedisConfig.Pwd}
Expand Down
9 changes: 8 additions & 1 deletion HadesAPI/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"encoding/json"
"net/http"
"time"

"github.com/Mtze/HadesCI/shared/payload"
"github.com/Mtze/HadesCI/shared/utils"
Expand Down Expand Up @@ -45,7 +46,13 @@ func AddBuildToQueue(c *gin.Context) {

task := asynq.NewTask(payload.Name, json_payload)
queuePriority := utils.PrioFromInt(payload.Priority)
info, err := AsynqClient.Enqueue(task, asynq.Queue(queuePriority))
info, err := AsynqClient.Enqueue(
task,
asynq.Queue(queuePriority),
asynq.Retention(time.Duration(cfg.RetentionTime)*time.Minute), // Keep the result for 30 minutes
asynq.Timeout(time.Duration(cfg.Timeout)*time.Minute), // Timeout for each queued task
asynq.MaxRetry(int(cfg.MaxRetries)), // Retry times
)
if err != nil {
log.WithError(err).Error("Failed to enqueue build")
c.String(http.StatusInternalServerError, "Failed to enqueue build")
Expand Down
3 changes: 1 addition & 2 deletions HadesScheduler/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (

"github.com/Mtze/HadesCI/shared/payload"
"github.com/Mtze/HadesCI/shared/utils"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/mount"
"github.com/docker/docker/client"
Expand Down Expand Up @@ -142,7 +141,7 @@ func (d Scheduler) executeStep(ctx context.Context, client *client.Client, step
}

// Start the container
err = client.ContainerStart(ctx, resp.ID, types.ContainerStartOptions{})
err = client.ContainerStart(ctx, resp.ID, container.StartOptions{})
if err != nil {
log.WithError(err).Errorf("Failed to start container %s with ID %s", step.Image, resp.ID)
return err
Expand Down

0 comments on commit 48076c2

Please sign in to comment.