Ejemplo n.º 1
0
def metrics_index():
    """Display some metrics."""

    return render_template(
        "metrics.html",
        new=len(utils.list_keys(Keys.new.value)),
        pending=len(utils.list_keys(Keys.pending.value)),
        processing=len(utils.list_keys(Keys.processing.value)),
        completed=len(utils.list_data()),
        worker=not APP.knife_worker.dead,
        now=datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%SZ"),
    )
Ejemplo n.º 2
0
def main():
    """Main worker entrypoint."""

    LOG.warning("worker online")

    # until we can resume jobs
    for state in (Keys.processing.value, Keys.pending.value):
        CACHE.delete_many(*utils.list_keys(state))

    while True:
        prune = []

        for glet in WORKERS:
            if glet.successful():
                prune.append(glet)
            elif glet.dead:
                LOG.warning(
                    "worker crashed: %s",
                    "".join(format_exception(*glet.exc_info)).strip(),
                )
                prune.append(glet)

        for glet in prune:
            WORKERS.remove(glet)

        process_new()

        gc.collect()
        gevent.sleep(10)
Ejemplo n.º 3
0
def get_knife(token):
    """Direct URL access to a knife result."""

    if utils.rate_limit():
        return Response(
            "chill out bruh, maybe you need to run a self-hosted copy",
            status=420,
        )

    results = utils.get_data(token)
    if results is None:
        for state in (Keys.pending, Keys.processing, Keys.new):
            if utils.list_keys("{}{}".format(state.value, token)):
                return render_template(
                    "pending.html",
                    token=token,
                    state=state.value,
                )
        return redirect("/?e=invalid_token")

    if request.headers.get("Accept") == "application/json":
        return Response(
            ujson.dumps(results, sort_keys=True, indent=4),
            content_type="application/json",
        )

    return render_template(
        "view.html",
        data=ujson.dumps(results, sort_keys=True),
        exposed_url=EXPOSED_URL,
        token=token,
    )
Ejemplo n.º 4
0
def process_new():
    """Process all new tokens, verify or we're done early."""

    for new_key in utils.list_keys(Keys.new.value):
        uuid = new_key.split(".")[-1]
        LOG.warning("processing new uuid: %r", uuid)

        token = CACHE.get(new_key)
        CACHE.delete(new_key)

        if not token:
            LOG.warning("no token stored for uuid: %r", uuid)
            continue

        pending_key = "{}{}".format(Keys.pending.value, uuid)
        CACHE.set(
            pending_key,
            "1",
            timeout=70,
        )
        headers = {"Authorization": "Bearer {}".format(token)}
        _, res = utils.request_or_wait(
            "{}/verify/".format(ESI),
            headers=headers,
        )

        failed = False
        if isinstance(res, str) or "CharacterID" not in res:
            utils.write_data(uuid, {"auth failure": res})
            failed = True
        else:
            _, roles = utils.request_or_wait(
                "{}/latest/characters/{}/roles/".format(
                    ESI,
                    res["CharacterID"],
                ),
                headers=headers,
            )
            if isinstance(roles, str):
                utils.write_data(uuid, {"roles failure": roles})
                failed = True

        CACHE.delete(pending_key)

        if not failed:
            CACHE.set(
                "{}{}".format(Keys.processing.value, uuid),
                res["CharacterID"],
                timeout=7200,
            )

            WORKERS.append(
                gevent.spawn(knife, uuid, token, res, roles)
            )