-
Notifications
You must be signed in to change notification settings - Fork 1
/
manager.go
144 lines (124 loc) · 3.17 KB
/
manager.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
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
package gorun
import (
"fmt"
)
// WorkerManager - manage workers, update worker status, reg new workers
// Workers - slice of Worker pointers
type WorkerManager struct {
Workers map[string]*Worker
}
var workerManager *WorkerManager
// New - create new worker manager
// return
// WM - new WorkerManager instance
func New() *WorkerManager {
return &WorkerManager{
Workers: make(map[string]*Worker),
}
}
// WM - singleton WorkerManager instance
// return
// * WM - pointer to WorkerManager instance
func WM() *WorkerManager {
if workerManager == nil {
workerManager = New()
}
return workerManager
}
func (tm *WorkerManager) workerExists(name string) bool {
_, check := tm.Workers[name]
return check
}
func (tm *WorkerManager) removeWorker(name string) {
delete(tm.Workers, name)
}
// AddWorker - add new worker to worker manager
// return
// err - error, if worker with the same name exists
func (tm *WorkerManager) AddWorker(name string, params interface{}, cb func(*Worker)) (*Worker, error) {
if tm.workerExists(name) {
return nil, fmt.Errorf(`worker with name [%s] exists`, name)
}
tm.Workers[name] = &Worker{
Name: name,
Status: WorkerCreate,
Params: params,
Fn: cb,
tm: tm,
}
return tm.Workers[name], nil
}
// GetWorkers - return workers list in queue
// return
// list - map[string]*Workers - list of available workers
func (tm *WorkerManager) GetWorkers() map[string]*Worker {
return tm.Workers
}
// RunAll - run all workers
func (tm *WorkerManager) RunAll() {
for _, worker := range tm.Workers {
worker.Run()
}
}
// StopAll - stop all workers
func (tm *WorkerManager) StopAll() {
for _, worker := range tm.Workers {
worker.Stop()
}
}
// PauseAll - pause all workers
func (tm *WorkerManager) PauseAll() {
for _, worker := range tm.Workers {
worker.Pause()
}
}
// UpdateWorker - update worker data
func (tm *WorkerManager) UpdateWorker(name string, params interface{}) error {
if !tm.workerExists(name) {
return fmt.Errorf(`worker %s not found`, name)
}
tm.Workers[name].Params = params
return nil
}
// SetStatus - update worker status
func (tm *WorkerManager) SetStatus(name string, status int) error {
if !tm.workerExists(name) {
return fmt.Errorf(`worker %s not found`, name)
}
if inArray([]int{WorkerCreate, WorkerRun, WorkerPause, WorkerStop}, status) {
tm.Workers[name].Status = status
return nil
}
return fmt.Errorf(`worker status not found`)
}
// RemoveWorker - remove worker by worker name
func (tm *WorkerManager) RemoveWorker(name string) error {
if !tm.workerExists(name) {
return fmt.Errorf(`worker %s not found`, name)
}
tm.Workers[name].Stop()
return nil
}
// Get - return worker by name
func (tm *WorkerManager) Get(name string) (*Worker, error) {
if !tm.workerExists(name) {
return nil, fmt.Errorf(`worker %s not found`, name)
}
return tm.Workers[name], nil
}
// Run - run worker or return error
func (tm *WorkerManager) Run(name string) (*Worker, error) {
if !tm.workerExists(name) {
return nil, fmt.Errorf(`worker %s not found`, name)
}
tm.Workers[name].Run()
return tm.Workers[name], nil
}
func inArray(arr []int, need int) bool {
for _, i := range arr {
if arr[i] == need {
return true
}
}
return false
}