Skip to content

Commit

Permalink
Fix: Connection handling, ranking order, SQL errors (#100)
Browse files Browse the repository at this point in the history
* fix: release all connection resources when server shutdown

* fix: release redis connection when websocket closed

* fix: problem rank should be ordered by score in descending order

* fix: optimize inner join by including matching column
The inner join should include the matching column; otherwise, the database will choose a sequential scan instead of an index scan.

* fix(contests): sql syntax error

* fix(pro): cannot load PDFJS

* fix: escape markdown code content failed
  • Loading branch information
tobiichi3227 authored Dec 15, 2024
1 parent c10e4bc commit ad2f985
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 16 deletions.
11 changes: 9 additions & 2 deletions src/handlers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,16 @@ def __init__(self, *args, **kwargs):
def check_origin(self, _: str) -> bool:
return True

async def cleanup(self):
await self.p.unsubscribe()
if self.task:
self.task.cancel()

await self.p.aclose()
await self.rs.aclose()

def on_close(self) -> None:
self.task.cancel()
asyncio.create_task(self.rs.aclose())
asyncio.create_task(self.cleanup())


def reqenv(func):
Expand Down
13 changes: 7 additions & 6 deletions src/handlers/rank.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,25 +37,26 @@ async def get(self, pro_id):
"account"."name" AS "acct_name",
"challenge_state"."runtime",
"challenge_state"."memory",
ROUND("challenge_state"."rate", "problem"."rate_precision")
ROUND("challenge_state"."rate", "problem"."rate_precision") AS rate
FROM "challenge"
INNER JOIN "problem"
ON "challenge"."pro_id" = "problem"."pro_id" AND "problem"."pro_id" = $1
INNER JOIN "account"
ON "challenge"."acct_id"="account"."acct_id"
INNER JOIN "challenge_state"
ON "challenge"."chal_id"="challenge_state"."chal_id"
INNER JOIN "problem"
ON "challenge"."pro_id" = $1
WHERE "challenge_state"."state"={ChalConst.STATE_AC}
ORDER BY "challenge"."acct_id" ASC, "challenge_state"."rate" ASC,
ORDER BY "challenge"."acct_id" ASC, "challenge_state"."rate" DESC,
"challenge_state"."runtime" ASC, "challenge_state"."memory" ASC,
"challenge"."timestamp" ASC
) temp
ORDER BY "runtime" ASC, "memory" ASC,
ORDER BY "rate" DESC, "runtime" ASC, "memory" ASC,
"timestamp" ASC, "acct_id" ASC OFFSET $2 LIMIT $3;
'''
,
Expand Down
10 changes: 5 additions & 5 deletions src/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ def stop_loop(deadline):
for task in asyncio.all_tasks():
task.cancel()

io_loop.add_callback(db.close)
io_loop.add_callback(rs.aclose)
io_loop.add_callback(pool.aclose)
io_loop.add_callback(JudgeServerClusterService.inst.disconnect_all_server)
io_loop.stop()
asyncio.create_task(db.close())
asyncio.create_task(rs.aclose())
asyncio.create_task(pool.aclose())
asyncio.create_task(JudgeServerClusterService.inst.disconnect_all_server())
io_loop.add_callback(io_loop.stop)

print('Shutdown finally')

Expand Down
2 changes: 1 addition & 1 deletion src/services/contests.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ async def get_ioi2013_scores(self, contest_id: int, pro_id: int, before_time: da
"challenge"."pro_id",
"challenge"."acct_id",
"challenge"."timestamp",
ROUND("challenge_state"."rate", problem.rate_precision),
ROUND("challenge_state"."rate", problem.rate_precision) AS rate,
ROW_NUMBER() OVER (
PARTITION BY "challenge"."pro_id", "challenge"."acct_id"
Expand Down
2 changes: 1 addition & 1 deletion src/static/templ/pro.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

function init() {
let PDFJS = pdfjsLib;
PDFJS.workerSrc = 'https://cdnjs.cloudflare.com/ajax/libs/pdf.js/4.7.76/pdf.worker.min.mjs';
PDFJS.GlobalWorkerOptions.workerSrc = 'https://cdnjs.cloudflare.com/ajax/libs/pdf.js/4.7.76/pdf.worker.min.mjs';

$('#tags').keydown(function(event) {
if (event.which == 13) // return
Expand Down
2 changes: 1 addition & 1 deletion src/utils/htmlgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ def set_page_title(title: str, site_title: str = None):
"""

def markdown_escape(code: str) -> str:
return code.replace('`', '\\`').replace('\\', '\\\\')
return code.replace('\\', '\\\\').replace('`', '\\`')

0 comments on commit ad2f985

Please sign in to comment.