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) { // {{{
|
||||
user := getUser(r)
|
||||
|
||||
body, _ := r.GetBody()
|
||||
data, _ := io.ReadAll(body)
|
||||
body, _ := io.ReadAll(r.Body)
|
||||
var request struct {
|
||||
ClientUUID string
|
||||
NodeData string
|
||||
}
|
||||
err := json.Unmarshal(data, &request)
|
||||
err := json.Unmarshal(body, &request)
|
||||
if err != nil {
|
||||
httpError(w, err)
|
||||
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{}{
|
||||
"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
|
||||
|
|
|
@ -88,19 +88,36 @@ export class Sync {
|
|||
}//}}}
|
||||
|
||||
static async nodesToServer() {//{{{
|
||||
while(true) {
|
||||
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 request = {
|
||||
NodeData: JSON.stringify(nodesToSend),
|
||||
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)
|
||||
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) {
|
||||
console.log(e)
|
||||
console.trace(e)
|
||||
alert(e)
|
||||
return
|
||||
}
|
||||
}
|
||||
}//}}}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue