Пример #1
0
 def _server_submit(self, json):
     # submit json to server
     postdata = urlencode({'json': setupfile.encode_setup(json)})
     res = self._url_json('submit', data=postdata)
     if 'error' in res:
         raise ServerError('Submit failed: ' + res.error)
     if 'why_build' not in res:
         if not self.subjob_cookie:
             self._printlist(res.jobs)
         self.validate_response(res.jobs)
     return res
Пример #2
0
def build(method,
          options={},
          datasets={},
          jobs={},
          name=None,
          caption=None,
          **kw):
    """Just like urd.build, but for making subjobs"""

    global _a, _bad_kws
    assert g.running != 'analysis', "Analysis is not allowed to make subjobs"
    assert g.subjob_cookie, "Can't build subjobs: out of cookies"
    if not _a:
        _a = Automata(g.server_url, subjob_cookie=g.subjob_cookie)
        _a.update_method_info()
        _a.record[None] = _a.jobs = globals()['jobs']
        _bad_kws = set(getarglist(_a.call_method))
    bad_kws = _bad_kws & set(kw)
    if bad_kws:
        raise Exception('subjobs.build does not accept these keywords: %r' %
                        (bad_kws, ))

    def run():
        return _a.call_method(method,
                              options=options,
                              datasets=datasets,
                              jobs=jobs,
                              record_as=name,
                              caption=caption,
                              **kw)

    try:
        if name or caption:
            msg = 'Building subjob %s' % (name or method, )
            if caption:
                msg += ' "%s"' % (caption, )
            with status(msg):
                jid = run()
        else:
            jid = run()
    except ServerError as e:
        raise ServerError(e.args[0])
    except JobError as e:
        raise JobError(e.job, e.method, e.status)
    for d in _a.job_retur.jobs.values():
        if d.link not in _record:
            _record[d.link] = bool(d.make)
    return jid
Пример #3
0
def call(url,
         data=None,
         fmt=json_decode,
         headers={},
         server_name='server',
         retries=4,
         quiet=False):
    if data is not None and not isinstance(data, bytes):
        data = json_encode(data)
    err = None
    req = Request(url, data=data, headers=headers)
    for attempt in range(1, retries + 2):
        resp = None
        try:
            r = urlopen(req)
            try:
                resp = r.read()
                if server_name == 'server' and g.running in (
                        'build',
                        'shell',
                ):
                    s_version = r.headers[
                        'Accelerator-Version'] or '<unknown (old)>'
                    if s_version != ax_version:
                        # Nothing is supposed to catch this, so just print and die.
                        print(
                            'Server is running version %s but we are running version %s'
                            % (
                                s_version,
                                ax_version,
                            ),
                            file=sys.stderr)
                        exit(1)
                if PY3:
                    resp = resp.decode('utf-8')
                # It is inconsistent if we get HTTPError or not.
                # It seems we do when using TCP sockets, but not when using unix sockets.
                if r.getcode() >= 400:
                    raise HTTPError(url, r.getcode(), resp, {}, None)
                return fmt(resp)
            finally:
                try:
                    r.close()
                except Exception:
                    pass
        except HTTPError as e:
            if resp is None and e.fp:
                resp = e.fp.read()
                if PY3:
                    resp = resp.decode('utf-8')
            msg = '%s says %d: %s' % (
                server_name,
                e.code,
                resp,
            )
            if server_name == 'urd' and 400 <= e.code < 500:
                if e.code == 401:
                    err = UrdPermissionError()
                if e.code == 409:
                    err = UrdConflictError()
                break
            if server_name == 'server' and e.code != 503 and resp:
                return fmt(resp)
        except URLError:
            # Don't say anything the first times, because the output
            # tests get messed up if this happens during them.
            if attempt < retries - 1:
                msg = None
            else:
                msg = 'error contacting ' + server_name
        except ValueError as e:
            msg = 'Bad data from %s, %s: %s' % (
                server_name,
                type(e).__name__,
                e,
            )
        if msg and not quiet:
            print(msg, file=sys.stderr)
        if attempt < retries + 1:
            time.sleep(attempt / 15)
            if msg and not quiet:
                print('Retrying (%d/%d).' % (
                    attempt,
                    retries,
                ),
                      file=sys.stderr)
    else:
        if not quiet:
            print('Giving up.', file=sys.stderr)
    if err:
        raise err
    if server_name == 'urd':
        raise UrdError(msg)
    else:
        raise ServerError(msg)