Skip to content

Commit

Permalink
change: Optimize code and fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
meua committed Jan 30, 2024
1 parent 256df6e commit e12cee4
Show file tree
Hide file tree
Showing 21 changed files with 301 additions and 58 deletions.
18 changes: 18 additions & 0 deletions apps/accounts/migrations/0003_auto_20240109_1048.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 2.2.20 on 2024-01-09 02:48

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('accounts', '0002_add_jwt_access_token_model'),
]

operations = [
migrations.AlterField(
model_name='profile',
name='avatar',
field=models.URLField(blank=True, max_length=100, null=True),
),
]
10 changes: 5 additions & 5 deletions apps/base/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,10 @@ def send_email(


def get_url_from_hostname(hostname):
# if settings.DEBUG or settings.TEST:
scheme = "http" # TODO After https is supported, this needs to be restored to its original state.
# else:
# scheme = "https"
if settings.DEBUG or settings.TEST:
scheme = "http"
else:
scheme = "https"
url = "{}://{}".format(scheme, hostname)
return url

Expand Down Expand Up @@ -180,7 +180,7 @@ def get_boto3_client(resource, aws_keys):

def get_or_create_sqs_queue(queue_name, challenge=None):
if settings.DEBUG or settings.TEST:
queue_name = "evalai_submission_queue"
queue_name = "arena_submission_queue"
sqs = boto3.resource(
"sqs",
endpoint_url=os.environ.get("AWS_SQS_ENDPOINT", "http://sqs:9324"),
Expand Down
41 changes: 41 additions & 0 deletions apps/challenges/aws_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -771,6 +771,34 @@ def create_ec2_instance(challenge, ec2_storage=None, worker_instance_type=None,
}


def update_sqs_retention_period(challenge):
"""
Update the SQS retention period for a challenge.
Args:
challenge (Challenge): The challenge for which the SQS retention period is to be updated.
Returns:
dict: A dictionary containing the status and message of the operation.
"""
sqs_retention_period = str(challenge.sqs_retention_period)
try:
sqs = get_boto3_client("sqs", aws_keys)
queue_url = sqs.get_queue_url(QueueName=challenge.queue)['QueueUrl']
response = sqs.set_queue_attributes(
QueueUrl=queue_url,
Attributes={
'MessageRetentionPeriod': sqs_retention_period
}
)
return {"message": response}
except Exception as e:
logger.exception(e)
return {
"error": str(e),
}


def start_workers(queryset):
"""
The function called by the admin action method to start all the selected workers.
Expand Down Expand Up @@ -1794,3 +1822,16 @@ def setup_ec2(challenge):
if challenge_obj.ec2_instance_id:
return start_ec2_instance(challenge_obj)
return create_ec2_instance(challenge_obj)


@app.task
def update_sqs_retention_period_task(challenge):
"""
Updates sqs retention period for a challenge when the attribute is changed.
Arguments:
challenge {<class 'django.db.models.query.QuerySet'>} -- instance of the model calling the post hook
"""
for obj in serializers.deserialize("json", challenge):
challenge_obj = obj.object
return update_sqs_retention_period(challenge_obj)
2 changes: 2 additions & 0 deletions apps/challenges/challenge_config_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,8 @@ def get_value_from_field(data, base_location, field_name):
"duplicate_rank": "ERROR: Duplicate rank {} found in YAML data.",
"prize_amount_wrong": "ERROR: Invalid amount value {}. Amount should be in decimal format with three-letter currency code (e.g. 100.00USD, 500EUR, 1000INR).",
"prize_rank_wrong": "ERROR: Invalid rank value {}. Rank should be an integer.",
"challenge_metadata_schema_errors": "ERROR: Unable to serialize the challenge because of the following errors: {}.",
"evaluation_script_not_zip": "ERROR: Please pass in a zip file as evaluation script. If using the `evaluation_script` directory (recommended), it should be `evaluation_script.zip`.",
}


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 2.2.20 on 2023-12-01 15:46

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('challenges', '0110_challenge_ephemeral_storage'),
]

operations = [
migrations.AlterField(
model_name='challenge',
name='ephemeral_storage',
field=models.PositiveIntegerField(default=21, verbose_name='Ephemeral Storage (GB)'),
),
]
18 changes: 18 additions & 0 deletions apps/challenges/migrations/0112_challenge_sqs_retention_period.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 2.2.20 on 2023-12-10 15:16

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('challenges', '0111_alter_challenge_ephemeral_storage_default'),
]

operations = [
migrations.AddField(
model_name='challenge',
name='sqs_retention_period',
field=models.PositiveIntegerField(default=345600, verbose_name='SQS Retention Period'),
),
]
5 changes: 5 additions & 0 deletions apps/challenges/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def __init__(self, *args, **kwargs):
super(Challenge, self).__init__(*args, **kwargs)
self._original_evaluation_script = self.evaluation_script
self._original_approved_by_admin = self.approved_by_admin
self._original_sqs_retention_period = self.sqs_retention_period

title = models.CharField(max_length=100, db_index=True, unique=True)
short_description = models.TextField(null=True, blank=True)
Expand Down Expand Up @@ -128,6 +129,10 @@ def __init__(self, *args, **kwargs):
null=False,
blank=False
)
sqs_retention_period = models.PositiveIntegerField(
default=345600,
verbose_name="SQS Retention Period"
)
is_docker_based = models.BooleanField(
default=False, verbose_name="Is Docker Based", db_index=True
)
Expand Down
11 changes: 9 additions & 2 deletions apps/challenges/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ class Meta:
"ephemeral_storage",
"evaluation_module_error",
"worker_image_url",
"worker_instance_type"
"worker_instance_type",
"sqs_retention_period"
)


Expand Down Expand Up @@ -276,6 +277,7 @@ class Meta:
"enable_forum",
"anonymous_leaderboard",
"leaderboard_description",
"manual_participant_approval",
"image",
"is_active",
"evaluation_script",
Expand All @@ -291,6 +293,10 @@ class Meta:
"is_docker_based",
"queue",
"queue_aws_region",
"aws_account_id",
"aws_access_key_id",
"aws_secret_access_key",
"aws_region",
"is_static_dataset_code_upload",
"slug",
"max_docker_image_size",
Expand All @@ -312,7 +318,8 @@ class Meta:
"ec2_storage",
"ephemeral_storage",
"evaluation_module_error",
"worker_image_url"
"worker_image_url",
"sqs_retention_period"
)


Expand Down
5 changes: 5 additions & 0 deletions apps/challenges/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,11 @@
views.update_challenge_tags_and_domain,
name="update_challenge_tags_and_domain",
),
url(
r"^challenge/update_challenge_attributes/$",
views.update_challenge_attributes,
name="update_challenge_attributes",
),
url(
r"^challenge/get_domain_choices/$",
views.get_domain_choices,
Expand Down
8 changes: 4 additions & 4 deletions apps/challenges/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
from botocore.exceptions import ClientError
from django.conf import settings
from django.core.files.base import ContentFile
# from moto import mock_ecr, mock_sts
from moto import mock_ecr, mock_sts

from base.utils import (
get_model_object,
get_boto3_client,
# mock_if_non_prod_aws,
mock_if_non_prod_aws,
send_email,
)

Expand Down Expand Up @@ -357,8 +357,8 @@ def create_federated_user(name, repository, aws_keys):
return response


# @mock_if_non_prod_aws(mock_ecr)
# @mock_if_non_prod_aws(mock_sts)
@mock_if_non_prod_aws(mock_ecr)
@mock_if_non_prod_aws(mock_sts)
def get_aws_credentials_for_submission(challenge, participant_team):
"""
Method to generate AWS Credentails for CLI's Push
Expand Down
Loading

0 comments on commit e12cee4

Please sign in to comment.