Wires the scheduler into the cup application following the exact conventions of the Asynq queue integration. A new testmac/config/scheduler.go file provides GetSchedulerConfig() with enable flag, polling interval, and rate limit. A new testmac/register-scheduler.go file registers task handlers (send_email and flaky_task as samples) on the Schedulermux, creates the DB-backed store via core.ResolveGorm(), and launches the scheduler in a goroutine — only when EnableScheduler: true. The scheduler tables are migrated alongside other models in run-auto-migrations.go via db.AutoMigrate(&scheduler.QueueItem{}, &scheduler.ProcessedItem{}).
This commit is contained in:
parent
f4476d3e89
commit
3d051e9617
6 changed files with 203 additions and 0 deletions
71
register-scheduler.go
Normal file
71
register-scheduler.go
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
// 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 main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"git.smarteching.com/goffee/core"
|
||||
"git.smarteching.com/goffee/core/scheduler"
|
||||
"git.smarteching.com/goffee/cup/config"
|
||||
)
|
||||
|
||||
// ─── Scheduler task handlers ─────────────────────────────────────────────────
|
||||
|
||||
// handleSendEmail processes a send_email scheduler task.
|
||||
func handleSendEmail(ctx context.Context, item *scheduler.QueueItem) error {
|
||||
var payload struct {
|
||||
To string `json:"to"`
|
||||
Subject string `json:"subject"`
|
||||
}
|
||||
if err := json.Unmarshal([]byte(item.Payload), &payload); err != nil {
|
||||
return fmt.Errorf("invalid send_email payload: %w", err)
|
||||
}
|
||||
log.Printf("[scheduler] [send_email] → to=%s subject=%q", payload.To, payload.Subject)
|
||||
time.Sleep(50 * time.Millisecond) // simulate I/O
|
||||
return nil
|
||||
}
|
||||
|
||||
// handleFlakyTask demonstrates a task that always fails.
|
||||
func handleFlakyTask(ctx context.Context, item *scheduler.QueueItem) error {
|
||||
log.Printf("[scheduler] [flaky_task] attempting... payload=%s", item.Payload)
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
return fmt.Errorf("external API returned 503 — simulated failure for demo")
|
||||
}
|
||||
|
||||
// ─── Registration ────────────────────────────────────────────────────────────
|
||||
|
||||
// Register simple scheduler
|
||||
func registerScheduler() {
|
||||
|
||||
var sched = new(core.Schedulermux)
|
||||
sched.SchedulerInit()
|
||||
|
||||
//########################################
|
||||
//# scheduler task registration #####
|
||||
//########################################
|
||||
|
||||
// Register your scheduler task handlers here ...
|
||||
sched.AddWork("send_email", handleSendEmail)
|
||||
sched.AddWork("flaky_task", handleFlakyTask)
|
||||
|
||||
//########################################
|
||||
// Set the store (DB-backed) and start
|
||||
//########################################
|
||||
|
||||
// Create the store using the application's GORM DB
|
||||
st, err := scheduler.NewStore(core.ResolveGorm())
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("scheduler: failed to create store: %v", err))
|
||||
}
|
||||
sched.SetStore(st)
|
||||
|
||||
// Start scheduler server in background, DO NOT TOUCH
|
||||
go sched.RunScheduler(config.GetSchedulerConfig())
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue