Exemple #1
0
async def system_test(rdoc, judge_category):
    coll = db.coll('record')
    _logger.info('Create system test in {0}, pid {1}, uid {2}'.format(
        rdoc['domain_id'], rdoc['pid'], rdoc['uid']))
    doc = {
        'hidden': rdoc['hidden'],
        'show_detail': 'show_detail' in rdoc and rdoc['show_detail'] or False,
        'status': constant.record.STATUS_WAITING,
        'score': 0,
        'time_ms': 0,
        'memory_kb': 0,
        'domain_id': rdoc['domain_id'],
        'pid': rdoc['pid'],
        'uid': rdoc['uid'],
        'lang': rdoc['lang'],
        'code_type': rdoc['code_type'],
        'code': rdoc['code'],
        'tid': rdoc['tid'],
        'data_id': rdoc['data_id'],
        'type': rdoc['type'],
        'judge_category': judge_category,
        'submit_time': rdoc['_id'].generation_time
    }
    rid = (await coll.insert_one(doc)).inserted_id
    bus.publish_throttle('record_change', doc, rid)
    await queue.publish('judge', rid=rid)
    return rid
Exemple #2
0
async def rejudge(record_id: objectid.ObjectId, enqueue: bool = True):
    coll = db.coll('record')
    doc = await coll.find_one_and_update(
        filter={'_id': record_id},
        update={
            '$unset': {
                'judge_uid': '',
                'judge_token': '',
                'judge_at': '',
                'compiler_texts': '',
                'judge_texts': '',
                'cases': ''
            },
            '$set': {
                'status': constant.record.STATUS_WAITING,
                'score': 0,
                'time_ms': 0,
                'memory_kb': 0,
                'rejudged': True
            }
        },
        return_document=ReturnDocument.AFTER)
    bus.publish_throttle('record_change', doc, doc['_id'])
    if enqueue:
        await queue.publish('judge', rid=doc['_id'])
Exemple #3
0
async def _post_judge(handler, rdoc):
    accept = rdoc['status'] == constant.record.STATUS_ACCEPTED
    bus.publish_throttle('record_change', rdoc, rdoc['_id'])
    post_coros = list()
    # TODO(twd2): ignore no effect statuses like system error, ...
    if rdoc['type'] == constant.record.TYPE_SUBMISSION:
        if accept:
            post_coros.append(_send_ac_mail(handler, rdoc))
        if rdoc['tid']:
            post_coros.append(
                contest.update_status(rdoc['domain_id'], rdoc['tid'],
                                      rdoc['uid'], rdoc['_id'], rdoc['pid'],
                                      accept, rdoc['score']))
        if not rdoc.get('rejudged'):
            if await problem.update_status(rdoc['domain_id'], rdoc['pid'],
                                           rdoc['uid'], rdoc['_id'],
                                           rdoc['status']):
                if accept:
                    # TODO(twd2): enqueue rdoc['pid'] to recalculate rp.
                    await problem.inc(rdoc['domain_id'], rdoc['pid'],
                                      'num_accept', 1)
                    post_coros.append(
                        domain.inc_user(rdoc['domain_id'],
                                        rdoc['uid'],
                                        num_accept=1))
        else:
            # TODO(twd2): enqueue rdoc['pid'] to recalculate rp.
            await job.record.user_in_problem(rdoc['uid'], rdoc['domain_id'],
                                             rdoc['pid'])
        post_coros.append(
            job.difficulty.update_problem(rdoc['domain_id'], rdoc['pid']))
    await asyncio.gather(*post_coros)
Exemple #4
0
 async def _on_queue_message(self, tag, *, rid):
     # TODO(iceboy): Error handling?
     rdoc = await record.begin_judge(rid, self.user['_id'], self.id,
                                     constant.record.STATUS_FETCHED)
     if rdoc:
         used_time = await opcount.get(**opcount.OPS['run_code'],
                                       ident=opcount.PREFIX_USER +
                                       str(rdoc['uid']))
         if used_time >= opcount.OPS['run_code']['max_operations']:
             rdoc, _ = await asyncio.gather(
                 record.end_judge(rid, self.user['_id'], self.id,
                                  constant.record.STATUS_CANCELED, 0, 0, 0),
                 self.channel.basic_client_ack(tag))
             bus.publish_throttle('record_change', rdoc, rdoc['_id'])
             return
         self.rids[tag] = rdoc['_id']
         self.send(rid=str(rdoc['_id']),
                   tag=tag,
                   pid=str(rdoc['pid']),
                   domain_id=rdoc['domain_id'],
                   lang=rdoc['lang'],
                   code=rdoc['code'],
                   type=rdoc['type'])
         bus.publish_throttle('record_change', rdoc, rdoc['_id'])
     else:
         # Record not found, eat it.
         await self.channel.basic_client_ack(tag)
Exemple #5
0
async def _post_judge(handler, rdoc):
  accept = rdoc['status'] == constant.record.STATUS_ACCEPTED
  bus.publish_throttle('record_change', rdoc, rdoc['_id'])
  post_coros = list()
  # TODO(twd2): ignore no effect statuses like system error, ...
  if rdoc['type'] == constant.record.TYPE_SUBMISSION:
    if accept:
      post_coros.append(_send_ac_mail(handler, rdoc))
    if rdoc['tid']:
      post_coros.append(contest.update_status(rdoc['domain_id'],
                                              rdoc.get('ttype', document.TYPE_CONTEST), rdoc['tid'],
                                              rdoc['uid'], rdoc['_id'], rdoc['pid'],
                                              accept, rdoc['score']))
    if not rdoc.get('rejudged'):
      if await problem.update_status(rdoc['domain_id'], rdoc['pid'], rdoc['uid'],
                                     rdoc['_id'], rdoc['status']):
        if accept:
          # TODO(twd2): enqueue rdoc['pid'] to recalculate rp.
          await problem.inc(rdoc['domain_id'], rdoc['pid'], 'num_accept', 1)
          post_coros.append(domain.inc_user(rdoc['domain_id'], rdoc['uid'], num_accept=1))
    else:
      # TODO(twd2): enqueue rdoc['pid'] to recalculate rp.
      await job.record.user_in_problem(rdoc['uid'], rdoc['domain_id'], rdoc['pid'])
    post_coros.append(job.difficulty.update_problem(rdoc['domain_id'], rdoc['pid']))
  await asyncio.gather(*post_coros)
Exemple #6
0
 async def on_message(self, *, key, tag, **kwargs):
   if key == 'next':
     rid = self.rids[tag]
     update = {}
     if 'status' in kwargs:
       update.setdefault('$set', {})['status'] = int(kwargs['status'])
     if 'compiler_text' in kwargs:
       update.setdefault('$push', {})['compiler_texts'] = str(kwargs['compiler_text'])
     if 'judge_text' in kwargs:
       update.setdefault('$push', {})['judge_texts'] = str(kwargs['judge_text'])
     if 'case' in kwargs:
       update.setdefault('$push', {})['cases'] = {
         'status': int(kwargs['case']['status']),
         'score': int(kwargs['case']['score']),
         'time_ms': int(kwargs['case']['time_ms']),
         'memory_kb': int(kwargs['case']['memory_kb']),
         'judge_text': str(kwargs['case']['judge_text']),
       }
     if 'progress' in kwargs:
       update.setdefault('$set', {})['progress'] = float(kwargs['progress'])
     rdoc = await record.next_judge(rid, self.user['_id'], self.id, **update)
     if not rdoc:
       return
     bus.publish_throttle('record_change', rdoc, rdoc['_id'])
   elif key == 'end':
     rid = self.rids.pop(tag)
     rdoc, _ = await asyncio.gather(record.end_judge(rid, self.user['_id'], self.id,
                                                     int(kwargs['status']),
                                                     int(kwargs['score']),
                                                     int(kwargs['time_ms']),
                                                     int(kwargs['memory_kb'])),
                                    self.channel.basic_client_ack(tag))
     if not rdoc:
       return
     await _post_judge(self, rdoc)
Exemple #7
0
 async def on_message(self, *, key, tag, **kwargs):
   if key == 'next':
     rid = self.rids[tag]
     update = {}
     if 'status' in kwargs:
       update.setdefault('$set', {})['status'] = int(kwargs['status'])
     if 'compiler_text' in kwargs:
       update.setdefault('$push', {})['compiler_texts'] = str(kwargs['compiler_text'])
     if 'judge_text' in kwargs:
       update.setdefault('$push', {})['judge_texts'] = str(kwargs['judge_text'])
     if 'case' in kwargs:
       update.setdefault('$push', {})['cases'] = {
         'status': int(kwargs['case']['status']),
         'score': int(kwargs['case']['score']),
         'time_ms': int(kwargs['case']['time_ms']),
         'memory_kb': int(kwargs['case']['memory_kb']),
         'judge_text': str(kwargs['case']['judge_text']),
       }
     if 'progress' in kwargs:
       update.setdefault('$set', {})['progress'] = float(kwargs['progress'])
     rdoc = await record.next_judge(rid, self.user['_id'], self.id, **update)
     if not rdoc:
       return
     bus.publish_throttle('record_change', rdoc, rdoc['_id'])
   elif key == 'end':
     rid = self.rids.pop(tag)
     rdoc, _ = await asyncio.gather(record.end_judge(rid, self.user['_id'], self.id,
                                                     int(kwargs['status']),
                                                     int(kwargs['score']),
                                                     int(kwargs['time_ms']),
                                                     int(kwargs['memory_kb'])),
                                    self.channel.basic_client_ack(tag))
     if not rdoc:
       return
     await _post_judge(self, rdoc)
Exemple #8
0
 async def _on_queue_message(self, tag, *, rid):
   rdoc = await record.begin_judge(rid, self.user['_id'], self.id,
                                   constant.record.STATUS_FETCHED)
   if rdoc:
     self.rids[tag] = rdoc['_id']
     self.send(rid=str(rdoc['_id']), tag=tag, pid=str(rdoc['pid']), domain_id=rdoc['domain_id'],
               lang=rdoc['lang'], code=rdoc['code'], type=rdoc['type'])
     bus.publish_throttle('record_change', rdoc, rdoc['_id'])
   else:
     # Record not found, eat it.
     await self.channel.basic_client_ack(tag)
Exemple #9
0
 async def _on_queue_message(self, tag, *, rid):
     rdoc = await record.begin_judge(rid, self.user['_id'], self.id,
                                     constant.record.STATUS_FETCHED)
     if rdoc:
         self.rids[tag] = rdoc['_id']
         self.send(rid=str(rdoc['_id']),
                   tag=tag,
                   pid=str(rdoc['pid']),
                   domain_id=rdoc['domain_id'],
                   lang=rdoc['lang'],
                   code=rdoc['code'],
                   type=rdoc['type'])
         bus.publish_throttle('record_change', rdoc, rdoc['_id'])
     else:
         # Record not found, eat it.
         await self.channel.basic_client_ack(tag)
Exemple #10
0
async def add(domain_id: str,
              pid: document.convert_doc_id,
              type: int,
              uid: int,
              lang: str,
              code: Union[str, objectid.ObjectId],
              data_id: objectid.ObjectId = None,
              tid: objectid.ObjectId = None,
              hidden=False,
              show_detail=False,
              code_type=constant.record.FILE_TYPE_TEXT,
              judge_category=[]):
    validator.check_lang(lang)
    coll = db.coll('record')
    doc = {
        'hidden': hidden,
        'show_detail': show_detail,
        'status': constant.record.STATUS_WAITING,
        'score': 0,
        'time_ms': 0,
        'memory_kb': 0,
        'domain_id': domain_id,
        'pid': pid,
        'uid': uid,
        'lang': lang,
        'code_type': code_type,
        'code': code,
        'tid': tid,
        'data_id': data_id,
        'type': type,
        'judge_category': judge_category
    }
    rid = (await coll.insert_one(doc)).inserted_id
    bus.publish_throttle('record_change', doc, rid)
    post_coros = [queue.publish('judge', rid=rid)]
    if type == constant.record.TYPE_SUBMISSION:
        post_coros.extend([
            problem.inc_status(domain_id, pid, uid, 'num_submit', 1),
            problem.inc(domain_id, pid, 'num_submit', 1),
            domain.inc_user(domain_id, uid, num_submit=1)
        ])
    await asyncio.gather(*post_coros)
    return rid
Exemple #11
0
async def system_test(rdoc, judge_category):
    coll = db.coll('record')
    doc = {
        'hidden': rdoc['hidden'],
        'status': constant.record.STATUS_WAITING,
        'score': 0,
        'time_ms': 0,
        'memory_kb': 0,
        'domain_id': rdoc['domain_id'],
        'pid': rdoc['pid'],
        'uid': rdoc['uid'],
        'lang': rdoc['lang'],
        'code_type': rdoc['code_type'],
        'code': rdoc['code'],
        'tid': rdoc['tid'],
        'data_id': rdoc['data_id'],
        'type': rdoc['type'],
        'judge_category': judge_category,
        'submit_time': rdoc['_id'].generation_time
    }
    rid = (await coll.insert_one(doc)).inserted_id
    bus.publish_throttle('record_change', doc, rid)
    await queue.publish('judge', rid=rid)
    return rid
Exemple #12
0
 async def reset_record(rid):
     rdoc = await record.end_judge(rid, self.user['_id'], self.id,
                                   constant.record.STATUS_WAITING,
                                   0, 0, 0)
     bus.publish_throttle('record_change', rdoc, rdoc['_id'])
Exemple #13
0
 async def reset_record(rid):
   rdoc = await record.end_judge(rid, self.user['_id'], self.id,
                                 constant.record.STATUS_WAITING, 0, 0, 0)
   bus.publish_throttle('record_change', rdoc, rdoc['_id'])