{"modulePath":"github.com/flant/shell-operator","version":"v1.21.5","isLatest":true,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package queue\n\n## Constants\n\n```go\nconst MainQueueName = \"main\"\n```\n\n## Variables\n\n```go\nvar (\n\tDefaultWaitLoopCheckInterval    = 125 * time.Millisecond\n\tDefaultDelayOnQueueIsEmpty      = 250 * time.Millisecond\n\tDefaultInitialDelayOnFailedTask = 5 * time.Second\n\tDefaultDelayOnRepeat            = 25 * time.Millisecond\n)\n```\n\n## Types\n\n```go\ntype QueueStatusType int\n```\nQueueStatusType represents the type of queue status\n\n```go\nconst (\n\tQueueStatusUnknown QueueStatusType = iota\n\tQueueStatusIdle\n\tQueueStatusNoHandlerSet\n\tQueueStatusRunningTask\n\tQueueStatusStop\n\tQueueStatusRepeatTask\n\tQueueStatusSleeping\n\tQueueStatusWaiting\n)\n```\n\n```go\nfunc (qst QueueStatusType) String() string\n```\nString returns a human-readable representation of the queue status type\n\n```go\ntype TaskCounter struct {\n\t// contains filtered or unexported fields\n}\n```\n\n```go\nfunc NewTaskCounter(name string, countableTypes map[task.TaskType]struct{}, metricStorage metricsstorage.Storage) *TaskCounter\n```\n\n```go\nfunc (tc *TaskCounter) Add(task task.Task)\n```\n\n```go\nfunc (tc *TaskCounter) GetReachedCap() map[string]struct{}\n```\n\n```go\nfunc (tc *TaskCounter) IsAnyCapReached() bool\n```\n\n```go\nfunc (tc *TaskCounter) Remove(task task.Task)\n```\n\n```go\nfunc (tc *TaskCounter) ResetReachedCap()\n```\n\n```go\nfunc (tc *TaskCounter) UpdateHookMetricsFromSnapshot(hookCounts map[string]uint)\n```\nUpdateHookMetricsFromSnapshot updates metrics for all hooks based on a snapshot of hook counts. Only hooks with task count above the threshold are published to avoid metric cardinality explosion.\n\n```go\ntype TaskQueue struct {\n\tName    string\n\tHandler func(ctx context.Context, t task.Task) TaskResult\n\n\t// Timing settings.\n\tWaitLoopCheckInterval time.Duration\n\tDelayOnQueueIsEmpty   time.Duration\n\tDelayOnRepeat         time.Duration\n\tExponentialBackoffFn  func(failureCount int) time.Duration\n\t// contains filtered or unexported fields\n}\n```\n\n```go\nfunc NewTasksQueue(name string, metricStorage metricsstorage.Storage, opts ...TaskQueueOption) *TaskQueue\n```\nNewTasksQueue creates a new TaskQueue with the provided options\n\n```go\nfunc (q *TaskQueue) AddAfter(id string, tasks ...task.Task)\n```\nAddAfter inserts a task after the task with specified id.\n\n```go\nfunc (q *TaskQueue) AddBefore(id string, newTask task.Task)\n```\nAddBefore inserts a task before the task with specified id.\n\n```go\nfunc (q *TaskQueue) AddFirst(tasks ...task.Task)\n```\nAddFirst adds new head element.\n\n```go\nfunc (q *TaskQueue) AddLast(tasks ...task.Task)\n```\naddLast adds a new tail element. It implements the merging logic for HookRun tasks by scanning the whole queue.\n\n```go\nfunc (q *TaskQueue) CancelTaskDelay()\n```\nCancelTaskDelay breaks wait loop. Useful to break the possible long sleep delay.\n\n```go\nfunc (q *TaskQueue) DeleteFunc(fn func(task.Task) bool)\n```\nDeleteFunc runs fn on every task and removes each task for which fn returns false.\n\n```go\nfunc (q *TaskQueue) Get(id string) task.Task\n```\nGet returns a task by id.\n\n```go\nfunc (q *TaskQueue) GetFirst() task.Task\n```\nGetFirst returns a head element.\n\n```go\nfunc (q *TaskQueue) GetLast() task.Task\n```\nGetLast returns a tail element.\n\n```go\nfunc (q *TaskQueue) GetSnapshot() []task.Task\n```\nGetSnapshot returns a copy of all tasks in the queue. This is useful for external iteration or processing without holding locks.\n\nThe returned slice is a snapshot at the time of the call and will not reflect subsequent changes to the queue.\n\n```go\nfunc (q *TaskQueue) GetStatus() string\n```\n\n```go\nfunc (q *TaskQueue) GetStatusType() QueueStatusType\n```\n\n```go\nfunc (q *TaskQueue) IterateSnapshot(doFn func(task.Task))\n```\nIterateSnapshot creates a snapshot of all tasks and iterates over the copy. This is safer than Iterate() when you need to call queue methods inside the callback, as no locks are held during callback execution.\n\nNote: The snapshot may become stale during iteration if tasks are added/removed by other goroutines or by the callback itself.\n\nUse this method when:\n\n  - You need to call queue methods inside the callback (Add, Length, Filter, etc.)\n  - You need to process tasks asynchronously\n  - Safety is more important than performance\n\nMemory overhead: O(n) where n is the number of tasks in the queue.\n\n```go\nfunc (q *TaskQueue) Length() int\n```\n\n```go\nfunc (q *TaskQueue) MeasureActionTime(action string) func()\n```\nMeasureActionTime is a helper to measure execution time of queue's actions\n\n```go\nfunc (q *TaskQueue) Remove(id string) task.Task\n```\nRemove finds element by id and deletes it.\n\n```go\nfunc (q *TaskQueue) RemoveFirst() task.Task\n```\nRemoveFirst deletes a head element, so head is moved.\n\n```go\nfunc (q *TaskQueue) RemoveLast() task.Task\n```\nRemoveLast deletes a tail element, so tail is moved.\n\n```go\nfunc (q *TaskQueue) SetStatus(statusType QueueStatusType)\n```\n\n```go\nfunc (q *TaskQueue) SetStatusText(text string)\n```\n\n```go\nfunc (q *TaskQueue) Start(ctx context.Context)\n```\n\n```go\nfunc (q *TaskQueue) Stop()\n```\n\n```go\nfunc (q *TaskQueue) String() string\n```\nDump tasks in queue to one line\n\n```go\ntype TaskQueueOption func(*TaskQueue)\n```\nTaskQueueOption defines a functional option for TaskQueue configuration\n\n```go\nfunc WithCompactableTypes(taskTypes ...task.TaskType) TaskQueueOption\n```\nWithCompactableTypes sets the compactable task types for the TaskQueue\n\n```go\nfunc WithCompactionCallback(callback func(compactedTasks []task.Task, targetTask task.Task)) TaskQueueOption\n```\n\n```go\nfunc WithContext(ctx context.Context) TaskQueueOption\n```\nWithContext sets the context for the TaskQueue\n\n```go\nfunc WithHandler(fn func(ctx context.Context, t task.Task) TaskResult) TaskQueueOption\n```\nWithHandler sets the task handler for the TaskQueue\n\n```go\nfunc WithLogger(logger *log.Logger) TaskQueueOption\n```\n\n```go\ntype TaskQueueSet struct {\n\tMainName string\n\n\tQueues *queueStorage\n\t// contains filtered or unexported fields\n}\n```\nTaskQueueSet is a manager for a set of named queues\n\n```go\nfunc NewTaskQueueSet() *TaskQueueSet\n```\n\n```go\nfunc (tqs *TaskQueueSet) Add(queue *TaskQueue)\n```\nAdd register a new queue for TaskQueueSet.\n\n```go\nfunc (tqs *TaskQueueSet) AddTailTasks(tasks ...task.Task)\n```\n\n```go\nfunc (tqs *TaskQueueSet) GetByName(name string) *TaskQueue\n```\n\n```go\nfunc (tqs *TaskQueueSet) GetMain() *TaskQueue\n```\n\n```go\nfunc (tqs *TaskQueueSet) GetSnapshot() []*TaskQueue\n```\nGetSnapshot returns a snapshot of all queues at the time of the call. This is useful for iteration when you need to call methods on the queues that might acquire locks, preventing deadlocks.\n\nThe returned slice is a snapshot and will not reflect subsequent changes. The main queue (tqs.MainName) is always placed first in the list.\n\n```go\nfunc (tqs *TaskQueueSet) IterateSnapshot(ctx context.Context, doFn func(ctx context.Context, queue *TaskQueue))\n```\nIterateSnapshot creates a snapshot of all queues and iterates over the copy. This is safer than Iterate() when you need to call queue methods inside the callback, as no locks are held during callback execution.\n\nNote: The snapshot may become stale during iteration if queues are added/removed by other goroutines or by the callback itself.\n\nUse this method when:\n\n  - You need to call queue methods inside the callback (Start, Stop, etc.)\n  - You need to process queues asynchronously\n  - Safety is more important than performance\n\nMemory overhead: O(n) where n is the number of queues.\n\n```go\nfunc (tqs *TaskQueueSet) NewNamedQueue(name string, handler func(ctx context.Context, t task.Task) TaskResult, opts ...TaskQueueOption)\n```\n\n```go\nfunc (tqs *TaskQueueSet) Remove(name string)\n```\n\n```go\nfunc (tqs *TaskQueueSet) Shutdown(ctx context.Context) error\n```\nShutdown stops every queue in the set and blocks until either all workers reach QueueStatusStop or ctx is canceled / deadlines. Returns ctx.Err() on timeout and nil on a clean stop. Safe to call once; calling it on an already-stopped set is a no-op.\n\n```go\nfunc (tqs *TaskQueueSet) Start(ctx context.Context)\n```\n\n```go\nfunc (tqs *TaskQueueSet) StartMain(ctx context.Context)\n```\n\n```go\nfunc (tqs *TaskQueueSet) Stop()\n```\n\n```go\nfunc (tqs *TaskQueueSet) WaitStopWithTimeout(timeout time.Duration)\n```\n\n```go\nfunc (tqs *TaskQueueSet) WithContext(ctx context.Context)\n```\n\n```go\nfunc (tqs *TaskQueueSet) WithLogger(logger *log.Logger) *TaskQueueSet\n```\n\n```go\nfunc (tqs *TaskQueueSet) WithMainName(name string)\n```\n\n```go\nfunc (tqs *TaskQueueSet) WithMetricStorage(mstor metricsstorage.Storage) *TaskQueueSet\n```\n\n```go\ntype TaskQueueStatus struct {\n\t// contains filtered or unexported fields\n}\n```\nTaskQueueStatus encapsulates queue status with thread-safe access\n\n```go\nfunc NewTaskQueueStatus() *TaskQueueStatus\n```\nNewTaskQueueStatus creates a new TaskQueueStatus\n\n```go\nfunc (tqs *TaskQueueStatus) Get() string\n```\nGet returns the current status as a string\n\n```go\nfunc (tqs *TaskQueueStatus) GetType() QueueStatusType\n```\nGetType returns the current status type\n\n```go\nfunc (tqs *TaskQueueStatus) Restore(statusType QueueStatusType, customText string)\n```\nRestore sets both the status type and custom text atomically\n\n```go\nfunc (tqs *TaskQueueStatus) Set(statusType QueueStatusType)\n```\nSet sets the status to a specific type and clears custom text\n\n```go\nfunc (tqs *TaskQueueStatus) SetText(text string)\n```\nSetText sets a custom status text\n\n```go\nfunc (tqs *TaskQueueStatus) SetWithText(statusType QueueStatusType, text string)\n```\nSetWithText sets the status type with custom text\n\n```go\nfunc (tqs *TaskQueueStatus) Snapshot() (QueueStatusType, string)\n```\nSnapshot returns both the status type and custom text atomically\n\n```go\ntype TaskResult struct {\n\tStatus TaskStatus\n\n\tDelayBeforeNextTask time.Duration\n\n\tAfterHandle func()\n\t// contains filtered or unexported fields\n}\n```\n\n```go\nfunc (res *TaskResult) AddAfterTasks(t ...task.Task)\n```\n\n```go\nfunc (res *TaskResult) AddHeadTasks(t ...task.Task)\n```\n\n```go\nfunc (res *TaskResult) AddTailTasks(t ...task.Task)\n```\n\n```go\nfunc (res *TaskResult) GetAfterTasks() []task.Task\n```\n\n```go\nfunc (res *TaskResult) GetHeadTasks() []task.Task\n```\n\n```go\nfunc (res *TaskResult) GetTailTasks() []task.Task\n```\n\n```go\ntype TaskStatus string\n```\n\n```go\nconst (\n\tSuccess TaskStatus = \"Success\"\n\tFail    TaskStatus = \"Fail\"\n\tRepeat  TaskStatus = \"Repeat\"\n\tKeep    TaskStatus = \"Keep\"\n)\n```\n\n```go\ntype TaskStorage struct {\n\t// contains filtered or unexported fields\n}\n```\nTaskStorage encapsulates the queue data structures with their own mutex\n\n```go\nfunc (ts *TaskStorage) AddAfter(id string, tasks ...task.Task)\n```\nAddAfter inserts tasks after the task with the specified ID\n\n```go\nfunc (ts *TaskStorage) AddBefore(id string, newTask task.Task)\n```\nAddBefore inserts a task before the task with the specified ID\n\n```go\nfunc (ts *TaskStorage) AddFirst(tasks ...task.Task)\n```\nAddFirst adds tasks to the front of the queue\n\n```go\nfunc (ts *TaskStorage) AddLast(tasks ...task.Task)\n```\nAddLast adds tasks to the back of the queue\n\n```go\nfunc (ts *TaskStorage) DeleteFunc(fn func(task.Task) bool)\n```\nDeleteFunc runs fn on every task and removes each task for which fn returns false\n\n```go\nfunc (ts *TaskStorage) Get(id string) task.Task\n```\nGet returns a task by ID\n\n```go\nfunc (ts *TaskStorage) GetFirst() task.Task\n```\nGetFirst returns the first task without removing it\n\n```go\nfunc (ts *TaskStorage) GetLast() task.Task\n```\nGetLast returns the last task in the queue\n\n```go\nfunc (ts *TaskStorage) GetSnapshot() []task.Task\n```\nGetSnapshot returns a copy of all tasks in the queue\n\n```go\nfunc (ts *TaskStorage) Iterate(fn func(*list.Element[task.Task]))\n```\nIterate iterates over all tasks in the queue with a callback\n\n```go\nfunc (ts *TaskStorage) Length() int\n```\nLength returns the number of items in the queue\n\n```go\nfunc (ts *TaskStorage) ProcessResult(taskRes TaskResult, t task.Task)\n```\n\n```go\nfunc (ts *TaskStorage) Remove(id string) task.Task\n```\nRemove removes and returns a task by ID\n\n```go\nfunc (ts *TaskStorage) RemoveElement(element *list.Element[task.Task]) task.Task\n```\nRemoveElement removes an element from the list and updates the index\n\n```go\nfunc (ts *TaskStorage) RemoveFirst() task.Task\n```\nRemoveFirst removes and returns the first task\n\n```go\nfunc (ts *TaskStorage) RemoveLast() task.Task\n```\nRemoveLast removes and returns the last task\n\n","path":"github.com/flant/shell-operator/pkg/task/queue","name":"queue","synopsis":"","isRedistributable":true}
