-
Notifications
You must be signed in to change notification settings - Fork 1
/
worker.go
75 lines (64 loc) · 1.42 KB
/
worker.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
package gorun
// WorkerCreate - constant for create status
const WorkerCreate = 1
// WorkerRun - constant for run status
const WorkerRun = 2
// WorkerStop - constant for stop status
const WorkerStop = 3
// WorkerPause - constant for stop status
const WorkerPause = 4
// Worker - basic worker struct
// Status - current worker status
// Name - worker unique name
// Params - worker outer params
// Fn - worker function
// tm - pointer to worker manager
// isRun - flag for workers, that gorutine is start
type Worker struct {
Status int
Name string
Params interface{}
Fn func(worker *Worker)
tm *WorkerManager
run bool
}
// Run - run worker gorutine
func (w *Worker) Run() {
if w.Status == WorkerCreate || w.Status == WorkerPause {
w.Status = WorkerRun
if !w.run {
w.run = true
go w.Fn(w)
}
}
}
// Stop - stop and remove worker
func (w *Worker) Stop() {
w.Status = WorkerStop
w.tm.removeWorker(w.Name)
}
// Pause - pause worker
func (w *Worker) Pause() {
w.Status = WorkerPause
}
// IsRun - check, that worker can run
func (w *Worker) IsRun() bool {
if w.Status == WorkerRun {
return true
}
return false
}
// IsStop - check, that worker must stop worker and finish gorutine
func (w *Worker) IsStop() bool {
if w.Status == WorkerStop {
return true
}
return false
}
// IsPause - check, that worker is pause
func (w *Worker) IsPause() bool {
if w.Status == WorkerPause {
return true
}
return false
}