Fix: queue will not attempt to reupload immediately but instead try every 5 minutes, fix #2436

This commit is contained in:
Pieter Vander Vennet 2025-06-14 01:46:25 +02:00
parent bc2ea7841f
commit 1e45c28915

View file

@ -3,7 +3,7 @@ import LinkImageAction from "../Osm/Actions/LinkImageAction"
import FeaturePropertiesStore from "../FeatureSource/Actors/FeaturePropertiesStore" import FeaturePropertiesStore from "../FeatureSource/Actors/FeaturePropertiesStore"
import { NoteId, OsmId, OsmTags } from "../../Models/OsmFeature" import { NoteId, OsmId, OsmTags } from "../../Models/OsmFeature"
import ThemeConfig from "../../Models/ThemeConfig/ThemeConfig" import ThemeConfig from "../../Models/ThemeConfig/ThemeConfig"
import { Store, UIEventSource } from "../UIEventSource" import { Store, Stores, UIEventSource } from "../UIEventSource"
import { OsmConnection } from "../Osm/OsmConnection" import { OsmConnection } from "../Osm/OsmConnection"
import { Changes } from "../Osm/Changes" import { Changes } from "../Osm/Changes"
import Translations from "../../UI/i18n/Translations" import Translations from "../../UI/i18n/Translations"
@ -80,6 +80,10 @@ export class ImageUploadManager {
this._changes = changes this._changes = changes
this._gps = gpsLocation this._gps = gpsLocation
this._reportError = reportError this._reportError = reportError
Stores.Chronic(5 * 60000).addCallback(() => {
// If images failed to upload: attempt to reupload
this.uploadQueue()
})
} }
public async canBeUploaded(file: File): Promise<true | { error: Translation }> { public async canBeUploaded(file: File): Promise<true | { error: Translation }> {
@ -166,16 +170,26 @@ export class ImageUploadManager {
if (this.uploadingAll) { if (this.uploadingAll) {
return return
} }
const queue = this._queue.imagesInQueue.data ?? [] let queue = this._queue.imagesInQueue.data ?? []
if (queue.length === 0) { if (queue.length === 0) {
return return
} }
console.log("Checking image upload queue and uploading if needed") console.log("Checking image upload queue and uploading if needed")
this.uploadingAll = true this.uploadingAll = true
try { try {
queue = [...queue]
while (queue.length > 0) { while (queue.length > 0) {
const currentItem = queue[0] const currentItem = queue.shift()
await this.handleQueueItem(currentItem) if(!currentItem){
continue
}
const uploadOk = await this.handleQueueItem(currentItem)
if(uploadOk){
this._queue.delete(currentItem)
}else{
// Our local 'queue' is a copy where we've removed the failed item from
// A next attempt to 'uploadQueue' will retry the upload
}
} }
} catch (e) { } catch (e) {
console.error("Error while handling the queue:", e) console.error("Error while handling the queue:", e)
@ -192,14 +206,11 @@ export class ImageUploadManager {
* - Applies the action to the correct element * - Applies the action to the correct element
* - indicates failure * - indicates failure
* *
* Modifies the queue: if the upload is successfull, deletes the item from the queue * Will _not_ modify the queue: if the upload is successful, deletes the item from the queue.
* @private * @returns true if successful (and the item should be deleted from the queue), false if something failed
*/ */
private async handleQueueItem(args: ImageUploadArguments): Promise<void> { private async handleQueueItem(args: NonNullable<ImageUploadArguments>): Promise<boolean> {
console.log("Handling queue item", args.blob.name, args) console.log("Handling queue item", args.blob.name, args)
if (!args) {
return
}
this._isUploading.set(args.featureId) this._isUploading.set(args.featureId)
let result: UploadResult = undefined let result: UploadResult = undefined
@ -221,7 +232,7 @@ export class ImageUploadManager {
if (result === undefined) { if (result === undefined) {
this._fails.data.push(args) this._fails.data.push(args)
this._fails.ping() this._fails.ping()
return return false
} }
let properties: UIEventSource<Record<string, string>> = this._featureProperties.getStore( let properties: UIEventSource<Record<string, string>> = this._featureProperties.getStore(
args.featureId args.featureId
@ -239,11 +250,12 @@ export class ImageUploadManager {
osmConnection: this._osmConnection, osmConnection: this._osmConnection,
}) })
} }
} else { return true
}
if (properties === undefined) { if (properties === undefined) {
const downloaded = await new OsmObjectDownloader( const downloaded = await new OsmObjectDownloader(
this._osmConnection.Backend(), this._osmConnection.Backend(),
this._changes this._changes,
).DownloadObjectAsync(args.featureId) ).DownloadObjectAsync(args.featureId)
if (downloaded === "deleted") { if (downloaded === "deleted") {
this._queue.delete(args) this._queue.delete(args)
@ -260,13 +272,11 @@ export class ImageUploadManager {
{ {
theme: args.layoutId ?? properties?.data?.["_orig_theme"] ?? this._theme.id, theme: args.layoutId ?? properties?.data?.["_orig_theme"] ?? this._theme.id,
changeType: "add-image", changeType: "add-image",
} },
) )
await this._changes.applyAction(action) await this._changes.applyAction(action)
await this._changes.flushChanges("Image upload completed") await this._changes.flushChanges("Image upload completed")
} return true
this._queue.delete(args)
} }
/** /**