Skip to content

Commit a0fdd1e

Browse files
Refactor main function: replace Redis client with server initialization and add video handler for processing tasks
1 parent 4d374ba commit a0fdd1e

2 files changed

Lines changed: 50 additions & 6 deletions

File tree

cmd/worker/main.go

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,29 @@ import (
44
"log"
55

66
"github.com/Prateet-Github/worker-go/internal/config"
7+
"github.com/Prateet-Github/worker-go/internal/handlers"
78
"github.com/Prateet-Github/worker-go/internal/queue"
9+
10+
"github.com/hibiken/asynq"
811
)
912

1013
func main() {
1114
cfg := config.Load()
1215

13-
client := queue.NewClient(cfg)
14-
defer client.Close()
16+
server := queue.NewServer(cfg)
1517

16-
if err := queue.Ping(client); err != nil {
17-
log.Fatal("Redis connection failed:", err)
18-
}
18+
videoHandler := handlers.NewVideoHandler()
19+
20+
mux := asynq.NewServeMux()
21+
22+
mux.HandleFunc(
23+
queue.TypeProcessVideo,
24+
videoHandler.ProcessVideo,
25+
)
1926

20-
log.Println("Connected to Redis")
2127
log.Println("Worker started")
28+
29+
if err := server.Run(mux); err != nil {
30+
log.Fatal(err)
31+
}
2232
}

internal/handlers/video.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package handlers
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
"log"
7+
8+
"github.com/Prateet-Github/worker-go/internal/queue"
9+
"github.com/hibiken/asynq"
10+
)
11+
12+
type VideoHandler struct{}
13+
14+
func NewVideoHandler() *VideoHandler {
15+
return &VideoHandler{}
16+
}
17+
18+
func (h *VideoHandler) ProcessVideo(
19+
ctx context.Context,
20+
task *asynq.Task,
21+
) error {
22+
23+
var payload queue.VideoTask
24+
25+
if err := json.Unmarshal(task.Payload(), &payload); err != nil {
26+
return err
27+
}
28+
29+
log.Println("Processing video...")
30+
log.Println("Video ID:", payload.VideoID)
31+
log.Println("S3 Key:", payload.S3Key)
32+
33+
return nil
34+
}

0 commit comments

Comments
 (0)