-
Notifications
You must be signed in to change notification settings - Fork 0
/
grom_test.go
82 lines (67 loc) · 2.39 KB
/
grom_test.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
package grom_test
import (
"fmt"
"log"
"time"
"github.com/geeksteam/gorm"
)
func ExampleGormUsing() {
gorm.Run(NewRoutine(func() {
for i := 0; i <= 2; i++ {
log.Println("Couner: ", i)
time.Sleep(time.Second)
}
log.Println("Finish")
}, "task", "Simple counting", 0))
time.Sleep(4 * time.Second)
log.Printf("%+v\n", gorm.Routines())
time.Sleep(1 * time.Second)
}
type Routine struct {
Started time.Time // Дата и время с микросекундами (27.07.2015 14:01.123) запуска рутины
Stopped time.Time // Дата и время остановки рутины
Type string // Тип горутины ( возможные типы: web handler, task, background, backup )
Owner int // Id пользователя запустившего рутину
Description string // Возможное описание рутины ( eg.: Backup files from /tmp/user )
Status int // Статус рутины ( 0 - successful done, 1 - running, 2 - stopped with error )
ExitError string // Описание ошибки при завершении рутины с ошибкой
f func() // Тело рутины
}
func NewRoutine(f func(), Type, description string, owner int) *Routine {
return &Routine{
Type: Type,
Owner: owner,
Description: description,
f: f,
}
}
func (r *Routine) Start() {
r.Started = time.Now()
r.Status = 1
defer func() {
r.Stopped = time.Now()
}()
r.f()
r.Status = 0
}
func (r *Routine) OnError(err error) {
r.Status = 2
r.ExitError = err.Error()
}
func (r *Routine) String() string {
switch r.Status {
case 0:
return fmt.Sprintf("Started: %v\nStopped: %v\nType: %v\nDescription: %v\nOwnerID: %v\nStatus: Successful done", r.Started, r.Stopped, r.Type, r.Description, r.Owner)
case 1:
return fmt.Sprintf("Started: %v\nType: %v\nDescription: %v\nOwnerID: %v\nStatus: Running", r.Started, r.Type, r.Description, r.Owner)
case 2:
return fmt.Sprintf("Started: %v\nStopped: %v\nType: %v\nDescription: %v\nOwnerID: %v\nStatus: Stopped with error '%v'", r.Started, r.Stopped, r.Type, r.Description, r.Owner, r.ExitError)
}
return fmt.Sprintf("Started: %v\nType: %v\nDescription: %v\nOwnerID: %v\nStatus: Unknown", r.Started, r.Type, r.Description, r.Owner)
}
func (r *Routine) Finished() bool {
if r.Status == 1 {
return false
}
return true
}