Example #1
0
def term_proc(proc, term_wait_time=3):
    global pid_to_procs
    if proc.poll() is None:
        LOG.debug(
            "Terminating process %i and waiting up to %i seconds for it to end...",
            proc.pid,
            term_wait_time,
        )
        proc.terminate()
        try:
            proc.wait(term_wait_time)
        except TimeoutExpired:
            pass

    if proc.poll() is None:
        LOG.warning(
            "Process %i didn't terminate. Killing process and waiting until process exits...",
            proc.pid,
        )
        proc.kill()
        proc.wait()

    if proc.stdout:
        proc.stdout.close()

    if proc.stderr:
        proc.stder.close()

    if proc.stdin:
        proc.stdin.close()

    assert proc.poll(
    ) is not None, "Proc %i didn't terminate properly" % proc.pid

    LOG.debug("Process %i terminated with %i", proc.pid, proc.poll())
Example #2
0
    def fetch_json_api(url, use_cache_on_failure=False):
        LOG.debug("Fetching %s...", url)
        num_tries = 0
        while True:
            resp_json = Fetcher._try_fetching_resp(url)
            LOG.debug("Response from %s: %s", url, resp_json)

            if resp_json:
                Fetcher._cache[url] = resp_json
                return resp_json
            elif use_cache_on_failure and url in Fetcher._cache:
                LOG.warn(
                    "Bad response from '%s\'! Falling back to cache...\033[0m\n",
                    url)
                return Fetcher._cache[url]

            num_tries += 1
            sleep_time = min(2**num_tries - 1, Fetcher._MAX_WAIT)
            LOG.warning(
                "Bad response from '%s\'! Backing off for %i second(s)...",
                url, sleep_time)
            sleep(sleep_time)