-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_task.go
104 lines (87 loc) · 2.56 KB
/
main_task.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
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
package main
import (
"fmt"
"time"
)
// ЗАДАНИЕ:
// * сделать из плохого кода хороший;
// * важно сохранить логику появления ошибочных тасков;
// * сделать правильную мультипоточность обработки заданий.
// Обновленный код отправить через merge-request.
// приложение эмулирует получение и обработку тасков, пытается и получать и обрабатывать в многопоточном режиме
// В конце должно выводить успешные таски и ошибки выполнены остальных тасков
// A Ttype represents a meaninglessness of our life
type Ttype struct {
id int
cT string // время создания
fT string // время выполнения
taskRESULT []byte
}
func main() {
taskCreturer := func(a chan Ttype) {
go func() {
for {
ft := time.Now().Format(time.RFC3339)
if time.Now().Nanosecond()%2 > 0 { // вот такое условие появления ошибочных тасков
ft = "Some error occured"
}
a <- Ttype{cT: ft, id: int(time.Now().Unix())} // передаем таск на выполнение
}
}()
}
superChan := make(chan Ttype, 10)
go taskCreturer(superChan)
task_worker := func(a Ttype) Ttype {
tt, _ := time.Parse(time.RFC3339, a.cT)
if tt.After(time.Now().Add(-20 * time.Second)) {
a.taskRESULT = []byte("task has been successed")
} else {
a.taskRESULT = []byte("something went wrong")
}
a.fT = time.Now().Format(time.RFC3339Nano)
time.Sleep(time.Millisecond * 150)
return a
}
doneTasks := make(chan Ttype)
undoneTasks := make(chan error)
tasksorter := func(t Ttype) {
if string(t.taskRESULT[14:]) == "successed" {
doneTasks <- t
} else {
undoneTasks <- fmt.Errorf("Task id %d time %s, error %s", t.id, t.cT, t.taskRESULT)
}
}
go func() {
// получение тасков
for t := range superChan {
t = task_worker(t)
go tasksorter(t)
}
close(superChan)
}()
result := map[int]Ttype{}
err := []error{}
go func() {
for r := range doneTasks {
go func() {
result[r.id] = r
}()
}
for r := range undoneTasks {
go func() {
err = append(err, r)
}()
}
close(doneTasks)
close(undoneTasks)
}()
time.Sleep(time.Second * 3)
println("Errors:")
for r := range err {
println(r)
}
println("Done tasks:")
for r := range result {
println(r)
}
}