This repository has been archived by the owner on Dec 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathin_memory_store.go
156 lines (143 loc) · 3.48 KB
/
in_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
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
145
146
147
148
149
150
151
152
153
154
155
156
// Copyright 2016-present Oliver Eilhard. All rights reserved.
// Use of this source code is governed by a MIT-license.
// See http://olivere.mit-license.org/license.txt for details.
package jobqueue
import (
"context"
"fmt"
"sync"
)
// InMemoryStore is a simple in-memory store implementation.
// It implements the Store interface. Do not use in production.
type InMemoryStore struct {
mu sync.Mutex
jobs map[string]Job
}
// NewInMemoryStore creates a new InMemoryStore.
func NewInMemoryStore() *InMemoryStore {
return &InMemoryStore{
jobs: make(map[string]Job),
}
}
// Start the store.
func (st *InMemoryStore) Start(_ StartupBehaviour) error {
return nil
}
// Create adds a new job.
func (st *InMemoryStore) Create(ctx context.Context, job *Job) error {
st.mu.Lock()
defer st.mu.Unlock()
st.jobs[job.ID] = *job
return nil
}
// Delete removes the job.
func (st *InMemoryStore) Delete(ctx context.Context, job *Job) error {
st.mu.Lock()
defer st.mu.Unlock()
delete(st.jobs, job.ID)
return nil
}
// Update updates the job.
func (st *InMemoryStore) Update(ctx context.Context, job *Job) error {
st.mu.Lock()
defer st.mu.Unlock()
st.jobs[job.ID] = *job
return nil
}
// Next picks the next job to execute.
func (st *InMemoryStore) Next() (*Job, error) {
st.mu.Lock()
defer st.mu.Unlock()
var next *Job
for _, job := range st.jobs {
if job.State == Waiting {
if next == nil || job.Rank > next.Rank || job.Priority > next.Priority {
dup := job
next = &dup
}
}
}
return next, nil
}
// Stats returns statistics about the jobs in the store.
func (st *InMemoryStore) Stats(ctx context.Context, req *StatsRequest) (*Stats, error) {
st.mu.Lock()
defer st.mu.Unlock()
stats := &Stats{}
for _, job := range st.jobs {
if req.Topic != "" && job.Topic != req.Topic {
continue
}
if req.CorrelationGroup != "" && job.CorrelationGroup != req.CorrelationGroup {
continue
}
switch job.State {
default:
return nil, fmt.Errorf("found unknown state %v", job.State)
case Waiting:
stats.Waiting++
case Working:
stats.Working++
case Succeeded:
stats.Succeeded++
case Failed:
stats.Failed++
}
}
return stats, nil
}
// Lookup returns the job with the specified identifier (or ErrNotFound).
func (st *InMemoryStore) Lookup(ctx context.Context, id string) (*Job, error) {
st.mu.Lock()
defer st.mu.Unlock()
job, found := st.jobs[id]
if !found {
return nil, ErrNotFound
}
dup := job
return &dup, nil
}
// LookupByCorrelationID returns the details of jobs by their correlation identifier.
// If no such job could be found, an empty array is returned.
func (st *InMemoryStore) LookupByCorrelationID(ctx context.Context, correlationID string) ([]*Job, error) {
st.mu.Lock()
defer st.mu.Unlock()
var result []*Job
for _, job := range st.jobs {
if job.CorrelationID == correlationID {
dup := job
result = append(result, &dup)
}
}
return result, nil
}
// List finds matching jobs.
func (st *InMemoryStore) List(ctx context.Context, req *ListRequest) (*ListResponse, error) {
st.mu.Lock()
defer st.mu.Unlock()
rsp := &ListResponse{}
i := -1
for _, job := range st.jobs {
var skip bool
if req.Topic != "" && req.Topic != job.Topic {
skip = true
}
if req.State != "" && job.State != req.State {
skip = true
}
if req.Offset > 0 && req.Offset < i {
skip = true
rsp.Total++
}
if req.Limit > 0 && i > req.Limit {
skip = true
rsp.Total++
}
if !skip {
dup := job
rsp.Jobs = append(rsp.Jobs, &dup)
}
i++
}
return rsp, nil
}