Browse Source

Introduce a maximum age for tasks (to avoid large backlogs)

Baptiste Jonglez 10 years ago
parent
commit
592373d131
2 changed files with 13 additions and 2 deletions
  1. 5 0
      config.py.sample
  2. 8 2
      peerfinder.py

+ 5 - 0
config.py.sample

@@ -4,3 +4,8 @@ SQLALCHEMY_DATABASE_URI = 'sqlite:////tmp/test.db'
 
 
 # Used when generating the measurement script
 # Used when generating the measurement script
 PEERFINDER_DN42 = "http://127.0.0.1:8888"
 PEERFINDER_DN42 = "http://127.0.0.1:8888"
+
+# Maximum age, in seconds, after which a task is not distributed to
+# participants anymore.  This avoids a huge backlog when new participants
+# join the worker pool.  Default: 0 (no restriction)
+MAX_AGE = 10800

+ 8 - 2
peerfinder.py

@@ -13,7 +13,7 @@ from netaddr import IPAddress
 from netaddr.strategy.ipv4 import packed_to_int as unpack_v4
 from netaddr.strategy.ipv4 import packed_to_int as unpack_v4
 from netaddr.strategy.ipv6 import packed_to_int as unpack_v6
 from netaddr.strategy.ipv6 import packed_to_int as unpack_v6
 import socket
 import socket
-from datetime import datetime
+from datetime import datetime, timedelta
 from uuid import uuid4
 from uuid import uuid4
 
 
 app = Flask(__name__)
 app = Flask(__name__)
@@ -168,7 +168,13 @@ def get_targets(uuid):
     # participant.
     # participant.
     already_done = Target.query.join(handled_targets).filter_by(participant_id=participant.id).with_entities(Target.id)
     already_done = Target.query.join(handled_targets).filter_by(participant_id=participant.id).with_entities(Target.id)
     # This takes the negation of the previous set.
     # This takes the negation of the previous set.
-    return Target.query.filter(~Target.id.in_(already_done))
+    new_tasks = Target.query.filter(~Target.id.in_(already_done))
+    max_age = app.config.get('MAX_AGE', 0)
+    if max_age == 0:
+        return new_tasks
+    else:
+        limit = datetime.now() - timedelta(seconds=max_age)
+        return new_tasks.filter(Target.submitted >= limit)
 
 
 
 
 @app.route('/')
 @app.route('/')