|
| 1 | +from mrq.job import Job |
| 2 | +import pytest |
| 3 | +from mrq.queue import Queue, send_task |
| 4 | +import time |
| 5 | +from mrq.context import set_current_config, get_config |
| 6 | + |
| 7 | + |
| 8 | +def test_pause_resume(worker): |
| 9 | + |
| 10 | + worker.start(flags="--paused_queues_refresh_interval=0.1") |
| 11 | + |
| 12 | + Queue("high").pause() |
| 13 | + |
| 14 | + assert Queue("high").is_paused() |
| 15 | + |
| 16 | + # wait for the paused_queues list to be refreshed |
| 17 | + time.sleep(2) |
| 18 | + |
| 19 | + job_id1 = send_task( |
| 20 | + "tests.tasks.general.MongoInsert", {"a": 41}, |
| 21 | + queue="high") |
| 22 | + |
| 23 | + job_id2 = send_task( |
| 24 | + "tests.tasks.general.MongoInsert", {"a": 43}, |
| 25 | + queue="low") |
| 26 | + |
| 27 | + time.sleep(5) |
| 28 | + |
| 29 | + job1 = Job(job_id1).fetch().data |
| 30 | + job2 = Job(job_id2).fetch().data |
| 31 | + |
| 32 | + assert job1["status"] == "queued" |
| 33 | + |
| 34 | + assert job2["status"] == "success" |
| 35 | + assert job2["result"] == {"a": 43} |
| 36 | + |
| 37 | + assert worker.mongodb_jobs.tests_inserts.count() == 1 |
| 38 | + |
| 39 | + Queue("high").resume() |
| 40 | + |
| 41 | + Job(job_id1).wait(poll_interval=0.01) |
| 42 | + |
| 43 | + job1 = Job(job_id1).fetch().data |
| 44 | + |
| 45 | + assert job1["status"] == "success" |
| 46 | + assert job1["result"] == {"a": 41} |
| 47 | + |
| 48 | + assert worker.mongodb_jobs.tests_inserts.count() == 2 |
| 49 | + |
| 50 | + worker.stop() |
| 51 | + |
| 52 | + |
| 53 | +def test_pause_refresh_interval(worker): |
| 54 | + |
| 55 | + """ Tests that a refresh interval of 0 disables the pause functionnality """ |
| 56 | + |
| 57 | + worker.start(flags="--paused_queues_refresh_interval=0") |
| 58 | + |
| 59 | + Queue("high").pause() |
| 60 | + |
| 61 | + assert Queue("high").is_paused() |
| 62 | + |
| 63 | + # wait for the paused_queues list to be refreshed |
| 64 | + time.sleep(2) |
| 65 | + |
| 66 | + job_id1 = send_task( |
| 67 | + "tests.tasks.general.MongoInsert", {"a": 41}, |
| 68 | + queue="high") |
| 69 | + |
| 70 | + time.sleep(5) |
| 71 | + |
| 72 | + job1 = Job(job_id1).fetch().data |
| 73 | + |
| 74 | + assert job1["status"] == "success" |
| 75 | + assert job1["result"] == {"a": 41} |
| 76 | + |
| 77 | + worker.stop() |
| 78 | + |
| 79 | + |
| 80 | +def test_pause_subqueue(worker): |
| 81 | + |
| 82 | + # set config in current context in order to have a subqueue delimiter |
| 83 | + set_current_config(get_config(config_type="worker")) |
| 84 | + |
| 85 | + worker.start(queues="high high/", flags="--subqueues_refresh_interval=1 --paused_queues_refresh_interval=1") |
| 86 | + |
| 87 | + Queue("high").pause() |
| 88 | + |
| 89 | + assert Queue("high/").is_paused() |
| 90 | + |
| 91 | + # wait for the paused_queues list to be refreshed |
| 92 | + time.sleep(2) |
| 93 | + |
| 94 | + job_id1 = send_task( |
| 95 | + "tests.tasks.general.MongoInsert", {"a": 41}, |
| 96 | + queue="high") |
| 97 | + |
| 98 | + job_id2 = send_task( |
| 99 | + "tests.tasks.general.MongoInsert", {"a": 43}, |
| 100 | + queue="high/subqueue") |
| 101 | + |
| 102 | + # wait a bit to make sure the jobs status will still be queued |
| 103 | + time.sleep(5) |
| 104 | + |
| 105 | + job1 = Job(job_id1).fetch().data |
| 106 | + job2 = Job(job_id2).fetch().data |
| 107 | + |
| 108 | + assert job1["status"] == "queued" |
| 109 | + assert job2["status"] == "queued" |
| 110 | + |
| 111 | + assert worker.mongodb_jobs.tests_inserts.count() == 0 |
| 112 | + |
| 113 | + Queue("high/").resume() |
| 114 | + |
| 115 | + Job(job_id1).wait(poll_interval=0.01) |
| 116 | + |
| 117 | + Job(job_id2).wait(poll_interval=0.01) |
| 118 | + |
| 119 | + job1 = Job(job_id1).fetch().data |
| 120 | + job2 = Job(job_id2).fetch().data |
| 121 | + |
| 122 | + assert job1["status"] == "success" |
| 123 | + assert job1["result"] == {"a": 41} |
| 124 | + |
| 125 | + assert job2["status"] == "success" |
| 126 | + assert job2["result"] == {"a": 43} |
| 127 | + |
| 128 | + assert worker.mongodb_jobs.tests_inserts.count() == 2 |
| 129 | + |
| 130 | + worker.stop() |
0 commit comments