-
Notifications
You must be signed in to change notification settings - Fork 1
/
importTask.go
72 lines (57 loc) · 1.57 KB
/
importTask.go
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
package main
import (
"fmt"
"log"
"sync"
"github.com/pixlise/core/v4/core/utils"
)
type importTask struct {
id string
params string
resultErr error
complete bool
}
var importTaskMutex sync.Mutex
var importTasks = map[string]importTask{}
var importTaskErrorCount = 0
func addImportTask(params string) string {
id := utils.RandStringBytesMaskImpr(10)
importTaskMutex.Lock()
defer importTaskMutex.Unlock()
importTasks[id] = importTask{id: id, params: params, resultErr: nil, complete: false}
return id
}
func finishImportTask(id string, err error) {
importTaskMutex.Lock()
defer importTaskMutex.Unlock()
task, ok := importTasks[id]
if !ok {
fatalError(fmt.Errorf("Failed to find import task: %v", id))
}
// mark this task as complete
//delete(importTasks, id)
task.complete = true
task.resultErr = err
if err != nil {
importTaskErrorCount++
printTaskError(importTaskErrorCount, task)
} else {
fmt.Printf("Task (of total %v) finished: %v\n", len(importTasks), task.params)
}
}
func reportFailedTasks() {
importTaskMutex.Lock()
defer importTaskMutex.Unlock()
fmt.Printf("Total tasks: %v. Failures: %v\n", len(importTasks), importTaskErrorCount)
fmt.Printf("List of all errors:\n")
errCount := 0
for _, task := range importTasks {
if task.resultErr != nil {
errCount++
printTaskError(errCount, task)
}
}
}
func printTaskError(idx int, task importTask) {
log.Printf("==================\nImport Task Failed\n------------------\nTask (%v of %v): %v\nError: %v\n==================\n", idx, len(importTasks), task.params, task.resultErr)
}