Skip to content

Commit 46a00ac

Browse files
refactor: sync to RFC Editor queue via celery (ietf-tools#7415)
* feat: rfc_editor_queue_updates_task * refactor: use rfc_editor_queue_updates_task() * chore: remove now-unused scripts * test: test new task * chore: de-lint
1 parent a4e0354 commit 46a00ac

5 files changed

Lines changed: 61 additions & 170 deletions

File tree

ietf/bin/rfc-editor-index-updates

Lines changed: 0 additions & 110 deletions
This file was deleted.

ietf/bin/rfc-editor-queue-updates

Lines changed: 0 additions & 44 deletions
This file was deleted.

ietf/sync/tasks.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
from ietf.sync import iana
1515
from ietf.sync import rfceditor
16+
from ietf.sync.rfceditor import MIN_QUEUE_RESULTS, parse_queue, update_drafts_from_queue
1617
from ietf.utils import log
1718
from ietf.utils.timezone import date_today
1819

@@ -70,6 +71,33 @@ def rfc_editor_index_update_task(full_index=False):
7071
log.log("RFC%s, %s: %s" % (rfc_number, doc.name, c))
7172

7273

74+
@shared_task
75+
def rfc_editor_queue_updates_task():
76+
log.log(f"Updating RFC Editor queue states from {settings.RFC_EDITOR_QUEUE_URL}")
77+
try:
78+
response = requests.get(
79+
settings.RFC_EDITOR_QUEUE_URL,
80+
timeout=30, # seconds
81+
)
82+
except requests.Timeout as exc:
83+
log.log(f"GET request timed out retrieving RFC editor queue: {exc}")
84+
return # failed
85+
drafts, warnings = parse_queue(io.StringIO(response.text))
86+
for w in warnings:
87+
log.log(f"Warning: {w}")
88+
89+
if len(drafts) < MIN_QUEUE_RESULTS:
90+
log.log("Not enough results, only %s" % len(drafts))
91+
return # failed
92+
93+
changed, warnings = update_drafts_from_queue(drafts)
94+
for w in warnings:
95+
log.log(f"Warning: {w}")
96+
97+
for c in changed:
98+
log.log(f"Updated {c}")
99+
100+
73101
@shared_task
74102
def iana_changes_update_task():
75103
# compensate to avoid we ask for something that happened now and then

ietf/sync/tests.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -886,6 +886,36 @@ def json(self):
886886
tasks.rfc_editor_index_update_task(full_index=False)
887887
self.assertFalse(update_docs_mock.called)
888888

889+
@override_settings(RFC_EDITOR_QUEUE_URL="https://rfc-editor.example.com/queue/")
890+
@mock.patch("ietf.sync.tasks.update_drafts_from_queue")
891+
@mock.patch("ietf.sync.tasks.parse_queue")
892+
def test_rfc_editor_queue_updates_task(self, mock_parse, mock_update):
893+
# test a request timeout
894+
self.requests_mock.get("https://rfc-editor.example.com/queue/", exc=requests.exceptions.Timeout)
895+
tasks.rfc_editor_queue_updates_task()
896+
self.assertFalse(mock_parse.called)
897+
self.assertFalse(mock_update.called)
898+
899+
# now return a value rather than an exception
900+
self.requests_mock.get("https://rfc-editor.example.com/queue/", text="the response")
901+
902+
# mock returning < MIN_QUEUE_RESULTS values - treated as an error, so no update takes place
903+
mock_parse.return_value = ([n for n in range(rfceditor.MIN_QUEUE_RESULTS - 1)], ["a warning"])
904+
tasks.rfc_editor_queue_updates_task()
905+
self.assertEqual(mock_parse.call_count, 1)
906+
self.assertEqual(mock_parse.call_args[0][0].read(), "the response")
907+
self.assertFalse(mock_update.called)
908+
mock_parse.reset_mock()
909+
910+
# mock returning +. MIN_QUEUE_RESULTS - should succeed
911+
mock_parse.return_value = ([n for n in range(rfceditor.MIN_QUEUE_RESULTS)], ["a warning"])
912+
mock_update.return_value = ([1,2,3], ["another warning"])
913+
tasks.rfc_editor_queue_updates_task()
914+
self.assertEqual(mock_parse.call_count, 1)
915+
self.assertEqual(mock_parse.call_args[0][0].read(), "the response")
916+
self.assertEqual(mock_update.call_count, 1)
917+
self.assertEqual(mock_update.call_args, mock.call([n for n in range(rfceditor.MIN_QUEUE_RESULTS)]))
918+
889919
@override_settings(IANA_SYNC_CHANGES_URL="https://iana.example.com/sync/")
890920
@mock.patch("ietf.sync.tasks.iana.update_history_with_changes")
891921
@mock.patch("ietf.sync.tasks.iana.parse_changes_json")

ietf/sync/views.py

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
# -*- coding: utf-8 -*-
33

44
import datetime
5-
import subprocess
65
import os
76
import json
87

@@ -79,30 +78,18 @@ def notify(request, org, notification):
7978
raise Http404
8079

8180
if request.method == "POST":
82-
def runscript(name):
83-
python = os.path.join(os.path.dirname(settings.BASE_DIR), "env", "bin", "python")
84-
cmd = [python, os.path.join(SYNC_BIN_PATH, name)]
85-
cmdstring = " ".join(cmd)
86-
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
87-
out, err = p.communicate()
88-
out = out.decode('utf-8')
89-
err = err.decode('utf-8')
90-
if p.returncode:
91-
log("Subprocess error %s when running '%s': %s %s" % (p.returncode, cmd, err, out))
92-
raise subprocess.CalledProcessError(p.returncode, cmdstring, "\n".join([err, out]))
93-
9481
if notification == "index":
9582
log("Queuing RFC Editor index sync from notify view POST")
9683
tasks.rfc_editor_index_update_task.delay()
84+
elif notification == "queue":
85+
log("Queuing RFC Editor queue sync from notify view POST")
86+
tasks.rfc_editor_queue_updates_task.delay()
9787
elif notification == "changes":
9888
log("Queuing IANA changes sync from notify view POST")
9989
tasks.iana_changes_update_task.delay()
10090
elif notification == "protocols":
10191
log("Queuing IANA protocols sync from notify view POST")
10292
tasks.iana_protocols_update_task.delay()
103-
elif notification == "queue":
104-
log("Running sync script from notify view POST")
105-
runscript("rfc-editor-queue-updates")
10693

10794
return HttpResponse("OK", content_type="text/plain; charset=%s"%settings.DEFAULT_CHARSET)
10895

0 commit comments

Comments
 (0)