aboutsummaryrefslogtreecommitdiff
path: root/weed/worker/tasks/task.go
blob: ceeb5f4132d277158b7b89133dc6656aeeaf071a (plain)
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
package tasks

import (
	"context"
	"fmt"
	"log"
	"os"
	"path/filepath"
	"sync"
	"time"

	"github.com/seaweedfs/seaweedfs/weed/worker/types"
)

// BaseTask provides common functionality for all tasks
type BaseTask struct {
	taskType          types.TaskType
	progress          float64
	cancelled         bool
	mutex             sync.RWMutex
	startTime         time.Time
	estimatedDuration time.Duration

	// Logging functionality
	logFile     *os.File
	logger      *log.Logger
	logFilePath string
}

// NewBaseTask creates a new base task
func NewBaseTask(taskType types.TaskType) *BaseTask {
	return &BaseTask{
		taskType:  taskType,
		progress:  0.0,
		cancelled: false,
	}
}

// Type returns the task type
func (t *BaseTask) Type() types.TaskType {
	return t.taskType
}

// GetProgress returns the current progress (0.0 to 100.0)
func (t *BaseTask) GetProgress() float64 {
	t.mutex.RLock()
	defer t.mutex.RUnlock()
	return t.progress
}

// SetProgress sets the current progress
func (t *BaseTask) SetProgress(progress float64) {
	t.mutex.Lock()
	defer t.mutex.Unlock()
	if progress < 0 {
		progress = 0
	}
	if progress > 100 {
		progress = 100
	}
	t.progress = progress
}

// Cancel cancels the task
func (t *BaseTask) Cancel() error {
	t.mutex.Lock()
	defer t.mutex.Unlock()
	t.cancelled = true
	return nil
}

// IsCancelled returns whether the task is cancelled
func (t *BaseTask) IsCancelled() bool {
	t.mutex.RLock()
	defer t.mutex.RUnlock()
	return t.cancelled
}

// SetStartTime sets the task start time
func (t *BaseTask) SetStartTime(startTime time.Time) {
	t.mutex.Lock()
	defer t.mutex.Unlock()
	t.startTime = startTime
}

// GetStartTime returns the task start time
func (t *BaseTask) GetStartTime() time.Time {
	t.mutex.RLock()
	defer t.mutex.RUnlock()
	return t.startTime
}

// SetEstimatedDuration sets the estimated duration
func (t *BaseTask) SetEstimatedDuration(duration time.Duration) {
	t.mutex.Lock()
	defer t.mutex.Unlock()
	t.estimatedDuration = duration
}

// GetEstimatedDuration returns the estimated duration
func (t *BaseTask) GetEstimatedDuration() time.Duration {
	t.mutex.RLock()
	defer t.mutex.RUnlock()
	return t.estimatedDuration
}

// InitializeTaskLogging sets up task-specific logging - can be called by worker or tasks
func (t *BaseTask) InitializeTaskLogging(workingDir, taskID string) error {
	return t.initializeLogging(workingDir, taskID)
}

// CloseTaskLogging properly closes task logging - can be called by worker or tasks
func (t *BaseTask) CloseTaskLogging() {
	t.closeLogging()
}

// ExecuteTask is a wrapper that handles common task execution logic
func (t *BaseTask) ExecuteTask(ctx context.Context, params types.TaskParams, executor func(context.Context, types.TaskParams) error) error {
	t.SetStartTime(time.Now())
	t.SetProgress(0)

	// Create a context that can be cancelled
	ctx, cancel := context.WithCancel(ctx)
	defer cancel()

	// Monitor for cancellation
	go func() {
		for !t.IsCancelled() {
			select {
			case <-ctx.Done():
				return
			case <-time.After(time.Second):
				// Check cancellation every second
			}
		}
		cancel()
	}()

	// Execute the actual task
	err := executor(ctx, params)

	if err != nil {
		return err
	}

	if t.IsCancelled() {
		return context.Canceled
	}

	t.SetProgress(100)
	return nil
}

// initializeLogging sets up task-specific logging to a file in the working directory
func (t *BaseTask) initializeLogging(workingDir, taskID string) error {
	if workingDir == "" {
		// If no working directory specified, skip file logging
		return nil
	}

	// Ensure working directory exists
	if err := os.MkdirAll(workingDir, 0755); err != nil {
		return fmt.Errorf("failed to create working directory %s: %v", workingDir, err)
	}

	// Create task-specific log file
	timestamp := time.Now().Format("20060102_150405")
	logFileName := fmt.Sprintf("%s_%s_%s.log", t.taskType, taskID, timestamp)
	t.logFilePath = filepath.Join(workingDir, logFileName)

	logFile, err := os.OpenFile(t.logFilePath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
	if err != nil {
		return fmt.Errorf("failed to create log file %s: %v", t.logFilePath, err)
	}

	t.logFile = logFile
	t.logger = log.New(logFile, "", log.LstdFlags|log.Lmicroseconds)

	// Log task initialization
	t.LogInfo("Task %s initialized for %s", taskID, t.taskType)

	return nil
}

// closeLogging properly closes the log file
func (t *BaseTask) closeLogging() {
	if t.logFile != nil {
		t.LogInfo("Task completed, closing log file")
		t.logFile.Close()
		t.logFile = nil
		t.logger = nil
	}
}

// LogInfo writes an info-level log message to both glog and task log file
func (t *BaseTask) LogInfo(format string, args ...interface{}) {
	message := fmt.Sprintf(format, args...)

	// Always log to task file if available
	if t.logger != nil {
		t.logger.Printf("[INFO] %s", message)
	}
}

// LogError writes an error-level log message to both glog and task log file
func (t *BaseTask) LogError(format string, args ...interface{}) {
	message := fmt.Sprintf(format, args...)

	// Always log to task file if available
	if t.logger != nil {
		t.logger.Printf("[ERROR] %s", message)
	}
}

// LogDebug writes a debug-level log message to task log file
func (t *BaseTask) LogDebug(format string, args ...interface{}) {
	message := fmt.Sprintf(format, args...)

	// Always log to task file if available
	if t.logger != nil {
		t.logger.Printf("[DEBUG] %s", message)
	}
}

// LogWarning writes a warning-level log message to both glog and task log file
func (t *BaseTask) LogWarning(format string, args ...interface{}) {
	message := fmt.Sprintf(format, args...)

	// Always log to task file if available
	if t.logger != nil {
		t.logger.Printf("[WARNING] %s", message)
	}
}

// GetLogFilePath returns the path to the task's log file
func (t *BaseTask) GetLogFilePath() string {
	return t.logFilePath
}

// TaskRegistry manages task factories
type TaskRegistry struct {
	factories map[types.TaskType]types.TaskFactory
	mutex     sync.RWMutex
}

// NewTaskRegistry creates a new task registry
func NewTaskRegistry() *TaskRegistry {
	return &TaskRegistry{
		factories: make(map[types.TaskType]types.TaskFactory),
	}
}

// Register registers a task factory
func (r *TaskRegistry) Register(taskType types.TaskType, factory types.TaskFactory) {
	r.mutex.Lock()
	defer r.mutex.Unlock()
	r.factories[taskType] = factory
}

// CreateTask creates a task instance
func (r *TaskRegistry) CreateTask(taskType types.TaskType, params types.TaskParams) (types.TaskInterface, error) {
	r.mutex.RLock()
	factory, exists := r.factories[taskType]
	r.mutex.RUnlock()

	if !exists {
		return nil, &UnsupportedTaskTypeError{TaskType: taskType}
	}

	return factory.Create(params)
}

// GetSupportedTypes returns all supported task types
func (r *TaskRegistry) GetSupportedTypes() []types.TaskType {
	r.mutex.RLock()
	defer r.mutex.RUnlock()

	types := make([]types.TaskType, 0, len(r.factories))
	for taskType := range r.factories {
		types = append(types, taskType)
	}
	return types
}

// GetFactory returns the factory for a task type
func (r *TaskRegistry) GetFactory(taskType types.TaskType) (types.TaskFactory, bool) {
	r.mutex.RLock()
	defer r.mutex.RUnlock()
	factory, exists := r.factories[taskType]
	return factory, exists
}

// UnsupportedTaskTypeError represents an error for unsupported task types
type UnsupportedTaskTypeError struct {
	TaskType types.TaskType
}

func (e *UnsupportedTaskTypeError) Error() string {
	return "unsupported task type: " + string(e.TaskType)
}

// BaseTaskFactory provides common functionality for task factories
type BaseTaskFactory struct {
	taskType     types.TaskType
	capabilities []string
	description  string
}

// NewBaseTaskFactory creates a new base task factory
func NewBaseTaskFactory(taskType types.TaskType, capabilities []string, description string) *BaseTaskFactory {
	return &BaseTaskFactory{
		taskType:     taskType,
		capabilities: capabilities,
		description:  description,
	}
}

// Capabilities returns the capabilities required for this task type
func (f *BaseTaskFactory) Capabilities() []string {
	return f.capabilities
}

// Description returns the description of this task type
func (f *BaseTaskFactory) Description() string {
	return f.description
}

// ValidateParams validates task parameters
func ValidateParams(params types.TaskParams, requiredFields ...string) error {
	for _, field := range requiredFields {
		switch field {
		case "volume_id":
			if params.VolumeID == 0 {
				return &ValidationError{Field: field, Message: "volume_id is required"}
			}
		case "server":
			if params.Server == "" {
				return &ValidationError{Field: field, Message: "server is required"}
			}
		case "collection":
			if params.Collection == "" {
				return &ValidationError{Field: field, Message: "collection is required"}
			}
		}
	}
	return nil
}

// ValidationError represents a parameter validation error
type ValidationError struct {
	Field   string
	Message string
}

func (e *ValidationError) Error() string {
	return e.Field + ": " + e.Message
}