Merge pull request #726 from sajith/3313.operations-ophandle-table-nevow-to-twisted

Replace nevow with twisted.web in operations.OphandleTable

Fixes: ticket:3313
This commit is contained in:
Sajith Sasidharan 2020-07-19 08:14:32 -04:00 committed by GitHub
commit 26ca9ae724
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 18 deletions

1
newsfragments/3313.minor Normal file
View File

@ -0,0 +1 @@
Replace nevow with twisted.web in web.operations.OphandleTable

View File

@ -1,19 +1,23 @@
import time import time
from nevow import rend, url from nevow import url
from nevow.inevow import IRequest
from twisted.web.template import ( from twisted.web.template import (
renderer, renderer,
tags as T, tags as T,
) )
from twisted.python.failure import Failure from twisted.python.failure import Failure
from twisted.internet import reactor, defer from twisted.internet import reactor, defer
from twisted.web import resource
from twisted.web.http import NOT_FOUND from twisted.web.http import NOT_FOUND
from twisted.web.html import escape from twisted.web.html import escape
from twisted.application import service from twisted.application import service
from allmydata.web.common import WebError, \ from allmydata.web.common import (
get_root, get_arg, boolean_of_arg WebError,
get_root,
get_arg,
boolean_of_arg,
)
MINUTE = 60 MINUTE = 60
HOUR = 60*MINUTE HOUR = 60*MINUTE
@ -21,13 +25,16 @@ DAY = 24*HOUR
(MONITOR, RENDERER, WHEN_ADDED) = range(3) (MONITOR, RENDERER, WHEN_ADDED) = range(3)
class OphandleTable(rend.Page, service.Service): class OphandleTable(resource.Resource, service.Service):
"""Renders /operations/%d."""
name = "operations" name = "operations"
UNCOLLECTED_HANDLE_LIFETIME = 4*DAY UNCOLLECTED_HANDLE_LIFETIME = 4*DAY
COLLECTED_HANDLE_LIFETIME = 1*DAY COLLECTED_HANDLE_LIFETIME = 1*DAY
def __init__(self, clock=None): def __init__(self, clock=None):
super(OphandleTable, self).__init__()
# both of these are indexed by ophandle # both of these are indexed by ophandle
self.handles = {} # tuple of (monitor, renderer, when_added) self.handles = {} # tuple of (monitor, renderer, when_added)
self.timers = {} self.timers = {}
@ -45,12 +52,17 @@ class OphandleTable(rend.Page, service.Service):
del self.timers del self.timers
return service.Service.stopService(self) return service.Service.stopService(self)
def add_monitor(self, ctx, monitor, renderer): def add_monitor(self, req, monitor, renderer):
ophandle = get_arg(ctx, "ophandle") """
:param allmydata.webish.MyRequest req:
:param allmydata.monitor.Monitor monitor:
:param allmydata.web.directory.ManifestResults renderer:
"""
ophandle = get_arg(req, "ophandle")
assert ophandle assert ophandle
now = time.time() now = time.time()
self.handles[ophandle] = (monitor, renderer, now) self.handles[ophandle] = (monitor, renderer, now)
retain_for = get_arg(ctx, "retain-for", None) retain_for = get_arg(req, "retain-for", None)
if retain_for is not None: if retain_for is not None:
self._set_timer(ophandle, int(retain_for)) self._set_timer(ophandle, int(retain_for))
monitor.when_done().addBoth(self._operation_complete, ophandle) monitor.when_done().addBoth(self._operation_complete, ophandle)
@ -67,36 +79,42 @@ class OphandleTable(rend.Page, service.Service):
# if we already have a timer, the client must have provided the # if we already have a timer, the client must have provided the
# retain-for= value, so don't touch it. # retain-for= value, so don't touch it.
def redirect_to(self, ctx): def redirect_to(self, req):
ophandle = get_arg(ctx, "ophandle") """
:param allmydata.webish.MyRequest req:
"""
ophandle = get_arg(req, "ophandle")
assert ophandle assert ophandle
target = get_root(ctx) + "/operations/" + ophandle target = get_root(req) + "/operations/" + ophandle
output = get_arg(ctx, "output") output = get_arg(req, "output")
if output: if output:
target = target + "?output=%s" % output target = target + "?output=%s" % output
# XXX: We have to use nevow.url here because nevow.appserver
# is unhappy with anything else; so this gets its own ticket.
# https://tahoe-lafs.org/trac/tahoe-lafs/ticket/3314
return url.URL.fromString(target) return url.URL.fromString(target)
def childFactory(self, ctx, name): def getChild(self, name, req):
ophandle = name ophandle = name
if ophandle not in self.handles: if ophandle not in self.handles:
raise WebError("unknown/expired handle '%s'" % escape(ophandle), raise WebError("unknown/expired handle '%s'" % escape(ophandle),
NOT_FOUND) NOT_FOUND)
(monitor, renderer, when_added) = self.handles[ophandle] (monitor, renderer, when_added) = self.handles[ophandle]
request = IRequest(ctx) t = get_arg(req, "t", "status")
t = get_arg(ctx, "t", "status") if t == "cancel" and req.method == "POST":
if t == "cancel" and request.method == "POST":
monitor.cancel() monitor.cancel()
# return the status anyways, but release the handle # return the status anyways, but release the handle
self._release_ophandle(ophandle) self._release_ophandle(ophandle)
else: else:
retain_for = get_arg(ctx, "retain-for", None) retain_for = get_arg(req, "retain-for", None)
if retain_for is not None: if retain_for is not None:
self._set_timer(ophandle, int(retain_for)) self._set_timer(ophandle, int(retain_for))
if monitor.is_finished(): if monitor.is_finished():
if boolean_of_arg(get_arg(ctx, "release-after-complete", "false")): if boolean_of_arg(get_arg(req, "release-after-complete", "false")):
self._release_ophandle(ophandle) self._release_ophandle(ophandle)
if retain_for is None: if retain_for is None:
# this GET is collecting the ophandle, so change its timer # this GET is collecting the ophandle, so change its timer
@ -123,6 +141,7 @@ class OphandleTable(rend.Page, service.Service):
self.timers.pop(ophandle, None) self.timers.pop(ophandle, None)
self.handles.pop(ophandle, None) self.handles.pop(ophandle, None)
class ReloadMixin(object): class ReloadMixin(object):
REFRESH_TIME = 1*MINUTE REFRESH_TIME = 1*MINUTE