Skip to content

Commit 29de34c

Browse files
committed
Fix submission timing
1 parent 57ae9c0 commit 29de34c

2 files changed

Lines changed: 35 additions & 37 deletions

File tree

src/apps/competitions/tasks.py

Lines changed: 32 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from django.core.exceptions import ObjectDoesNotExist
1616
from django.core.files.base import ContentFile
1717
from django.db.models import Subquery, OuterRef, Count, Case, When, Value, F
18+
from django.db import transaction
1819
from django.utils.text import slugify
1920
from django.utils.timezone import now
2021
from rest_framework.exceptions import ValidationError
@@ -141,14 +142,6 @@ def _send_to_compute_worker(submission, is_scoring):
141142
submission = Submission.objects.get(id=submission.id)
142143
task = submission.task
143144

144-
# priority of scoring tasks is higher, we don't want to wait around for
145-
# many submissions to be scored while we're waiting for results
146-
if is_scoring:
147-
# higher numbers are higher priority
148-
priority = 10
149-
else:
150-
priority = 0
151-
152145
if not is_scoring:
153146
run_args['prediction_result'] = make_url_sassy(
154147
path=submission.prediction_result.name,
@@ -201,40 +194,45 @@ def _send_to_compute_worker(submission, is_scoring):
201194
time_padding = 60 * 20 # 20 minutes
202195
time_limit = submission.phase.execution_time_limit + time_padding
203196

204-
if submission.phase.competition.queue: # if the competition is running on a custom queue, not the default queue
205-
submission.queue_name = submission.phase.competition.queue.name or ''
206-
run_args['execution_time_limit'] = submission.phase.execution_time_limit # use the competition time limit
207-
submission.save()
208-
209-
# Send to special queue? Using `celery_app` var name here since we'd be overriding the imported `app`
210-
# variable above
211-
celery_app = app_or_default()
212-
with celery_app.connection() as new_connection:
213-
new_connection.virtual_host = str(submission.phase.competition.queue.vhost)
214-
task = celery_app.send_task(
197+
def _enqueue_after_commit():
198+
# priority of scoring tasks is higher, we don't want to wait around for
199+
# many submissions to be scored while we're waiting for results
200+
priority = 10 if is_scoring else 0
201+
202+
if submission.phase.competition.queue: # if the competition is running on a custom queue, not the default queue
203+
submission.queue_name = submission.phase.competition.queue.name or ''
204+
run_args['execution_time_limit'] = submission.phase.execution_time_limit # use the competition time limit
205+
submission.save(update_fields=["queue_name"])
206+
celery_app = app_or_default()
207+
with celery_app.connection() as new_connection:
208+
new_connection.virtual_host = str(submission.phase.competition.queue.vhost)
209+
task = celery_app.send_task(
210+
'compute_worker_run',
211+
args=(run_args,),
212+
queue='compute-worker',
213+
soft_time_limit=time_limit,
214+
connection=new_connection,
215+
priority=priority,
216+
)
217+
else:
218+
task = app.send_task(
215219
'compute_worker_run',
216220
args=(run_args,),
217221
queue='compute-worker',
218222
soft_time_limit=time_limit,
219-
connection=new_connection,
220223
priority=priority,
221224
)
222-
else:
223-
task = app.send_task(
224-
'compute_worker_run',
225-
args=(run_args,),
226-
queue='compute-worker',
227-
soft_time_limit=time_limit,
228-
priority=priority,
229-
)
230-
submission.celery_task_id = task.id
231225

232-
if submission.status == Submission.SUBMITTING:
233-
# Don't want to mark an already-prepared submission as "submitted" again, so
234-
# only do this if we were previously "SUBMITTING"
235-
submission.status = Submission.SUBMITTED
226+
submission.celery_task_id = task.id
227+
228+
if submission.status == Submission.SUBMITTING:
229+
# Don't want to mark an already-prepared submission as "submitted" again, so
230+
# only do this if we were previously "SUBMITTING"
231+
submission.status = Submission.SUBMITTED
232+
233+
submission.save(update_fields=["celery_task_id", "status"])
236234

237-
submission.save()
235+
transaction.on_commit(_enqueue_after_commit)
238236

239237

240238
def create_detailed_output_file(detail_name, submission):

tests/test_submission.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def run_tests(page, competition, submission) -> None:
4343
expect(page.get_by_role("cell", name="Finished")).to_be_visible(timeout=25000)
4444
except:
4545
page.reload()
46-
expect(page.get_by_role("cell", name="Finished")).to_be_visible(timeout=3000)
46+
expect(page.get_by_role("cell", name="Finished")).to_be_visible(timeout=2000)
4747
# Add to leaderboard and see if shows
4848
text = page.locator(".submission_row").first.inner_text()
4949
submission_Id = text.split(None, 1)
@@ -123,7 +123,7 @@ def test_v2_multiTask(page: Page) -> None:
123123
expect(page.get_by_role("cell", name="Finished")).to_be_visible(timeout=35000)
124124
except:
125125
page.reload()
126-
expect(page.get_by_role("cell", name="Finished")).to_be_visible(timeout=3000)
126+
expect(page.get_by_role("cell", name="Finished")).to_be_visible(timeout=2000)
127127
# Add to leaderboard and see if shows
128128
text = page.locator(".submission_row").first.inner_text()
129129
submission_Id = text.split(None, 1)
@@ -183,7 +183,7 @@ def test_v2_multiTaskFactSheet(page: Page) -> None:
183183
expect(page.get_by_role("cell", name="Finished")).to_be_visible(timeout=35000)
184184
except:
185185
page.reload()
186-
expect(page.get_by_role("cell", name="Finished")).to_be_visible(timeout=3000)
186+
expect(page.get_by_role("cell", name="Finished")).to_be_visible(timeout=2000)
187187
# Add to leaderboard and see if shows
188188
text = page.locator(".submission_row").first.inner_text()
189189
submission_Id = text.split(None, 1)

0 commit comments

Comments
 (0)