Clean queue after sending
This commit is contained in:
parent
1c3116d9dc
commit
dfd6260a7a
3 changed files with 49 additions and 16 deletions
7
main.go
7
main.go
|
@ -299,19 +299,18 @@ func actionNodeRetrieve(w http.ResponseWriter, r *http.Request) { // {{{
|
||||||
func actionSyncToServer(w http.ResponseWriter, r *http.Request) { // {{{
|
func actionSyncToServer(w http.ResponseWriter, r *http.Request) { // {{{
|
||||||
user := getUser(r)
|
user := getUser(r)
|
||||||
|
|
||||||
body, _ := r.GetBody()
|
body, _ := io.ReadAll(r.Body)
|
||||||
data, _ := io.ReadAll(body)
|
|
||||||
var request struct {
|
var request struct {
|
||||||
ClientUUID string
|
ClientUUID string
|
||||||
NodeData string
|
NodeData string
|
||||||
}
|
}
|
||||||
err := json.Unmarshal(data, &request)
|
err := json.Unmarshal(body, &request)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
httpError(w, err)
|
httpError(w, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
db.Exec(`CALL add_nodes(%d, %s::jsonb)`, user.ID, request.ClientUUID, request.NodeData)
|
db.Exec(`CALL add_nodes($1, $2, $3::jsonb)`, user.ID, request.ClientUUID, request.NodeData)
|
||||||
|
|
||||||
responseData(w, map[string]interface{}{
|
responseData(w, map[string]interface{}{
|
||||||
"OK": true,
|
"OK": true,
|
||||||
|
|
|
@ -396,6 +396,23 @@ class SimpleNodeStore {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}//}}}
|
}//}}}
|
||||||
|
async delete(keys) {//{{{
|
||||||
|
const store = this.db
|
||||||
|
.transaction(['nodes', this.storeName], 'readwrite')
|
||||||
|
.objectStore(this.storeName)
|
||||||
|
|
||||||
|
const promises = []
|
||||||
|
for (const key of keys) {
|
||||||
|
const p = new Promise((resolve, reject)=>{
|
||||||
|
// TODO - implement a way to add an error to a page-global error log.
|
||||||
|
const request = store.delete(key)
|
||||||
|
request.onsuccess = (event)=>resolve(event)
|
||||||
|
request.onerror = (event)=>reject(event)
|
||||||
|
})
|
||||||
|
promises.push(p)
|
||||||
|
}
|
||||||
|
return Promise.all(promises)
|
||||||
|
}//}}}
|
||||||
}
|
}
|
||||||
|
|
||||||
// vim: foldmethod=marker
|
// vim: foldmethod=marker
|
||||||
|
|
|
@ -88,19 +88,36 @@ export class Sync {
|
||||||
}//}}}
|
}//}}}
|
||||||
|
|
||||||
static async nodesToServer() {//{{{
|
static async nodesToServer() {//{{{
|
||||||
|
while(true) {
|
||||||
try {
|
try {
|
||||||
const nodesToSend = await nodeStore.sendQueue.retrieve(100)
|
// Send nodes in batches until everything is sent, or an error has occured.
|
||||||
|
const nodesToSend = await nodeStore.sendQueue.retrieve(2)
|
||||||
|
if (nodesToSend.length === 0)
|
||||||
|
break
|
||||||
|
console.debug(`Sending ${nodesToSend.length} node(s) to server`)
|
||||||
|
|
||||||
const clientUUID = await nodeStore.getAppState('client_uuid')
|
const clientUUID = await nodeStore.getAppState('client_uuid')
|
||||||
const request = {
|
const request = {
|
||||||
NodeData: JSON.stringify(nodesToSend),
|
NodeData: JSON.stringify(nodesToSend),
|
||||||
ClientUUID: clientUUID.value,
|
ClientUUID: clientUUID.value,
|
||||||
}
|
}
|
||||||
res = await API.query('POST', `/sync/to_server/${oldMax}/${offset}`, request)
|
const res = await API.query('POST', '/sync/to_server', request)
|
||||||
|
if (!res.OK) {
|
||||||
|
// TODO - implement better error management here.
|
||||||
console.log(res)
|
console.log(res)
|
||||||
|
alert(res)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Nodes are archived on server and can now be deleted from the send queue.
|
||||||
|
const keys = nodesToSend.map(node => node.ClientSequence)
|
||||||
|
console.log(await nodeStore.sendQueue.delete(keys))
|
||||||
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.log(e)
|
console.trace(e)
|
||||||
alert(e)
|
alert(e)
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}//}}}
|
}//}}}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue