Configure server cache time from the command line (--cached-time) #901 / Also implement cache time for the Web server mode

This commit is contained in:
nicolargo 2016-09-25 22:12:58 +02:00
parent 57e432a526
commit 4c5105aa99
4 changed files with 27 additions and 13 deletions

1
NEWS
View File

@ -10,6 +10,7 @@ Enhancements and new features:
* Add CouchDB exporter (issue #928)
* Highlight max stats in the processes list (issue #878)
* Glances API returns the processes PPID (issue #926)
* Configure server cached time from the command line --cached-time (issue #901)
Bugs corrected:

View File

@ -168,12 +168,12 @@ def main():
from glances.server import GlancesServer
args = core.get_args()
print args.cached_time
server = GlancesServer(cached_time=core.cached_time,
server = GlancesServer(cached_time=args.cached_time,
config=core.get_config(),
args=args)
print('Glances server is running on {}:{}'.format(args.bind_address, args.port))
# Set the server login/password (if -P/--password tag)
if args.password != "":
server.add_user(args.username, args.password)

View File

@ -25,7 +25,7 @@ import sys
import tempfile
from glances import __appname__, __version__, psutil_version
from glances.compat import input
from glances.compat import input, NoOptionError, NoSectionError
from glances.config import Config
from glances.globals import LINUX, WINDOWS
from glances.logger import logger
@ -37,9 +37,7 @@ class GlancesMain(object):
# Default stats' refresh time is 3 seconds
refresh_time = 3
# Set the default cache lifetime to 1 second (only for server)
# !!! Todo: To be configurable (=> https://github.com/nicolargo/glances/issues/901)
cached_time = 1
# By default, Glances is ran in standalone mode (no client/server)
client_tag = False
@ -83,6 +81,7 @@ Start the client browser (browser mode):\n\
def __init__(self):
"""Manage the command line arguments."""
# Read the command line arguments
self.args = self.parse_args()
def init_args(self):
@ -210,6 +209,8 @@ Start the client browser (browser mode):\n\
dest='time', help='set refresh time in seconds [default: {} sec]'.format(self.refresh_time))
parser.add_argument('-w', '--webserver', action='store_true', default=False,
dest='webserver', help='run Glances in web server mode (bottle needed)')
parser.add_argument('--cached-time', default=self.cached_time, type=int,
dest='cached_time', help='set the server cache time [default: {} sec]'.format(self.cached_time))
# Display options
parser.add_argument('-q', '--quiet', default=False, action='store_true',
dest='quiet', help='do not display the curses interface')

View File

@ -25,6 +25,7 @@ import sys
import tempfile
from io import open
from glances.timer import Timer
from glances.logger import logger
try:
@ -46,6 +47,11 @@ class GlancesBottle(object):
# Will be updated within Bottle route
self.stats = None
# cached_time is the minimum time interval between stats updates
# i.e. HTTP/Restful calls will not retrieve updated info until the time
# since last update is passed (will retrieve old cached info instead)
self.timer = Timer(0)
# Init Bottle
self._app = Bottle()
# Enable CORS (issue #479)
@ -59,6 +65,12 @@ class GlancesBottle(object):
# Path where the statics files are stored
self.STATIC_PATH = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'static/public')
def __update__(self):
# Never update more than 1 time per cached_time
if self.timer.finished():
self.stats.update()
self.timer = Timer(self.args.cached_time)
def app(self):
return self._app()
@ -117,7 +129,7 @@ class GlancesBottle(object):
def _index(self, refresh_time=None):
"""Bottle callback for index.html (/) file."""
# Update the stat
self.stats.update()
self.__update__()
# Display
return static_file("index.html", root=self.STATIC_PATH)
@ -170,7 +182,7 @@ class GlancesBottle(object):
response.content_type = 'application/json'
# Update the stat
self.stats.update()
self.__update__()
try:
plist = json.dumps(self.plugins_list)
@ -197,7 +209,7 @@ class GlancesBottle(object):
logger.debug("Debug file (%s) not found" % fname)
# Update the stat
self.stats.update()
self.__update__()
try:
# Get the JSON value of the stat ID
@ -254,7 +266,7 @@ class GlancesBottle(object):
abort(400, "Unknown plugin %s (available plugins: %s)" % (plugin, self.plugins_list))
# Update the stat
self.stats.update()
self.__update__()
try:
# Get the JSON value of the stat ID
@ -278,7 +290,7 @@ class GlancesBottle(object):
abort(400, "Unknown plugin %s (available plugins: %s)" % (plugin, self.plugins_list))
# Update the stat
self.stats.update()
self.__update__()
try:
# Get the JSON value of the stat ID
@ -301,7 +313,7 @@ class GlancesBottle(object):
abort(400, "Unknown plugin %s (available plugins: %s)" % (plugin, self.plugins_list))
# Update the stat
# self.stats.update()
# self.__update__()
try:
# Get the JSON value of the stat limits
@ -324,7 +336,7 @@ class GlancesBottle(object):
abort(400, "Unknown plugin %s (available plugins: %s)" % (plugin, self.plugins_list))
# Update the stat
# self.stats.update()
# self.__update__()
try:
# Get the JSON value of the stat views
@ -341,7 +353,7 @@ class GlancesBottle(object):
abort(400, "Unknown plugin %s (available plugins: %s)" % (plugin, self.plugins_list))
# Update the stat
self.stats.update()
self.__update__()
if value is None:
if history: