1

I have a Flask application that spins up my web application to a specific port (e.g., 8080). It launches by running python run.py. I also have a Python file that runs Selenium end-to-end tests for the web app.

This is part of a GitHub repo, and I have it hooked up so that whenever I push/commit to the GitHub repo, it initiates a Jenkins test. Jenkins is using my machine as the agent to run the commands. I have a series of commands in a Pipeline on Jenkins to execute.

Ideally, the order of commands should be:

  1. Spin up the Flask web application
  2. Run the Selenium tests
  3. Terminate the Flask web application

Running Flask and Selenium in parallel in a Jenkins Pipeline

Now, my issue is this: I can use Jenkins to clone the repo to the local workspace on my computer (the agent), and then run the Python command to spin up my web app. I can use Jenkins to run a task in parallel to do the Selenium tests on the port used by the web app.

BUT I cannot figure out how to end the Flask web app once the Selenium tests are done. If I don't end the run.py execution, it is stuck on an infinite loop, endlessly serving up the application to the port. I can have Jenkins automatically abort after some specified amount of time, but that flags the entire process as a failure.

Does anyone have a good idea of how to end the Flask hosting process once the Selenium tests are done? Or, if I am doing this in a block-headed way (I admit I am speaking out of copious amounts of ignorance and inexperience), is there a better way I should be handling this?

cthelin7
  • 51
  • 6
  • Just to clarify, I don't need Selenium to do the shutting down; I only mentioned Selenium to illustrate why I am doing things this way. The critical part is the actual act of shutting down the port programatically. Thanks! – cthelin7 Jul 01 '20 at 16:34
  • Does this answer your question? [How to run Flask Server in the background](https://stackoverflow.com/questions/36465899/how-to-run-flask-server-in-the-background) – Greg Burghardt Jul 01 '20 at 17:08

1 Answers1

0

I'm not aware of an official way to cause the development server to exit under Selenium control, but the following route works with Flask 1.1.2

from flask import request
...

if settings.DEBUG:
    @app.route('/quit')
    def quit():
        shutdown_hook = request.environ.get('werkzeug.server.shutdown')
        if shutdown_hook is not None:
            shutdown_hook()
            return "Bye"
        return "No shutdown hook"
Dave W. Smith
  • 24,318
  • 4
  • 40
  • 46
  • Thanks, Dave. Creating a shutdown route would definitely work, but I am hesitant about having a route that just anyone could use to shutdown my server. I wonder if there would be a way to protect just that one route, keep it open only for testing purposes? – cthelin7 Jul 01 '20 at 16:33
  • You can selectively enable the route, e.g., only if DEBUG is enabled. I changed the example slightly to demonstrate that. – Dave W. Smith Jul 01 '20 at 17:03
  • use token_required decorator to protect the route. – Aritra Chatterjee Oct 23 '21 at 06:29