<commit_before>import json
import math
import re
import random
import os
import sys
from collections import defaultdict #Counter
from cgi import parse_qs, escape

import zmq

_defaults = {
    'connection' : 'tcp://localhost:5151'
}

def not_found(environ, start_response):
    """Called if no URL matches."""
    start_response('404 NOT FOUND', [('Content-Type', 'text/plain')])
    return [environ.get('PATH_INFO', '').lstrip('/')]

def stats(environ, start_response):
    sys.stderr.write('global stats\n')
    start_response('200 OK', [('Content-Type', 'text/plain')])
    response_body = { "featureDensity": 0.02,
                      "scoreMin": 0,
                      "scoreMax": 1,
                      "featureDensityByRegion" : 50000,
                    }
    return json.dumps(response_body)

def status(environ, start_response):
    start_response('200 OK', [('Content-Type', 'application/json')])

    result = {}

    try:
        args = environ['url_args']
        identity = args['id']
    except KeyError:
        return json.dumps(result)

    if not identity:
        return json.dumps(result)

    try:
        context = zmq.Context()
        socket = context.socket(zmq.REQ)
        socket.connect(_defaults['connection'])
        request = {
            'request' : 'get_status',
            'data' : {
                'id' : args['id']
            },
        }

        socket.send_json(request)
        result = socket.recv_json()
    except:
        result = {}

    return json.dumps(result)

urls = [
    (r"synmap/status/(?P<id>[a-z0-9\-]+)?", status),
    (r"synfind/status/(?P<id>[a-z0-9\-]+)?", status),
]

def application(environ, start_response):
    """
    The main WSGI application. Dispatch the current request to
    the functions from above and store the regular expression
    captures in the WSGI environment as  `myapp.url_args` so that<commit_msg>Added timeout when job engine is not running.<commit_after>import json
import itertools
import math
import re
import random
import os
import sys
from collections import defaultdict #Counter
from cgi import parse_qs, escape

import zmq

_defaults = {
    'connection' : 'tcp://localhost:5151',
    'max_attempts' : 5,
    'timeout' : 1000
}

def not_found(environ, start_response):
    """Called if no URL matches."""
    start_response('404 NOT FOUND', [('Content-Type', 'text/plain')])
    return [environ.get('PATH_INFO', '').lstrip('/')]

def stats(environ, start_response):
    sys.stderr.write('global stats\n')
    start_response('200 OK', [('Content-Type', 'text/plain')])
    response_body = { "featureDensity": 0.02,
                      "scoreMin": 0,
                      "scoreMax": 1,
                      "featureDensityByRegion" : 50000,
                    }
    return json.dumps(response_body)

def status(environ, start_response):
    start_response('200 OK', [('Content-Type', 'application/json')])

    result = None

    try:
        args = environ['url_args']
        identity = args['id']
    except KeyError:
        return json.dumps({})

    if not identity:
        return json.dumps({})

    context = zmq.Context()
    socket = context.socket(zmq.REQ)
    socket.setsockopt(zmq.LINGER, 0)
    socket.connect(_defaults['connection'])
    request = {
        'request' : 'get_status',
        'data' : {
            'id' : args['id']
        },
    }

    poller = zmq.Poller()
    poller.register(socket, zmq.POLLIN)
    counter = itertools.count()

    socket.send_json(request, zmq.NOBLOCK)

    while _defaults['max_attempts'] > counter.next() and not result:
        sys.stderr.write('waiting...\n');
        if socket in dict(poller.poll(timeout=_defaults['timeout'])):
            result = socket.recv_json(flags=zmq.NOBLOCK)

    socket.close()
    context.term()

    if not result:
        result = {"error" : 1}

    return json.dumps(result)

urls = [
    (r"synmap/status/(?P<id>[a-z0-9\-]+)?", status),
    (r"synfind/status/(?P<id>[a-z0-9\-]+)?", status),
]

def application(environ, start_response):
    """
    The main WSGI application. Dispatch the current request to
    the functions from above and store the regular expression
    captures in the WSGI environment as  `myapp.url_args` so that
{'fraction_non_alphanumeric': 0.1400943396226415, 'fraction_numerical': 0.014386792452830189, 'mean_word_length': 2.640343347639485, 'pattern_counts': {'":': 6, '<': 7, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6159029', 'n_tokens_mistral': 1367, 'n_tokens_neox': 1311, 'n_words': 316}