-
Notifications
You must be signed in to change notification settings - Fork 6
/
simulate.go
69 lines (58 loc) · 1.5 KB
/
simulate.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
package cmd
import (
"encoding/json"
"log"
"os"
"github.com/hibiken/asynq"
"github.com/joho/godotenv"
"github.com/mauricioabreu/mosaic-video/internal/config"
"github.com/mauricioabreu/mosaic-video/internal/mosaic"
"github.com/mauricioabreu/mosaic-video/internal/worker"
"github.com/spf13/cobra"
)
func Simulate() *cobra.Command {
return &cobra.Command{
Use: "simulate",
Short: "Simulate a mosaic processing using a queue",
Run: func(cmd *cobra.Command, args []string) {
if err := godotenv.Load(".env"); err != nil {
log.Println("Could not load .env file")
}
cfg, err := config.NewConfig()
if err != nil {
log.Fatal(err)
}
redisAddress := cfg.Redis.Host + ":" + cfg.Redis.Port
client := asynq.NewClient(asynq.RedisClientOpt{Addr: redisAddress})
defer client.Close()
if err := enqueueStartMosaicTask(client, cfg); err != nil {
log.Println(err)
}
},
}
}
func enqueueStartMosaicTask(client *asynq.Client, cfg *config.Config) error {
fileData, err := os.ReadFile("./testing/tasks.json")
if err != nil {
return err
}
var mosaics []mosaic.Mosaic
if err := json.Unmarshal(fileData, &mosaics); err != nil {
return err
}
// Enqueue a task for each mosaic
for _, m := range mosaics {
payload, err := json.Marshal(worker.StartMosaicPayload{Mosaic: m})
if err != nil {
return err
}
_, err = client.Enqueue(
asynq.NewTask(worker.TypeStartMosaic, payload),
asynq.MaxRetry(cfg.MaxRetriesTasks),
)
if err != nil {
return err
}
}
return nil
}