From aa57277ff81b23c390694eecf99bb78291837575 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Thu, 18 Aug 2016 08:39:19 +0200 Subject: [PATCH 1/2] Only report files as enqueued for analysis which actually are --- src/octoprint/filemanager/__init__.py | 4 ++-- src/octoprint/filemanager/analysis.py | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/octoprint/filemanager/__init__.py b/src/octoprint/filemanager/__init__.py index 184d8108..ec4d1eea 100644 --- a/src/octoprint/filemanager/__init__.py +++ b/src/octoprint/filemanager/__init__.py @@ -212,8 +212,8 @@ class FileManager(object): # we'll use the default printer profile for the backlog since we don't know better queue_entry = QueueEntry(entry, file_type, storage_type, path, self._printer_profile_manager.get_default()) - self._analysis_queue.enqueue(queue_entry, high_priority=False) - counter += 1 + if self._analysis_queue.enqueue(queue_entry, high_priority=False): + counter += 1 self._logger.info("Added {counter} items from storage type \"{storage_type}\" to analysis queue".format(**locals())) def add_storage(self, storage_type, storage_manager): diff --git a/src/octoprint/filemanager/analysis.py b/src/octoprint/filemanager/analysis.py index cd4ca7fd..95fdaa9b 100644 --- a/src/octoprint/filemanager/analysis.py +++ b/src/octoprint/filemanager/analysis.py @@ -68,9 +68,10 @@ class AnalysisQueue(object): def enqueue(self, entry, high_priority=False): if not entry.type in self._queues: - return + return False self._queues[entry.type].enqueue(entry, high_priority=high_priority) + return True def pause(self): for queue in self._queues.values(): From 599098a5897ef164adcb8915c877f1009702e6a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Thu, 18 Aug 2016 12:27:30 +0200 Subject: [PATCH 2/2] Have intermediary reload only after preliminary caching --- src/octoprint/server/util/flask.py | 15 +++++++++++++ src/octoprint/server/views.py | 14 ++++++++++++- src/octoprint/static/intermediary.html | 29 ++++++++++++++++++++------ 3 files changed, 51 insertions(+), 7 deletions(-) diff --git a/src/octoprint/server/util/flask.py b/src/octoprint/server/util/flask.py index 649704da..11ef9bc3 100644 --- a/src/octoprint/server/util/flask.py +++ b/src/octoprint/server/util/flask.py @@ -312,6 +312,18 @@ class LessSimpleCache(BaseCache): return False return len(self._cache) > self._threshold + def __getitem__(self, key): + return self.get(key) + + def __setitem__(self, key, value): + return self.set(key, value) + + def __delitem__(self, key): + return self.delete(key) + + def __contains__(self, key): + return key in self._cache + _cache = LessSimpleCache() def cached(timeout=5 * 60, key=lambda: "view:%s" % flask.request.path, unless=None, refreshif=None, unless_response=None): @@ -358,6 +370,9 @@ def cached(timeout=5 * 60, key=lambda: "view:%s" % flask.request.path, unless=No return decorator +def is_in_cache(key=lambda: "view:%s" % flask.request.path): + return key() in _cache + def cache_check_headers(): return "no-cache" in flask.request.cache_control or "no-cache" in flask.request.pragma diff --git a/src/octoprint/server/views.py b/src/octoprint/server/views.py index 0a825e2f..bcc29ce1 100644 --- a/src/octoprint/server/views.py +++ b/src/octoprint/server/views.py @@ -9,7 +9,7 @@ import os import datetime from collections import defaultdict -from flask import request, g, url_for, make_response, render_template, send_from_directory, redirect +from flask import request, g, url_for, make_response, render_template, send_from_directory, redirect, abort import octoprint.plugin @@ -20,6 +20,7 @@ from octoprint.settings import settings from octoprint.filemanager import get_all_extensions import re +import base64 from . import util @@ -29,6 +30,17 @@ _logger = logging.getLogger(__name__) _valid_id_re = re.compile("[a-z_]+") _valid_div_re = re.compile("[a-zA-Z_-]+") +@app.route("/cached.gif") +def in_cache(): + url = request.base_url.replace("/cached.gif", "/") + key = lambda: "view:{}:{}".format(url, g.locale.language if g.locale else "en") + if not util.flask.is_in_cache(key): + return abort(404) + + response = make_response(bytes(base64.b64decode("R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7"))) + response.headers["Content-Type"] = "image/gif" + return response + @app.route("/") @util.flask.preemptively_cached(cache=preemptiveCache, data=lambda: dict(path=request.path, base_url=request.url_root, query_string="l10n={}".format(g.locale.language) if g.locale else "en"), diff --git a/src/octoprint/static/intermediary.html b/src/octoprint/static/intermediary.html index 1f8b3919..56c621e5 100644 --- a/src/octoprint/static/intermediary.html +++ b/src/octoprint/static/intermediary.html @@ -153,20 +153,20 @@ if (baseUrl[baseUrl.length - 1] != "/") { baseUrl += "/"; } + var indexCachedUrl = baseUrl + "cached.gif"; var serverOnlineUrl = baseUrl + "online.gif"; var backendOnlineUrl = baseUrl + "intermediary.gif"; var serverTimeout; + var cachedTimeout; var message = window.document.getElementById("message"); - var serverIsOnline = false; - var serverOnlineCallback = function(result) { + var indexCachedCallback = function(result) { if (result == "load") { - // our online.gif loaded, so the server is up, let's reload - serverIsOnline = true; + // our cached.gif loaded, so the index is cached now, let's reload message.className = "pulsate1 green"; - message.innerText = "OctoPrint server online, reloading page..."; + message.innerText = "OctoPrint server ready, reloading page..."; var reloadUrl = baseUrl + currentQuery + currentFragment; if (reloadUrl == window.location.href) { @@ -175,7 +175,21 @@ window.location.href = reloadUrl; } } else { - // online.gif still not available, let's look at + // cached.gif still not available, let's look at it again in a second + cachedTimeout = setTimeout(function() { + ping(indexCachedUrl, timeout, indexCachedCallback); + }, 1000) + } + }; + + var serverIsOnline = false; + var serverOnlineCallback = function(result) { + if (result == "load") { + // our online.gif loaded, so the server is up, let's wait for it to become ready + serverIsOnline = true; + ping(indexCachedUrl, timeout, indexCachedCallback); + } else { + // online.gif still not available, let's look at it again later var interval = 10; if (intervals.length) { interval = intervals.shift(); @@ -204,6 +218,9 @@ if (serverTimeout) { window.clearTimeout(serverTimeout); } + if (cachedTimeout) { + window.clearTimeout(cachedTimeout); + } message.className = "red"; message.innerHTML = "Looks like something went wrong during startup, the server is gone again. You should check octoprint.log.";