@@ -118,7 +118,7 @@ impl DatastoreWorker {
118
118
119
119
fn work_loop ( & mut self , method : DatastoreMethod ) {
120
120
// Open SQLite connection
121
- let mut conn = match method {
121
+ let mut conn = match & method {
122
122
DatastoreMethod :: Memory ( ) => {
123
123
Connection :: open_in_memory ( ) . expect ( "Failed to create in-memory datastore" )
124
124
}
@@ -149,13 +149,26 @@ impl DatastoreWorker {
149
149
150
150
// Start handling and respond to requests
151
151
loop {
152
+ if self . quit {
153
+ break ;
154
+ } ;
155
+
152
156
let last_commit_time: DateTime < Utc > = Utc :: now ( ) ;
153
- let mut transaction = conn
154
- . transaction_with_behavior ( TransactionBehavior :: Immediate )
155
- . unwrap ( ) ;
157
+ info ! ( "Method '{:?}'" , & method) ;
158
+ let mut transaction: Transaction =
159
+ match conn. transaction_with_behavior ( TransactionBehavior :: Immediate ) {
160
+ Ok ( transaction) => transaction,
161
+ Err ( err) => {
162
+ error ! ( "Unable to start transaction! {:?}" , err) ;
163
+ // Wait 1s before retrying
164
+ std:: thread:: sleep ( std:: time:: Duration :: from_millis ( 1000 ) ) ;
165
+ continue ;
166
+ }
167
+ } ;
168
+ transaction. set_drop_behavior ( DropBehavior :: Commit ) ;
169
+
156
170
self . uncommited_events = 0 ;
157
171
self . commit = false ;
158
- transaction. set_drop_behavior ( DropBehavior :: Commit ) ;
159
172
loop {
160
173
let ( request, response_sender) = match self . responder . poll ( ) {
161
174
Ok ( ( req, res_sender) ) => ( req, res_sender) ,
@@ -182,9 +195,6 @@ impl DatastoreWorker {
182
195
Ok ( _) => ( ) ,
183
196
Err ( err) => panic ! ( "Failed to commit datastore transaction! {}" , err) ,
184
197
}
185
- if self . quit {
186
- break ;
187
- } ;
188
198
}
189
199
info ! ( "DB Worker thread finished" ) ;
190
200
}
@@ -196,29 +206,27 @@ impl DatastoreWorker {
196
206
transaction : & Transaction ,
197
207
) -> Result < Response , DatastoreError > {
198
208
match request {
199
- Command :: CreateBucket ( bucket) => match ds. create_bucket ( & transaction, bucket) {
209
+ Command :: CreateBucket ( bucket) => match ds. create_bucket ( transaction, bucket) {
200
210
Ok ( _) => {
201
211
self . commit = true ;
202
212
Ok ( Response :: Empty ( ) )
203
213
}
204
214
Err ( e) => Err ( e) ,
205
215
} ,
206
- Command :: DeleteBucket ( bucketname) => {
207
- match ds. delete_bucket ( & transaction, & bucketname) {
208
- Ok ( _) => {
209
- self . commit = true ;
210
- Ok ( Response :: Empty ( ) )
211
- }
212
- Err ( e) => Err ( e) ,
216
+ Command :: DeleteBucket ( bucketname) => match ds. delete_bucket ( transaction, & bucketname) {
217
+ Ok ( _) => {
218
+ self . commit = true ;
219
+ Ok ( Response :: Empty ( ) )
213
220
}
214
- }
221
+ Err ( e) => Err ( e) ,
222
+ } ,
215
223
Command :: GetBucket ( bucketname) => match ds. get_bucket ( & bucketname) {
216
224
Ok ( b) => Ok ( Response :: Bucket ( b) ) ,
217
225
Err ( e) => Err ( e) ,
218
226
} ,
219
227
Command :: GetBuckets ( ) => Ok ( Response :: BucketMap ( ds. get_buckets ( ) ) ) ,
220
228
Command :: InsertEvents ( bucketname, events) => {
221
- match ds. insert_events ( & transaction, & bucketname, events) {
229
+ match ds. insert_events ( transaction, & bucketname, events) {
222
230
Ok ( events) => {
223
231
self . uncommited_events += events. len ( ) ;
224
232
self . last_heartbeat . insert ( bucketname. to_string ( ) , None ) ; // invalidate last_heartbeat cache
@@ -229,7 +237,7 @@ impl DatastoreWorker {
229
237
}
230
238
Command :: Heartbeat ( bucketname, event, pulsetime) => {
231
239
match ds. heartbeat (
232
- & transaction,
240
+ transaction,
233
241
& bucketname,
234
242
event,
235
243
pulsetime,
@@ -243,14 +251,14 @@ impl DatastoreWorker {
243
251
}
244
252
}
245
253
Command :: GetEvent ( bucketname, event_id) => {
246
- match ds. get_event ( & transaction, & bucketname, event_id) {
254
+ match ds. get_event ( transaction, & bucketname, event_id) {
247
255
Ok ( el) => Ok ( Response :: Event ( el) ) ,
248
256
Err ( e) => Err ( e) ,
249
257
}
250
258
}
251
259
Command :: GetEvents ( bucketname, starttime_opt, endtime_opt, limit_opt) => {
252
260
match ds. get_events (
253
- & transaction,
261
+ transaction,
254
262
& bucketname,
255
263
starttime_opt,
256
264
endtime_opt,
@@ -261,13 +269,13 @@ impl DatastoreWorker {
261
269
}
262
270
}
263
271
Command :: GetEventCount ( bucketname, starttime_opt, endtime_opt) => {
264
- match ds. get_event_count ( & transaction, & bucketname, starttime_opt, endtime_opt) {
272
+ match ds. get_event_count ( transaction, & bucketname, starttime_opt, endtime_opt) {
265
273
Ok ( n) => Ok ( Response :: Count ( n) ) ,
266
274
Err ( e) => Err ( e) ,
267
275
}
268
276
}
269
277
Command :: DeleteEventsById ( bucketname, event_ids) => {
270
- match ds. delete_events_by_id ( & transaction, & bucketname, event_ids) {
278
+ match ds. delete_events_by_id ( transaction, & bucketname, event_ids) {
271
279
Ok ( ( ) ) => Ok ( Response :: Empty ( ) ) ,
272
280
Err ( e) => Err ( e) ,
273
281
}
@@ -277,22 +285,22 @@ impl DatastoreWorker {
277
285
Ok ( Response :: Empty ( ) )
278
286
}
279
287
Command :: InsertKeyValue ( key, data) => {
280
- match ds. insert_key_value ( & transaction, & key, & data) {
288
+ match ds. insert_key_value ( transaction, & key, & data) {
281
289
Ok ( ( ) ) => Ok ( Response :: Empty ( ) ) ,
282
290
Err ( e) => Err ( e) ,
283
291
}
284
292
}
285
- Command :: GetKeyValue ( key) => match ds. get_key_value ( & transaction, & key) {
293
+ Command :: GetKeyValue ( key) => match ds. get_key_value ( transaction, & key) {
286
294
Ok ( result) => Ok ( Response :: KeyValue ( result) ) ,
287
295
Err ( e) => Err ( e) ,
288
296
} ,
289
297
Command :: GetKeysStarting ( pattern) => {
290
- match ds. get_keys_starting ( & transaction, & pattern) {
298
+ match ds. get_keys_starting ( transaction, & pattern) {
291
299
Ok ( result) => Ok ( Response :: StringVec ( result) ) ,
292
300
Err ( e) => Err ( e) ,
293
301
}
294
302
}
295
- Command :: DeleteKeyValue ( key) => match ds. delete_key_value ( & transaction, & key) {
303
+ Command :: DeleteKeyValue ( key) => match ds. delete_key_value ( transaction, & key) {
296
304
Ok ( ( ) ) => Ok ( Response :: Empty ( ) ) ,
297
305
Err ( e) => Err ( e) ,
298
306
} ,
0 commit comments