Added "priority" property to build queue items.

/render APIs use priority 10, whereas /arbitrary use priority 0, to prevent thumbnail downloads from holding up website loading. The priorities can be adjusted later, with maybe some service types being given higher priority than others.
This commit is contained in:
CalDescent 2022-02-11 15:08:12 +00:00
parent 2343e739d1
commit 6932fb9935
5 changed files with 19 additions and 6 deletions

View File

@ -1044,7 +1044,7 @@ public class ArbitraryResource {
// Loop until we have data // Loop until we have data
if (async) { if (async) {
// Asynchronous // Asynchronous
arbitraryDataReader.loadAsynchronously(false); arbitraryDataReader.loadAsynchronously(false, 0);
} }
else { else {
// Synchronous // Synchronous

View File

@ -13,6 +13,7 @@ public class ArbitraryDataBuildQueueItem extends ArbitraryDataResource {
private final Long creationTimestamp; private final Long creationTimestamp;
private Long buildStartTimestamp = null; private Long buildStartTimestamp = null;
private Long buildEndTimestamp = null; private Long buildEndTimestamp = null;
private Integer priority = 0;
private boolean failed = false; private boolean failed = false;
/* The maximum amount of time to spend on a single build */ /* The maximum amount of time to spend on a single build */
@ -77,6 +78,14 @@ public class ArbitraryDataBuildQueueItem extends ArbitraryDataResource {
return this.buildStartTimestamp; return this.buildStartTimestamp;
} }
public Integer getPriority() {
return this.priority;
}
public void setPriority(Integer priority) {
this.priority = priority;
}
public void setFailed(boolean failed) { public void setFailed(boolean failed) {
this.failed = failed; this.failed = failed;
} }

View File

@ -126,7 +126,7 @@ public class ArbitraryDataReader {
* @param overwrite - set to true to force rebuild an existing cache * @param overwrite - set to true to force rebuild an existing cache
* @return true if added or already present in queue; false if not * @return true if added or already present in queue; false if not
*/ */
public boolean loadAsynchronously(boolean overwrite) { public boolean loadAsynchronously(boolean overwrite, int priority) {
ArbitraryDataCache cache = new ArbitraryDataCache(this.uncompressedPath, overwrite, ArbitraryDataCache cache = new ArbitraryDataCache(this.uncompressedPath, overwrite,
this.resourceId, this.resourceIdType, this.service, this.identifier); this.resourceId, this.resourceIdType, this.service, this.identifier);
if (cache.isCachedDataAvailable()) { if (cache.isCachedDataAvailable()) {
@ -135,7 +135,9 @@ public class ArbitraryDataReader {
return true; return true;
} }
return ArbitraryDataBuildManager.getInstance().addToBuildQueue(this.createQueueItem()); ArbitraryDataBuildQueueItem item = this.createQueueItem();
item.setPriority(priority);
return ArbitraryDataBuildManager.getInstance().addToBuildQueue(item);
} }
/** /**

View File

@ -76,7 +76,7 @@ public class ArbitraryDataRenderer {
if (!arbitraryDataReader.isCachedDataAvailable()) { if (!arbitraryDataReader.isCachedDataAvailable()) {
// If async is requested, show a loading screen whilst build is in progress // If async is requested, show a loading screen whilst build is in progress
if (async) { if (async) {
arbitraryDataReader.loadAsynchronously(false); arbitraryDataReader.loadAsynchronously(false, 10);
return this.getLoadingResponse(service, resourceId); return this.getLoadingResponse(service, resourceId);
} }

View File

@ -9,6 +9,7 @@ import org.qortal.repository.DataException;
import org.qortal.utils.NTP; import org.qortal.utils.NTP;
import java.io.IOException; import java.io.IOException;
import java.util.Comparator;
import java.util.Map; import java.util.Map;
@ -43,12 +44,13 @@ public class ArbitraryDataBuilderThread implements Runnable {
ArbitraryDataBuildQueueItem queueItem = null; ArbitraryDataBuildQueueItem queueItem = null;
// Find resources that are queued for building // Find resources that are queued for building (sorted by highest priority first)
synchronized (buildManager.arbitraryDataBuildQueue) { synchronized (buildManager.arbitraryDataBuildQueue) {
Map.Entry<String, ArbitraryDataBuildQueueItem> next = buildManager.arbitraryDataBuildQueue Map.Entry<String, ArbitraryDataBuildQueueItem> next = buildManager.arbitraryDataBuildQueue
.entrySet().stream() .entrySet().stream()
.filter(e -> e.getValue().isQueued()) .filter(e -> e.getValue().isQueued())
.findFirst().orElse(null); .sorted(Comparator.comparing(item -> item.getValue().getPriority()))
.reduce((first, second) -> second).orElse(null);
if (next == null) { if (next == null) {
continue; continue;