-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathglobal_task_api.go
More file actions
146 lines (116 loc) · 3.97 KB
/
Copy pathglobal_task_api.go
File metadata and controls
146 lines (116 loc) · 3.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package clicky
import (
"os"
"sync"
"time"
"github.com/flanksource/clicky/task"
)
var (
// Phase tracking
currentPhaseTask *Task
phaseMutex sync.Mutex
)
func StartTask[T any](name string, taskFunc task.TaskFunc[T], opts ...TaskOption) task.TypedTask[T] {
return task.StartTask(name, taskFunc, opts...)
}
func StartGroup[T any](name string, opts ...task.TaskGroupOption) task.TypedGroup[T] {
return task.StartGroup[T](name, opts...)
}
// WaitForGlobalCompletion waits for all global tasks to complete and returns exit code
func WaitForGlobalCompletion() int {
return task.Wait()
}
// WaitForGlobalCompletionSilent waits for global tasks without displaying results
func WaitForGlobalCompletionSilent() int {
return task.WaitSilent()
}
// SetGlobalInterruptHandler sets the interrupt handler for the global TaskManager
func SetGlobalInterruptHandler(fn func()) {
task.SetInterruptHandler(fn)
}
// SetGlobalMaxConcurrency sets the maximum concurrency for the global TaskManager
func SetGlobalMaxConcurrency(max int) {
task.SetMaxConcurrent(max)
}
// SetGlobalVerbose enables/disables verbose output for the global TaskManager
func SetGlobalVerbose(verbose bool) {
task.SetVerbose(verbose)
}
// CancelAllGlobalTasks cancels all running global tasks
func CancelAllGlobalTasks() {
task.CancelAll()
}
// StopTask cancels a specific global task by immutable task ID.
func StopTask(id string) bool {
return task.StopTask(id)
}
// ClearGlobalTasks removes completed tasks from the global TaskManager
func ClearGlobalTasks() {
task.ClearTasks()
}
// StartCapturingOutput replaces os.Stdout / os.Stderr with internal
// pipes so that bare fmt.Print / os.Stderr writes are buffered instead
// of interleaving with the live task renderer. The live renderer keeps
// drawing on the real terminal because it captured the original file
// descriptors at manager init. Loggers that captured os.Stderr before
// this call keep writing live, too.
//
// Pair with StopCapturingOutput at end-of-run; the buffered content is
// flushed in stream order onto the restored streams.
func StartCapturingOutput() {
task.StartCapturingOutput()
}
// StopCapturingOutput restores os.Stdout / os.Stderr and flushes every
// buffered line to the real terminal in write order, tagged by stream.
// Safe to call when capture wasn't started.
func StopCapturingOutput() {
task.StopCapturingOutput()
}
// GetGlobalTaskManagerStats returns stats about the global TaskManager
func GetGlobalTaskManagerStats() (total, running, completed, failed int) {
// This would require adding a stats method to TaskManager
// For now, return basic info
return 0, 0, 0, 0 // TODO: Implement stats method
}
// RegisterGlobalExit ensures tasks are displayed when the program exits
func RegisterGlobalExit() {
// This could be called at program startup to ensure tasks are shown on exit
// Implementation would depend on how the program exit flow works
}
// SetGlobalSignalTimeout configures the graceful shutdown timeout
func SetGlobalSignalTimeout(timeout time.Duration) {
task.SetGracefulTimeout(timeout)
}
// ExitWithGlobalTaskSummary displays task summary and exits with appropriate code
func ExitWithGlobalTaskSummary() {
exitCode := WaitForGlobalCompletion()
os.Exit(exitCode)
}
// StartGlobalPhase starts a new phase tracking task
func StartGlobalPhase(phaseName string) *Task {
phaseMutex.Lock()
defer phaseMutex.Unlock()
// Complete the previous phase if it exists
if currentPhaseTask != nil {
currentPhaseTask.Success()
currentPhaseTask = nil
}
return currentPhaseTask
}
// UpdateGlobalPhaseProgress updates the progress of the current phase
func UpdateGlobalPhaseProgress(message string) {
phaseMutex.Lock()
defer phaseMutex.Unlock()
if currentPhaseTask != nil {
currentPhaseTask.Infof("%s", message)
}
}
// CompleteGlobalPhase marks the current phase as completed
func CompleteGlobalPhase() {
phaseMutex.Lock()
defer phaseMutex.Unlock()
if currentPhaseTask != nil {
currentPhaseTask.Success()
currentPhaseTask = nil
}
}