/
import_queue.go
211 lines (171 loc) · 4.47 KB
/
import_queue.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package ownmapdal
import (
"fmt"
"io"
"log"
"os"
"path/filepath"
"sync"
"time"
"github.com/jamesrr39/goutil/dirtraversal"
"github.com/jamesrr39/goutil/errorsx"
)
const (
PBFFileSuffix = ".pbf"
)
type ImportStatus int
const (
ImportStatusQueued ImportStatus = 1
ImportStatusInProgress ImportStatus = 2
ImportStatusDone ImportStatus = 3
)
var importStatusNames = []string{
"",
"Queued",
"In Progress",
"Done",
}
func (i ImportStatus) String() string {
return importStatusNames[i]
}
type OnImportedSuccessfullyFunc func(dataSource DataSourceConn)
type ProcessImportFunc func(pbfReader PBFReader) (DataSourceConn, errorsx.Error)
type ImportQueueItem struct {
datasource *DataSourceConn
RawDataFilePath string
Status ImportStatus
ProgressPercent float64
TimeInProgress time.Duration
processFunc ProcessImportFunc
}
type ImportQueue struct {
items []*ImportQueueItem
mu *sync.RWMutex
pathsConfig *PathsConfig
}
func NewImportQueue(pathsConfig *PathsConfig) *ImportQueue {
return &ImportQueue{[]*ImportQueueItem{}, new(sync.RWMutex), pathsConfig}
}
func (q *ImportQueue) GetItems() []*ImportQueueItem {
q.mu.RLock()
defer q.mu.RUnlock()
return q.items
}
func (q *ImportQueue) AddItemToQueue(rawData io.Reader, fileName string, processFunc ProcessImportFunc, onImportedSuccessfully OnImportedSuccessfullyFunc) errorsx.Error {
var err error
tryingToGoUp := dirtraversal.IsTryingToTraverseUp(fileName)
if tryingToGoUp {
return errorsx.Errorf("not allowed to traverse up with filename %q", fileName)
}
rawDataFilePath, err := GenerateFilePathForNewDiskFile(q.pathsConfig.RawDataFilesDir, fileName, PBFFileSuffix)
if err != nil {
return errorsx.Wrap(err)
}
f, err := os.Create(rawDataFilePath)
if err != nil {
return errorsx.Wrap(err)
}
defer f.Close()
_, err = io.Copy(f, rawData)
if err != nil {
return errorsx.Wrap(err)
}
item := &ImportQueueItem{
RawDataFilePath: rawDataFilePath,
Status: ImportStatusQueued,
ProgressPercent: 0,
TimeInProgress: 0,
processFunc: processFunc,
}
q.mu.Lock()
q.items = append(q.items, item)
q.mu.Unlock()
nextItem := q.getNextDatasourceToProcess()
if nextItem != nil {
go func() {
dataSource, err := q.importQueueItem(nextItem)
if err != nil {
log.Printf(
"ERROR: failed to import queue item. Raw Data file: %q.\nError: %q\nStack: %s\n",
nextItem.RawDataFilePath, err.Error(), err.Stack())
return
}
onImportedSuccessfully(dataSource)
}()
}
return nil
}
func (q *ImportQueue) getNextDatasourceToProcess() *ImportQueueItem {
q.mu.Lock()
defer q.mu.Unlock()
for _, item := range q.items {
if item.Status == ImportStatusInProgress {
// there is already an import in progress. Wait.
return nil
}
}
// no imports in progress. Now go through the list and pick the fist one to move to in progress
for _, item := range q.items {
if item.Status == ImportStatusQueued {
return item
}
}
// all imports are finished
return nil
}
func (q *ImportQueue) importQueueItem(item *ImportQueueItem) (DataSourceConn, errorsx.Error) {
var err error
rawDataFile, err := os.Open(item.RawDataFilePath)
if err != nil {
return nil, errorsx.Wrap(err)
}
defer rawDataFile.Close()
pbfReader, err := NewDefaultPBFReader(rawDataFile)
if err != nil {
return nil, errorsx.Wrap(err)
}
item.Status = ImportStatusInProgress
startTime := time.Now()
go func() {
totalSize := pbfReader.TotalSize()
for {
time.Sleep(2 * time.Second)
item.TimeInProgress = time.Now().Sub(startTime)
if item.Status == ImportStatusDone {
item.ProgressPercent = 100
return
}
progressPercent := float64(pbfReader.FullyScannedBytes()) * 100 / float64(totalSize)
item.ProgressPercent = progressPercent
}
}()
dataSource, err := item.processFunc(pbfReader)
if err != nil {
return nil, errorsx.Wrap(err)
}
item.Status = ImportStatusDone
return dataSource, nil
}
func GenerateFilePathForNewDiskFile(dirPath, fileName, suffix string) (string, errorsx.Error) {
var err error
for i := 0; i < 1000000; i++ {
var id string
if i != 0 {
id = fmt.Sprintf("_%d", i)
}
fileName := fmt.Sprintf("%s%s%s", fileName, id, suffix)
filePath := filepath.Join(dirPath, fileName)
_, err = os.Stat(filePath)
if err != nil {
if !os.IsNotExist(err) {
return "", errorsx.Wrap(err)
}
}
if err == nil {
// file already exists
continue
}
return filePath, nil
}
return "", errorsx.Errorf("ran out of numbers for suffix")
}