# Path: sio/protocol/rpc.py
# class RemoteError(Exception):
#     def __init__(self, err, tb=None, uid=None):
#         if uid is not None:
#             err = "received from " + uid + ("" if err is None else ": " + err)
#         super(RemoteError, self).__init__(err)
#         self.traceback = tb
#         self.uid = uid
# 
# Path: sio/sioworkersd/utils.py
# def get_required_ram_for_job(env):
#     job_type = env['job_type']
#     if job_type.endswith('exec'):
#         required_ram = env.get('exec_mem_limit', DEFAULT_RAM_REQUIREMENTS['exec'])
#         # We need to make sure that we have enough ram for a checker as well.
#         if env.get('check_output'):
#             required_ram = max(
#                 required_ram,
#                 env.get('checker_mem_limit', DEFAULT_RAM_REQUIREMENTS['checker']),
#             )
#     else:
#         required_ram = env.get(
#             job_type + '_mem_limit',
#             DEFAULT_RAM_REQUIREMENTS.get(job_type, DEFAULT_RAM_REQUIREMENTS['default']),
#         )
# 
#     # Convert KiB to MiB
#     return required_ram / 1024
# 
# Path: sio/sioworkersd/workermanager.py
# class WorkerGone(Exception):
#     """Worker disconnected while executing task."""
# 
#     pass
# 
# Path: sio/workers/util.py
# def json_dumps(obj, **kwargs):
#     """Python 3 and 2 compatible json.dump."""
#     kwargs.setdefault('cls', CompatibleJSONEncoder)
#     return json.dumps(obj, **kwargs)


import traceback
import json
import six
import time
    import bsddb
    import bsddb3 as bsddb
from twisted.application.service import Service
from twisted.internet import defer, reactor
from twisted.internet.task import deferLater, LoopingCall
from twisted.python.failure import Failure
from twisted.web import client
from twisted.web.http_headers import Headers
from collections import namedtuple
from six import BytesIO
from six.moves import range
from operator import itemgetter
from sio.protocol.rpc import RemoteError
from sio.sioworkersd.utils import get_required_ram_for_job
from sio.sioworkersd.workermanager import WorkerGone
from sio.workers.util import json_dumps
from twisted.logger import Logger, LogLevel
from urllib3 import encode_multipart_formdata

        # There is no need to save synchronous task. In case of server
        # failure client is disconnected, so it can't receive the result
        # anyway.
        save = 'return_url' in group_env
        if save:
            self.database.update(
                group_env['group_id'],
                {
                    'id': group_env['group_id'],
                    'env': group_env,
                    'status': 'to_judge',
                    'timestamp': time.time(),
                    'retry_cnt': 0,
                },
            )
        ret = yield self._addGroup(group_env)
        defer.returnValue(ret)

    def returnToSio(self, x, url, orig_env=None, tid=None, count=0):
        if isinstance(x, Failure):
            assert orig_env
            env = orig_env
            log.failure('Returning with error', x, LogLevel.warn)
        else:
            env = x

        if not tid:
            tid = env['group_id']


# Path: sio/protocol/rpc.py
# class RemoteError(Exception):
#     def __init__(self, err, tb=None, uid=None):
#         if uid is not None:
#             err = "received from " + uid + ("" if err is None else ": " + err)
#         super(RemoteError, self).__init__(err)
#         self.traceback = tb
#         self.uid = uid
# 
# Path: sio/sioworkersd/utils.py
# def get_required_ram_for_job(env):
#     job_type = env['job_type']
#     if job_type.endswith('exec'):
#         required_ram = env.get('exec_mem_limit', DEFAULT_RAM_REQUIREMENTS['exec'])
#         # We need to make sure that we have enough ram for a checker as well.
#         if env.get('check_output'):
#             required_ram = max(
#                 required_ram,
#                 env.get('checker_mem_limit', DEFAULT_RAM_REQUIREMENTS['checker']),
#             )
#     else:
#         required_ram = env.get(
#             job_type + '_mem_limit',
#             DEFAULT_RAM_REQUIREMENTS.get(job_type, DEFAULT_RAM_REQUIREMENTS['default']),
#         )
# 
#     # Convert KiB to MiB
#     return required_ram / 1024
# 
# Path: sio/sioworkersd/workermanager.py
# class WorkerGone(Exception):
#     """Worker disconnected while executing task."""
# 
#     pass
# 
# Path: sio/workers/util.py
# def json_dumps(obj, **kwargs):
#     """Python 3 and 2 compatible json.dump."""
#     kwargs.setdefault('cls', CompatibleJSONEncoder)
#     return json.dumps(obj, **kwargs)


# Path: sio/sioworkersd/taskmanager.py
import traceback
import json
import six
import time
    import bsddb
    import bsddb3 as bsddb
from twisted.application.service import Service
from twisted.internet import defer, reactor
from twisted.internet.task import deferLater, LoopingCall
from twisted.python.failure import Failure
from twisted.web import client
from twisted.web.http_headers import Headers
from collections import namedtuple
from six import BytesIO
from six.moves import range
from operator import itemgetter
from sio.protocol.rpc import RemoteError
from sio.sioworkersd.utils import get_required_ram_for_job
from sio.sioworkersd.workermanager import WorkerGone
from sio.workers.util import json_dumps
from twisted.logger import Logger, LogLevel
from urllib3 import encode_multipart_formdata


        # There is no need to save synchronous task. In case of server
        # failure client is disconnected, so it can't receive the result
        # anyway.
        save = 'return_url' in group_env
        if save:
            self.database.update(
                group_env['group_id'],
                {
                    'id': group_env['group_id'],
                    'env': group_env,
                    'status': 'to_judge',
                    'timestamp': time.time(),
                    'retry_cnt': 0,
                },
            )
        ret = yield self._addGroup(group_env)
        defer.returnValue(ret)

    def returnToSio(self, x, url, orig_env=None, tid=None, count=0):
        if isinstance(x, Failure):
            assert orig_env
            env = orig_env
            log.failure('Returning with error', x, LogLevel.warn)
        else:
            env = x

        if not tid:
            tid = env['group_id']


        body, content_type = encode_multipart_formdata({'data': json_dumps(env)})