52 lines
1.2 KiB
Go
52 lines
1.2 KiB
Go
// Copyright (c) 2025 Zeni Kim <zenik@smarteching.com>
|
|
// Use of this source code is governed by MIT-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package controllers
|
|
|
|
import (
|
|
"encoding/json"
|
|
"log"
|
|
"time"
|
|
|
|
"git.smarteching.com/goffee/core"
|
|
"git.smarteching.com/goffee/cup/workers"
|
|
"github.com/hibiken/asynq"
|
|
)
|
|
|
|
// Make samples queues
|
|
func Queuesample(c *core.Context) *core.Response {
|
|
|
|
// Get client queue asynq
|
|
client := c.GetQueueClient()
|
|
|
|
// Create a task with typename and payload.
|
|
payload, err := json.Marshal(workers.EmailTaskPayload{UserID: 42})
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
t1 := asynq.NewTask(workers.TypeWelcomeEmail, payload)
|
|
|
|
t2 := asynq.NewTask(workers.TypeReminderEmail, payload)
|
|
|
|
// Process the task immediately.
|
|
info, err := client.Enqueue(t1)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
log.Printf(" [*] Successfully enqueued task: %+v", info)
|
|
|
|
// Process 2 task 1 min later.
|
|
for i := 1; i < 3; i++ {
|
|
info, err = client.Enqueue(t2, asynq.ProcessIn(1*time.Minute))
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
log.Printf(" [*] Successfully enqueued task: %+v", info)
|
|
}
|
|
|
|
message := "{\"message\": \"Task queued\"}"
|
|
return c.Response.Json(message)
|
|
|
|
}
|