Skip to content

Commit

Permalink
Add quit event, used for getting locust's exit code just before exit (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
DennisKrone authored Mar 15, 2022
1 parent 6bddf09 commit dd53fdd
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 5 deletions.
8 changes: 7 additions & 1 deletion examples/test_data_management.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
# 6. Test run stopping
# 7. User stop
# 8. Test run stop
# 9. (not shown in this example) Locust quit
# 9. Locust quitting
# 10. Locust quit
#
# try it out by running:
# locust -f test_data_management.py --headless -u 2 -t 5
Expand Down Expand Up @@ -61,6 +62,11 @@ def _(environment, **_kwargs):
).json()["data"]


@events.quit.add_listener
def _(environment, exit_code, **kwargs):
print(f"Locust has shut down with code {exit_code}")


@events.test_stopping.add_listener
def _(environment, **_kwargs):
print("stopping test run")
Expand Down
11 changes: 10 additions & 1 deletion locust/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,13 +149,22 @@ class Events:

quitting: EventHook
"""
Fired when the locust process is exiting
Fired when the locust process is exiting.
Event arguments:
:param environment: Environment instance
"""

quit: EventHook
"""
Fired after quitting events, just before process is exited.
Event arguments:
:param exit_code: Exit code for process
"""

init: EventHook
"""
Fired when Locust is started, once the Environment instance and locust runner instance
Expand Down
8 changes: 5 additions & 3 deletions locust/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -425,16 +425,17 @@ def shutdown():
environment.events.quitting.fire(environment=environment, reverse=True)

# determine the process exit code
if log.unhandled_greenlet_exception:
code = 2
elif environment.process_exit_code is not None:
if environment.process_exit_code is not None:
code = environment.process_exit_code
elif len(runner.errors) or len(runner.exceptions):
code = options.exit_code_on_error
elif log.unhandled_greenlet_exception:
code = 2
else:
code = 0

logger.info(f"Shutting down (exit code {code})")

if stats_printer_greenlet is not None:
stats_printer_greenlet.kill(block=False)
if headless_master_greenlet is not None:
Expand All @@ -448,6 +449,7 @@ def shutdown():
print_percentile_stats(runner.stats)
print_error_report(runner.stats)

environment.events.quit.fire(exit_code=code)
sys.exit(code)

# install SIGTERM handler
Expand Down
5 changes: 5 additions & 0 deletions locust/test/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,9 @@ def test_custom_exit_code(self):
@events.quitting.add_listener
def _(environment, **kw):
environment.process_exit_code = 42
@events.quit.add_listener
def _(exit_code, **kw):
print(f"Exit code in quit event {exit_code}")
class TestUser(User):
wait_time = constant(3)
@task
Expand All @@ -279,9 +282,11 @@ def my_task(self):
proc.send_signal(signal.SIGTERM)
stdout, stderr = proc.communicate()
stderr = stderr.decode("utf-8")
stdout = stdout.decode("utf-8")
self.assertIn("Starting web interface at", stderr)
self.assertIn("Starting Locust", stderr)
self.assertIn("Shutting down (exit code 42)", stderr)
self.assertIn("Exit code in quit event 42", stdout)
self.assertEqual(42, proc.returncode)

def test_webserver(self):
Expand Down

0 comments on commit dd53fdd

Please sign in to comment.