Files
dagu/internal/queue/queue.go

121 lines
4.6 KiB
Go

// Copyright (C) 2026 Yota Hamada
// SPDX-License-Identifier: GPL-3.0-or-later
package queue
import (
"context"
"errors"
"time"
"github.com/dagucloud/dagu/v2/internal/ir"
"github.com/dagucloud/dagu/v2/internal/pagination"
)
// Errors for the queue
var (
ErrQueueItemNotFound = errors.New("queue item not found")
)
// QueueStore provides an interface for interacting with the underlying database
// for storing and retrieving queued dag-run items.
type QueueStore interface {
// Enqueue adds an item to the queue
Enqueue(ctx context.Context, name string, priority QueuePriority, dagRun ir.DAGRunRef) error
// DequeueByDAGRunID retrieves items from the queue by dag-run reference and removes them
DequeueByDAGRunID(ctx context.Context, name string, dagRun ir.DAGRunRef) ([]QueuedItemData, error)
// DeleteByItemIDs removes the exact queued items identified by their queue item IDs.
DeleteByItemIDs(ctx context.Context, name string, itemIDs []string) (int, error)
// Len returns the number of items in the queue
Len(ctx context.Context, name string) (int, error)
// List returns all items in the queue with the given name
List(ctx context.Context, name string) ([]QueuedItemData, error)
// GetByItemID returns an exact item from the named queue.
GetByItemID(ctx context.Context, name, itemID string) (QueuedItemData, error)
// ListCursor returns one forward-only page of queued items for a specific queue.
ListCursor(ctx context.Context, name, cursor string, limit int) (pagination.CursorResult[QueuedItemData], error)
// Revision returns a value that changes when the ordered queue membership changes.
Revision(ctx context.Context, name string) (int64, error)
// All returns all items in the queue
All(ctx context.Context) ([]QueuedItemData, error)
// ListByDAGName returns all items that has a specific DAG name
ListByDAGName(ctx context.Context, name, dagName string) ([]QueuedItemData, error)
// QueueList lists all queue names that have at least one item in the queue
QueueList(ctx context.Context) ([]string, error)
// Watcher returns a QueueWatcher for the queue data
QueueWatcher(ctx context.Context) QueueWatcher
}
// Summary is one queue that holds queued items: how many wait, and the
// next of them in line, by its item ID and when it was queued.
type Summary struct {
Name string
Queued int
Next string
NextQueuedAt time.Time
}
// Summarizer sums up the queues that hold queued items, in name order,
// without reading the items.
type Summarizer interface {
Summaries(ctx context.Context) ([]Summary, error)
}
// QueueWatcher watches the queue state
type QueueWatcher interface {
// Start start swatching queue data and signal when a queue state changed
Start(ctx context.Context) (<-chan struct{}, error)
// Stop stops watching queue data
Stop(ctx context.Context)
}
// QueuePriority represents the priority of a queued item
type QueuePriority int
const (
QueuePriorityHigh QueuePriority = iota
QueuePriorityLow
)
// PriorityOf returns the queue priority of a run. A backfill run waits
// behind every other run of its queue; the others keep their order among
// themselves.
func PriorityOf(status *ir.DAGRunStatus) QueuePriority {
if status != nil && status.BackfillID != "" {
return QueuePriorityLow
}
return QueuePriorityHigh
}
// QueuedItemData represents a dag-run reference that is queued for execution.
type QueuedItemData interface {
// ID returns the ID of the queued item
ID() string
// Data returns the data of the queued item
Data() (*ir.DAGRunRef, error)
}
// RetryAdmissionIdentity binds a queue item to one persisted checkpoint and
// retry generation. Legacy items do not carry this identity.
type RetryAdmissionIdentity struct {
ID string `json:"id"`
AttemptID string `json:"attemptId"`
AttemptKey string `json:"attemptKey,omitempty"`
QueuedAt string `json:"queuedAt"`
Root ir.DAGRunRef `json:"root,omitzero"`
}
// RetryAdmissionStore atomically creates at most one item for an identity.
// EnsureRetryAdmission must not replace an existing item with different data.
// Success requires the item to be durably discoverable through queue listing.
// The caller serializes publication and acknowledgement with the latest
// attempt's status transition; this method alone cannot deduplicate a dequeue.
type RetryAdmissionStore interface {
EnsureRetryAdmission(context.Context, string, QueuePriority, ir.DAGRunRef, RetryAdmissionIdentity) (bool, error)
}
// RetryAdmissionItem exposes the checkpoint identity on a recoverable item.
type RetryAdmissionItem interface {
RetryAdmission() *RetryAdmissionIdentity
}