3 from allmydata.scripts.common import get_alias, DEFAULT_ALIAS, escape_path, \
5 from allmydata.scripts.common_http import do_http
6 from allmydata.util import base32
10 class SlowOperationRunner:
12 def run(self, options):
13 stderr = options.stderr
14 self.options = options
15 self.ophandle = ophandle = base32.b2a(os.urandom(16))
16 nodeurl = options['node-url']
17 if not nodeurl.endswith("/"):
19 self.nodeurl = nodeurl
22 rootcap, path = get_alias(options.aliases, where, DEFAULT_ALIAS)
23 except UnknownAliasError, e:
24 print >>stderr, "error: %s" % e.args[0]
28 url = nodeurl + "uri/%s" % urllib.quote(rootcap)
30 url += "/" + escape_path(path)
31 # todo: should it end with a slash?
32 url = self.make_url(url, ophandle)
33 resp = do_http("POST", url)
34 if resp.status not in (200, 302):
35 print >>stderr, "ERROR", resp.status, resp.reason, resp.read()
37 # now we poll for results. We nominally poll at t=1, 5, 10, 30, 60,
38 # 90, k*120 seconds, but if the poll takes non-zero time, that will
39 # be slightly longer. I'm not worried about trying to make up for
42 return self.wait_for_results()
45 for i in (1,5,10,30,60,90):
52 def wait_for_results(self):
54 for next in self.poll_times():
62 url = self.nodeurl + "operations/" + self.ophandle
63 url += "?t=status&output=JSON&release-after-complete=true"
64 stdout = self.options.stdout
65 stderr = self.options.stderr
66 resp = do_http("GET", url)
67 if resp.status != 200:
68 print >>stderr, "ERROR", resp.status, resp.reason, resp.read()
71 data = simplejson.loads(jdata)
72 if not data["finished"]:
74 if self.options.get("raw"):
77 self.write_results(data)