scheduler
This commit is contained in:
parent
a5a9f4ba50
commit
1a7fc564ff
3 changed files with 10 additions and 8 deletions
|
|
@ -35,31 +35,32 @@ func SchedulerSample(c *core.Context) *core.Response {
|
|||
}
|
||||
c.GetLogger().Info(fmt.Sprintf("[scheduler] enqueued send_email task id=%d (maxRuns=1, thread=0)", item1.ID))
|
||||
|
||||
// ── Enqueue a "flaky_task" (runs 3 times, sequential) ──────────────────
|
||||
// ── Enqueue a "flaky_task" (runs 3 times, concurrent) ──────────────────
|
||||
flakyPayload, _ := json.Marshal(map[string]interface{}{
|
||||
"description": "This task will fail and the error will be recorded",
|
||||
})
|
||||
item2, err := store.Enqueue(ctx, "flaky_task", string(flakyPayload), 0, 3, 0)
|
||||
item2, err := store.Enqueue(ctx, "flaky_task", string(flakyPayload), 0, 3, 1)
|
||||
if err != nil {
|
||||
c.GetLogger().Error(fmt.Sprintf("failed to enqueue flaky_task: %v", err))
|
||||
return c.Response.SetStatusCode(500).Json(`{"message": "failed to enqueue flaky_task"}`)
|
||||
}
|
||||
c.GetLogger().Info(fmt.Sprintf("[scheduler] enqueued flaky_task id=%d (maxRuns=3, thread=0)", item2.ID))
|
||||
c.GetLogger().Info(fmt.Sprintf("[scheduler] enqueued flaky_task id=%d (maxRuns=3, thread=1/concurrent)", item2.ID))
|
||||
|
||||
// ── Enqueue a high-priority "send_email" (repeats indefinitely, concurrent) ──
|
||||
// ── Enqueue a high-priority "send_email" (repeats indefinitely, sequential) ──
|
||||
urgentPayload, _ := json.Marshal(map[string]interface{}{
|
||||
"to": "urgent@example.com",
|
||||
"subject": "URGENT: Priority message",
|
||||
})
|
||||
item3, err := store.Enqueue(ctx, "send_email", string(urgentPayload), 10, -1, 1)
|
||||
item3, err := store.Enqueue(ctx, "send_email", string(urgentPayload), 10, -1, 0)
|
||||
if err != nil {
|
||||
c.GetLogger().Error(fmt.Sprintf("failed to enqueue urgent send_email: %v", err))
|
||||
return c.Response.SetStatusCode(500).Json(`{"message": "failed to enqueue urgent send_email"}`)
|
||||
}
|
||||
c.GetLogger().Info(fmt.Sprintf("[scheduler] enqueued high-priority send_email id=%d (maxRuns=-1/infinite, thread=1/concurrent)", item3.ID))
|
||||
c.GetLogger().Info(fmt.Sprintf("[scheduler] enqueued high-priority send_email id=%d (maxRuns=-1/infinite, thread=0)", item3.ID))
|
||||
|
||||
message := fmt.Sprintf(`{"message": "Enqueued 3 scheduler tasks (send_email x2, flaky_task x1)", "tasks": [%d, %d, %d]}`,
|
||||
message := fmt.Sprintf(`{"message": "Enqueued 3 scheduler tasks (send_email, flaky_task)", "tasks": [%d, %d, %d]}`,
|
||||
item1.ID, item2.ID, item3.ID)
|
||||
|
||||
return c.Response.Json(message)
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -28,7 +28,7 @@ func handleSendEmail(ctx context.Context, item *scheduler.QueueItem) error {
|
|||
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
|
||||
time.Sleep(2000 * time.Millisecond) // simulate I/O
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -30,6 +30,7 @@ func RunAutoMigrations() {
|
|||
// Simple scheduler (sche) tables — enabled when scheduler is active
|
||||
db.AutoMigrate(&scheduler.QueueItem{})
|
||||
db.AutoMigrate(&scheduler.ProcessedItem{})
|
||||
db.AutoMigrate(&scheduler.SchedulerMeta{})
|
||||
|
||||
// End your auto migrations
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue