Skip to content

Commit

Permalink
refactor: expire last calls via celery (#7417)
Browse files Browse the repository at this point in the history
* feat: expire_last_calls_task

* feat: create periodic task for last calls

* test: test new task

* chore: remove expire-last-calls script
  • Loading branch information
jennifer-richards authored May 15, 2024
1 parent 46a00ac commit c9f3598
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 39 deletions.
4 changes: 0 additions & 4 deletions bin/daily
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,5 @@ $DTDIR/ietf/manage.py populate_yang_model_dirs -v0
# Re-run yang checks on active documents
$DTDIR/ietf/manage.py run_yang_model_checks -v0

# Expire last calls
# Enable when removed from /a/www/ietf-datatracker/scripts/Cron-runner:
$DTDIR/ietf/bin/expire-last-calls

# Purge older PersonApiKeyEvents
$DTDIR/ietf/manage.py purge_old_personal_api_key_events 14
34 changes: 0 additions & 34 deletions ietf/bin/expire-last-calls

This file was deleted.

12 changes: 12 additions & 0 deletions ietf/doc/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
get_soon_to_expire_drafts,
send_expire_warning_for_draft,
)
from .lastcall import get_expired_last_calls, expire_last_call
from .models import Document
from .utils import generate_idnits2_rfc_status, generate_idnits2_rfcs_obsoleted

Expand Down Expand Up @@ -61,6 +62,17 @@ def notify_expirations_task(notify_days=14):


@shared_task
def expire_last_calls_task():
for doc in get_expired_last_calls():
try:
expire_last_call(doc)
except Exception:
log.log(f"ERROR: Failed to expire last call for {doc.file_tag()} (id={doc.pk})")
else:
log.log(f"Expired last call for {doc.file_tag()} (id={doc.pk})")


@shared_task
def generate_idnits2_rfc_status_task():
outpath = Path(settings.DERIVED_DIR) / "idnits2-rfc-status"
blob = generate_idnits2_rfc_status()
Expand Down
25 changes: 24 additions & 1 deletion ietf/doc/tests_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from .models import Document
from .tasks import (
expire_ids_task,
expire_last_calls_task,
generate_idnits2_rfcs_obsoleted_task,
generate_idnits2_rfc_status_task,
notify_expirations_task,
Expand Down Expand Up @@ -71,6 +72,29 @@ def test_notify_expirations_task(self, get_drafts_mock, send_warning_mock):
self.assertEqual(send_warning_mock.call_count, 1)
self.assertEqual(send_warning_mock.call_args[0], ("sentinel",))

@mock.patch("ietf.doc.tasks.expire_last_call")
@mock.patch("ietf.doc.tasks.get_expired_last_calls")
def test_expire_last_calls_task(self, mock_get_expired, mock_expire):
docs = DocumentFactory.create_batch(3)
mock_get_expired.return_value = docs
expire_last_calls_task()
self.assertTrue(mock_get_expired.called)
self.assertEqual(mock_expire.call_count, 3)
self.assertEqual(mock_expire.call_args_list[0], mock.call(docs[0]))
self.assertEqual(mock_expire.call_args_list[1], mock.call(docs[1]))
self.assertEqual(mock_expire.call_args_list[2], mock.call(docs[2]))

# Check that it runs even if exceptions occur
mock_get_expired.reset_mock()
mock_expire.reset_mock()
mock_expire.side_effect = ValueError
expire_last_calls_task()
self.assertTrue(mock_get_expired.called)
self.assertEqual(mock_expire.call_count, 3)
self.assertEqual(mock_expire.call_args_list[0], mock.call(docs[0]))
self.assertEqual(mock_expire.call_args_list[1], mock.call(docs[1]))
self.assertEqual(mock_expire.call_args_list[2], mock.call(docs[2]))

@mock.patch("ietf.doc.tasks.generate_idnits2_rfc_status")
def test_generate_idnits2_rfc_status_task(self, mock_generate):
mock_generate.return_value = "dåtå"
Expand All @@ -90,4 +114,3 @@ def test_generate_idnits2_rfcs_obsoleted_task(self, mock_generate):
"dåtå".encode("utf8"),
(Path(settings.DERIVED_DIR) / "idnits2-rfcs-obsoleted").read_bytes(),
)

10 changes: 10 additions & 0 deletions ietf/utils/management/commands/periodic_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,16 @@ def create_default_tasks(self):
),
)

PeriodicTask.objects.get_or_create(
name="Expire Last Calls",
task="ietf.doc.tasks.expire_last_calls_task",
defaults=dict(
enabled=False,
crontab=self.crontabs["daily"],
description="Move docs whose last call has expired to their next states",
),
)

PeriodicTask.objects.get_or_create(
name="Sync with IANA changes",
task="ietf.sync.tasks.iana_changes_update_task",
Expand Down

0 comments on commit c9f3598

Please sign in to comment.