Example #1
0
def student_submit_solution(data):
    print(data)
    user = User(session['user'])

    try:
        type = str(data['type'])
        action = ProcessRequestType(type)
        skip_docker = not data.get('docker', True)
    except:
        Emittor.error('Unsupported action', [
            'Given action is not supported:',
            '    %s' % data['type'], '', 'Please contact [email protected]',
            'if you think this is a mistake.'
        ])
        return

    if not user.is_admin() and (skip_docker or action
                                in (ProcessRequestType.GENERATE_INPUT,
                                    ProcessRequestType.GENERATE_OUTPUT)):
        Emittor.error('Operation not permitted', [
            'You do not have sufficient privileges to perform action:',
            '    %s (skip docker: %s)' %
            (action, skip_docker), '', 'Please contact [email protected]',
            'if you want to gain the privileges.'
        ])
        return

    request = processing.request.ProcessRequest(
        user=user,
        lang=data['lang'],
        problem=data['prob'],
        course=data['course'],
        solution=data['src'],
        type=action,
        docker=False if (skip_docker and user.is_admin()) else True,
    )

    mongo.save_log(request.get_log_dict())

    # ignore problems which are past due
    if request.problem.time_left < 0:
        return

    Emittor.register_events(request)
    Emittor.queue_status(queue_status())

    time.sleep(0.1)
    queue.append(request)
    Emittor.queue_push(request)

    time.sleep(0.1)

    # put a barrier here so only certain amount fo users can process code at once
    # while other will see queue list
    with thread_lock:
        try:
            request.process()
        except ConfigurationException as e:
            if user.is_admin():
                logger.exception('[visible to admin only] invalid yaml config')
                Emittor.exception(e)
        except Exception as e:
            logger.exception('process error:')
            Emittor.exception(e)
        finally:
            mongo.save_result(request.get_result_dict())
            request.save_result()
            request.destroy()

    queue.remove(request)
    Emittor.queue_pop(request)
Example #2
0
def _process_solution(user,
                      action,
                      skip_docker,
                      problem_id,
                      course_id,
                      lang_id=None,
                      src=None,
                      _id=None):
    if not user.is_admin() and (skip_docker or action
                                in (ProcessRequestType.GENERATE_INPUT,
                                    ProcessRequestType.GENERATE_OUTPUT)):
        Emittor.error('Operation not permitted', [
            'You do not have sufficient privileges to perform action:',
            '    %s (skip docker: %s)' %
            (action, skip_docker), '', 'Please contact [email protected]',
            'if you want to gain the privileges.'
        ])
        return

    request = processing.request.ProcessRequest(
        user=user,
        lang=lang_id,
        problem=problem_id,
        course=course_id,
        src=src,
        type=action,
        docker=False if (skip_docker and user.is_admin()) else True,
    )

    if Env.use_database:
        Mongo().save_log(request.get_log_dict())

    # ignore problems which are past due
    if request.problem.time_left < 0:
        return

    Emittor.register_events(request)
    Emittor.queue_status(queue_status())
    queue.append(request)
    Emittor.queue_push(request)

    # put a barrier here so only certain amount fo users can process code at once
    # while other will see queue list
    with thread_lock:
        try:
            request.process()
        except ConfigurationException as e:
            if user.is_admin():
                logger.exception('[visible to admin only] invalid yaml config')
                Emittor.exception(e)
        except Exception as e:
            logger.exception('process error:')
            Emittor.exception(e)
        finally:
            output_dir, attempt = request.save_result()
            if Env.use_database:
                # replace document instead of creating new one
                Mongo().save_result(
                    request.get_result_dict(),
                    _id=_id,
                    output_dir=output_dir,
                    attempt=attempt,
                )
            request.destroy()

    queue.remove(request)
    Emittor.queue_pop(request)