-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmemory_store.go
57 lines (52 loc) · 1012 Bytes
/
memory_store.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
package cleanjob
import (
"errors"
"sync"
)
type Memory struct {
list map[string]*Job
lock sync.Map
watch WatchFunc
}
func NewMemory() *Memory {
return &Memory{
list: make(map[string]*Job),
lock: sync.Map{},
}
}
func (m *Memory) Test(job *Job) {
m.list[job.Key] = job
}
func (m *Memory) Load() ([]*Job, error) {
jobs := make([]*Job, 0)
for _, v := range m.list {
jobs = append(jobs, v)
}
return jobs, nil
}
func (m *Memory) Save(job *Job) error {
m.list[job.Key] = job
m.watch(AddEvent, job)
return nil
}
func (m *Memory) Delete(job *Job) error {
delete(m.list, job.Key)
m.watch(DeleteEvent, job)
return nil
}
func (m *Memory) Lock(ex *ExecuteContext) error {
if _, ok := m.lock.Load(ex.Job.Key); ok {
return errors.New("locked")
}
m.lock.Store(ex.Job.Key, ex.Job.Key)
return nil
}
func (m *Memory) UnLock(ex *ExecuteContext) error {
if _, ok := m.lock.Load(ex.Job.Key); ok {
m.lock.Delete(ex.Job.Key)
}
return nil
}
func (m *Memory) Watch(f WatchFunc) {
m.watch = f
}