Skip to content

Commit e4beadc

Browse files
committed
refactor: make listener a Django management command
1 parent 3eefb47 commit e4beadc

File tree

4 files changed

+45
-45
lines changed

4 files changed

+45
-45
lines changed

docs/architecture.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ This is a fairly reliable endpoint which also lets us resume the stream from a r
3333
ingestion stopped for some reason. By default, the listener tries to resume listening from the
3434
date of the latest edit it has ingested.
3535

36-
This process can be invoked directly as a script::
36+
This process can be invoked directly as a Django management command::
3737

38-
python listener.py
38+
python3 manage.py listener
3939

4040
It can be run as an `attached daemon to uwsgi <https://uwsgi-docs.readthedocs.io/en/latest/AttachingDaemons.html>`_.
4141

docs/install.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ start a local web server, you can then access your EditGroups instance at ``http
3333
By default, there will not be much to see as the database will be empty. To get some data in, you need
3434
to run the listener script, which reads the Wikidata event stream and populates the database::
3535

36-
python listener.py
36+
python3 manage.py listener
3737

3838
You will also need to run Celery, which will periodically annotate edits which need inspection,
3939
as well as providing the undo functionality (if you have set up OAuth, see below)::
@@ -130,7 +130,7 @@ Put the following content in ``~/www/python/uwsgi.ini``::
130130
static-map = /static=/data/project/editgroups/www/python/src/static
131131

132132
master = true
133-
attach-daemon = /data/project/editgroups/www/python/venv/bin/python3 /data/project/editgroups/www/python/src/listener.py
133+
attach-daemon = /data/project/editgroups/www/python/venv/bin/python3 /data/project/editgroups/www/python/src/manage.py listener
134134

135135
and run ``./manage.py collectstatic`` in the ``~/www/python/src`` directory. The listener will be an attached dameon, restarting with webservice restart.
136136

listener.py

Lines changed: 0 additions & 41 deletions
This file was deleted.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/usr/bin/env python
2+
import sys
3+
from datetime import datetime
4+
from datetime import timedelta
5+
6+
from django.core.management.base import BaseCommand
7+
8+
from store.stream import WikiEditStream
9+
from store.utils import grouper
10+
from store.models import Edit
11+
12+
13+
class Command(BaseCommand):
14+
"""
15+
Amount of time to look back when restarting
16+
the listener. This helps make sure that we don't
17+
lose any edit when the listener is restarted.
18+
"""
19+
20+
LOOKBEHIND_OFFSET = timedelta(minutes=5)
21+
help = "Listens to edits with EventStream"
22+
23+
def handle(self, *args, **options):
24+
print("Listening to edits...")
25+
s = WikiEditStream()
26+
try:
27+
latest_edit_seen = Edit.objects.order_by("-timestamp")[0].timestamp
28+
fetch_from = latest_edit_seen - self.LOOKBEHIND_OFFSET
29+
except IndexError:
30+
fetch_from = None
31+
offset = fetch_from.isoformat() if fetch_from else "now"
32+
print("Starting from offset %s" % offset)
33+
34+
for i, batch in enumerate(grouper(s.stream(fetch_from), 50)):
35+
if i % 50 == 0:
36+
print("batch %d" % i)
37+
print(datetime.fromtimestamp(batch[0].get("timestamp")))
38+
sys.stdout.flush()
39+
Edit.ingest_edits(batch)
40+
41+
print("End of stream")

0 commit comments

Comments
 (0)