3 from allmydata.scripts.common import get_alias, DEFAULT_ALIAS, escape_path
4 from allmydata.scripts.common_http import do_http
5 from allmydata.util import base32
9 class SlowOperationRunner:
11 def run(self, options):
12 stderr = options.stderr
13 self.options = options
14 self.ophandle = ophandle = base32.b2a(os.urandom(16))
15 nodeurl = options['node-url']
16 if not nodeurl.endswith("/"):
18 self.nodeurl = nodeurl
20 rootcap, path = get_alias(options.aliases, where, DEFAULT_ALIAS)
23 url = nodeurl + "uri/%s" % urllib.quote(rootcap)
25 url += "/" + escape_path(path)
26 # todo: should it end with a slash?
27 url = self.make_url(url, ophandle)
28 resp = do_http("POST", url)
29 if resp.status not in (200, 302):
30 print >>stderr, "ERROR", resp.status, resp.reason, resp.read()
32 # now we poll for results. We nominally poll at t=1, 5, 10, 30, 60,
33 # 90, k*120 seconds, but if the poll takes non-zero time, that will
34 # be slightly longer. I'm not worried about trying to make up for
37 return self.wait_for_results()
40 for i in (1,5,10,30,60,90):
47 def wait_for_results(self):
49 for next in self.poll_times():
57 url = self.nodeurl + "operations/" + self.ophandle
58 url += "?t=status&output=JSON&release-after-complete=true"
59 stdout = self.options.stdout
60 stderr = self.options.stderr
61 resp = do_http("GET", url)
62 if resp.status != 200:
63 print >>stderr, "ERROR", resp.status, resp.reason, resp.read()
66 data = simplejson.loads(jdata)
67 if not data["finished"]:
69 if self.options.get("raw"):
72 self.write_results(data)