Skip to content

Commit

Permalink
Add clean job task on startup
Browse files Browse the repository at this point in the history
  • Loading branch information
ldeluigi committed Oct 6, 2022
1 parent f8c9269 commit c5fa672
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
1 change: 1 addition & 0 deletions backend/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ ENV DJANGO_SETTINGS_MODULE=backend.production_settings
RUN echo "python manage.py collectstatic --no-input --clear" > entrypoint.prod.sh
RUN echo "sleep 3" >> entrypoint.prod.sh
RUN echo "python manage.py migrate --noinput" >> entrypoint.prod.sh
RUN echo "python manage.py clean_jobs" >> entrypoint.prod.sh
RUN echo "python manage.py export_variants" >> entrypoint.prod.sh
RUN echo "gunicorn backend.wsgi:application --bind 0.0.0.0:8000" >> entrypoint.prod.sh
EXPOSE 8000
Expand Down
25 changes: 25 additions & 0 deletions backend/spellbook/management/commands/clean_jobs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from django.utils import timezone
from django.core.management.base import BaseCommand
from spellbook.models import Job


class Command(BaseCommand):
help = 'Clean pending jobs'

def handle(self, *args, **options):
self.stdout.write('Cleaning pending jobs...')
query = Job.objects.filter(status=Job.Status.PENDING)
count = query.count()
if count > 0:
query.update(status=Job.Status.FAILURE, message='Job was cancelled.', termination=timezone.now())
now = timezone.now() + timezone.timedelta(seconds=2)
job = Job(
name="cancel_jobs",
expected_termination=now,
termination=now,
status=Job.Status.SUCCESS,
message=f"{count} pending jobs were cancelled.")
job.save()
self.stdout.write(self.style.SUCCESS(f"{count} pending jobs were cancelled."))
else:
self.stdout.write(self.style.SUCCESS("No pending jobs to cancel."))

0 comments on commit c5fa672

Please sign in to comment.