【问题标题】:How to run a server in background with CircleCI?如何使用 CircleCI 在后台运行服务器?
【发布时间】:2019-07-20 08:10:01
【问题描述】:

我在我的 Django 项目中使用 CircleCI。我想在后台运行一个服务器(特别是python manage.py runserver)以进行一些特定的硒测试。

我的config.yml有点像

version: 2
jobs:
  build:
    docker:
      - image: circleci/python:3.6.1-browsers
      - image: selenium/standalone-chrome

    working_directory: ~/myproject

    steps:
      - checkout
      - run:
          name: install dependencies
          command: |
            python3 -m venv venv
            . venv/bin/activate
            pip install -r requirements.txt

      - run:
          name: run unit tests
          command: |
            . venv/bin/activate
            python manage.py test

      - run:
          name: run selenium tests
          command: |
            . venv/bin/activate
            python manage.py migrate
            python manage.py runserver 8000 
            python manage.py run_selenium_tests         

我可以通过在 django LiveServerTestCase 中运行 selenium 测试来使其工作。但我想独立运行 selenium 测试,因为我需要 runserver 在后台运行。现在 circleci 在python manage.py runserver 处停止执行并最终超时。有什么想法吗?

【问题讨论】:

    标签: python django continuous-integration circleci


    【解决方案1】:

    您需要以background command 身份启动服务器。或者,您也可以使用 cURL 等待服务器准备好。

    根据您发布的配置,您可以执行以下操作:

    version: 2
    jobs:
      build:
        docker:
          - image: circleci/python:3.6.1-browsers
          - image: selenium/standalone-chrome
    
        working_directory: ~/myproject
    
        steps:
          - checkout
          - run:
              name: install dependencies
              command: |
                python3 -m venv venv
                . venv/bin/activate
                pip install -r requirements.txt
    
          - run:
              name: run unit tests
              command: |
                . venv/bin/activate
                python manage.py test
    
          - run:
              name: run selenium tests prep
              command: |
                . venv/bin/activate
                python manage.py migrate
          - run:
              name: run server
              command: python manage.py runserver 8000
              background: true
          - run:
              name: run selenium tests
              command: |
                curl --retry-delay 5 --retry 10  --retry-connrefused http://localhost:8000
                python manage.py run_selenium_tests
    

    curl 语句在继续之前等待端口响应。这让您的服务器有时间完全启动。

    - 里卡多·N·费利西亚诺
    开发人员布道师,CircleCI

    【讨论】:

    • 当我尝试这个时,我收到了这个错误curl: option --retry-connrefused: is unknown。但是当我删除curl 时工作。谢谢!
    • 我必须在curl 之前使用sleep 5,否则它会因curl: (7) Failed to connect to localhost port 8000: Connection refused 而失败
    • 嗨,Richard,您能建议另一种方法来尝试服务器直到连接成功吗?我正在使用 CircleCI 并且 curl 也失败了。
    • 尝试 Dockerize:discuss.circleci.com/t/…
    • 如果 --retry-connrefused “似乎”被忽略,例如在 docker 容器中,请尝试添加使 curl 使用 IPv4 的“-4”选项。见这里:github.com/curl/curl/issues/5080
    【解决方案2】:

    我不使用 CircleCI,但问题是 manage.py runserver 正在阻塞,即 - 它不在后台运行。根据 CircleCI 文档,您可以守护进程:

    https://circleci.com/docs/1.0/background-process/

    至于之后你是否可以访问端口(CI 是否允许你绑定到端口?),我不确定。

    【讨论】:

      【解决方案3】:

      让它与 gunicorn 一起工作。这是config.yml

        # run tests!
        - run:
            name: run unittests
            command: |
              . venv/bin/activate
              python manage.py test
              python manage.py migrate
      
        - run:
            name: run server in background
            command: |
              . venv/bin/activate
              gunicorn myproject.wsgi:application --bind=127.0.0.1:8000 --pid=gunicorn.txt --daemon
            background: true
      
        - run:
            name: run selenium test
            command: |
              . venv/bin/activate
              python manage.py run_selenium_tests
              kill -9 `cat gunicorn.txt`
      

      【讨论】:

        猜你喜欢
        • 2015-07-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-02-04
        • 2020-03-06
        • 2017-01-28
        • 1970-01-01
        相关资源
        最近更新 更多