add base queue system
This commit is contained in:
parent
d431963181
commit
d413d7def4
7 changed files with 141 additions and 0 deletions
52
controllers/queuesample.go
Normal file
52
controllers/queuesample.go
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
// 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)
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue