-
Notifications
You must be signed in to change notification settings - Fork 115
Max time #152
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Max time #152
Changes from 5 commits
09128f2
ea442d7
5afebad
e2e44e5
ab4527a
1d53fb0
3b55b6d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,7 +15,6 @@ | |
| import sys | ||
| import json as json_stdlib | ||
| import ujson as json | ||
| import http.server | ||
| from bson import ObjectId | ||
| from collections import defaultdict | ||
|
|
||
|
|
@@ -53,6 +52,8 @@ def __init__(self): | |
|
|
||
| self.done_jobs = 0 | ||
| self.max_jobs = self.config["max_jobs"] | ||
| max_time = self.config.get("max_time") | ||
| self.max_time = datetime.timedelta(seconds=max_time) if max_time is not None else None | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. à priori max_time sera toujours défini (comme il est dans la config) donc le .get() et le test sur None sont inutiles |
||
|
|
||
| self.connected = False # MongoDB + Redis | ||
|
|
||
|
|
@@ -419,7 +420,7 @@ def work(self): | |
| """ | ||
| self.work_init() | ||
|
|
||
| self.work_loop(max_jobs=self.max_jobs) | ||
| self.work_loop(max_jobs=self.max_jobs, max_time=self.max_time) | ||
|
|
||
| return self.work_stop() | ||
|
|
||
|
|
@@ -449,7 +450,7 @@ def work_init(self): | |
|
|
||
| self.install_signal_handlers() | ||
|
|
||
| def work_loop(self, max_jobs=None): | ||
| def work_loop(self, max_jobs=None, max_time=None): | ||
|
|
||
| self.done_jobs = 0 | ||
| self.idle_wait_count = 0 | ||
|
|
@@ -459,6 +460,7 @@ def work_loop(self, max_jobs=None): | |
| try: | ||
|
|
||
| queue_offset = 0 | ||
| max_time_reached = False | ||
|
|
||
| while True: | ||
|
|
||
|
|
@@ -472,6 +474,12 @@ def work_loop(self, max_jobs=None): | |
|
|
||
| while True: | ||
|
|
||
| # we put this here to make sure we have a strict limit on max_time | ||
| if max_time and datetime.datetime.utcnow() - self.datestarted >= max_time: | ||
| self.log.info("Reached max_time=%s" % max_time.seconds) | ||
| max_time_reached = True | ||
| break | ||
|
|
||
| free_pool_slots = self.gevent_pool.free_count() | ||
|
|
||
| if max_jobs: | ||
|
|
@@ -487,6 +495,9 @@ def work_loop(self, max_jobs=None): | |
| self.status = "full" | ||
| gevent.sleep(0.01) | ||
|
|
||
| if max_time_reached: | ||
| break | ||
|
|
||
| jobs = [] | ||
|
|
||
| available_queues = [ | ||
|
|
@@ -561,7 +572,6 @@ def work_loop(self, max_jobs=None): | |
|
|
||
| self.log.debug("Joining the greenlet pool...") | ||
| self.status = "join" | ||
|
|
||
| self.gevent_pool.join(timeout=None, raise_error=False) | ||
| self.log.debug("Joined.") | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -322,6 +322,30 @@ def test_interrupt_maxjobs(worker): | |
| assert Queue("default").size() == 7 | ||
|
|
||
|
|
||
| def test_worker_interrupt_after_max_time(worker): | ||
| worker.start(flags="--greenlets=2 --max_time=1", queues="test1 default") | ||
|
|
||
| worker.send_tasks("tests.tasks.general.Add", [{"a": i, "b": 1, "sleep": 1} for i in range(5)], block=False) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. j'aurais bien fait max_time=2 & sleep=3 pour être sûrs des timings |
||
|
|
||
| time.sleep(3) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. et du coup ici sleep(5) |
||
|
|
||
| assert Queue("default").size() == 3 | ||
|
|
||
|
|
||
| def test_worker_runs_but_interrupt_after_max_time(worker): | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. pas bien compris ce que ca teste comparé au précédent ?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ca récupère juste le résultat pour vérifier qu'il a été bien calculé
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. je combinerais bien les 2 en un seul alors |
||
| worker.start(flags="--greenlets=2 --max_time=1", queues="test1 default") | ||
|
|
||
| result = worker.send_task("tests.tasks.general.Add", {"a": 2, "b": 1, "sleep": 1}, block=True) | ||
|
|
||
| assert result == 3 | ||
|
|
||
| worker.send_tasks("tests.tasks.general.Add", [{"a": i, "b": 1, "sleep": 1} for i in range(5)], block=False) | ||
|
|
||
| time.sleep(3) | ||
|
|
||
| assert Queue("default").size() == 5 | ||
|
|
||
|
|
||
| def test_interrupt_maxconcurrency(worker): | ||
|
|
||
| # The worker will raise a maxconcurrency on the second job | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pas très grave, mais plutôt float ?