gpt4 book ai didi

ruby-on-rails - 在 Google Cloud Run 中使用 Sidekiq 的 Rails 作业不起作用

转载 作者:行者123 更新时间:2023-12-03 06:39:38 25 4
gpt4 key购买 nike

我在 Google Cloud Run 中有一个可用的 Rails 应用程序,使用:

  • 云构建
  • 云注册
  • 云跑
  • 云 SQL
  • 云存储
  • 云密管服务
  • 云调度

  • 我已将 Sidekiq 配置为在后台执行一些作业,但是当我尝试执行 Sidekiq 作业时,我在 Cloud Run 日志中收到此错误。
    Redis::CannotConnectError (Error connecting to Redis on 127.0.0.1:6379 (Errno::ECONNREFUSED)):

    我让你在这里我的部署设置:

    cloudbuild.yaml
    steps:

    # Decrypt Rails Master key file
    - name: gcr.io/cloud-builders/gcloud
    args: ["kms", "decrypt", "--ciphertext-file=./config/master.key.enc",
    "--plaintext-file=./config/master.key",
    "--location=us-central1","--keyring=project_name",
    "--key=rails_master_key"]

    # Decrypt Whale on Rails service account credentials
    - name: gcr.io/cloud-builders/gcloud
    args: ["kms", "decrypt", "--ciphertext-file=./config/project_name_runner.key.enc",
    "--plaintext-file=./config/project_name_runner.key",
    "--location=us-central1","--keyring=project_name",
    "--key=project_name_runner_key"]

    # Build image with tag 'latest' and pass decrypted Rails DB password as argument
    - name: 'gcr.io/cloud-builders/docker'
    args: [ 'build',
    '--tag', 'gcr.io/$PROJECT_ID/project_name:latest',
    '--build-arg', 'DB_PWD',
    '--build-arg', 'RUBY_VERSION=${_RUBY_VERSION}',
    '--build-arg', 'PG_MAJOR=${_PG_MAJOR}',
    '--build-arg', 'NODE_MAJOR=${_NODE_MAJOR}',
    '--build-arg', 'BUNDLER_VERSION=${_BUNDLER_VERSION}',
    '--build-arg', 'RAILS_ENV=${_RAILS_ENV}',
    '--build-arg', 'REDIS_URL=${_REDIS_URL}',
    '--build-arg', 'DATABASE_HOST=${_DATABASE_HOST}',
    '--build-arg', 'DATABASE_USER=${_DATABASE_USER}',
    '--build-arg', 'DATABASE_NAME=${_DATABASE_NAME}',
    '.'
    ]
    secretEnv: ['DB_PWD']

    # Push new image to Google Cloud Registry
    - name: 'gcr.io/cloud-builders/docker'
    args: ['push', 'gcr.io/$PROJECT_ID/project_name:latest']

    secrets:
    - kmsKeyName: projects/project_name/locations/us-central1/keyRings/project_name/cryptoKeys/db_pwd_key
    secretEnv:
    DB_PWD: "db_password"

    substitutions:
    _RUBY_VERSION: '2.7.0'
    _PG_MAJOR: '11'
    _NODE_MAJOR: '12'
    _BUNDLER_VERSION: '2.1.2'
    _RAILS_ENV: production
    _REDIS_URL: redis://redis:6379/
    _DATABASE_HOST: /cloudsql/project_name:us-central1:project_name-production
    _DATABASE_USER: production_user
    _DATABASE_NAME: project_name-production

    入口点.sh

    #!/usr/bin/env bash

    cd /usr/src/app

    # Create the Rails production DB on first run
    bundle exec rake db:prepare

    # Do some protective cleanup
    > log/production.log
    rm -f tmp/pids/server.pid

    # Run the web service on container startup
    # $PORT is provided as an environment variable by Cloud Run
    bundle exec rails server -b 0.0.0.0 -p $PORT

    # Run sidekiq in production
    bundle exec sidekiq -C config/sidekiq.yml

    Dockerfile
    # Leverage the official Ruby image from Docker Hub
    # https://hub.docker.com/_/ruby
    ARG RUBY_VERSION
    FROM ruby:$RUBY_VERSION

    LABEL maintainer="myemail@gmail.com"

    ARG PG_MAJOR
    ARG NODE_MAJOR
    ARG BUNDLER_VERSION
    ARG RAILS_ENV

    # Add PostgreSQL to sources list
    RUN curl -sSL https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add - \
    && echo 'deb http://apt.postgresql.org/pub/repos/apt/ stretch-pgdg main' $PG_MAJOR > /etc/apt/sources.list.d/pgdg.list

    # Install recent versions of nodejs (10.x) and yarn pkg manager
    # Needed to properly pre-compile Rails assets
    RUN (curl -sL https://deb.nodesource.com/setup_$NODE_MAJOR.x | bash -) && apt-get update && apt-get install -y nodejs

    # Add Yarn to the sources list
    RUN (curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add -) && \
    echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list && \
    apt-get update && apt-get install -y yarn

    # Install production dependencies (Gems installation in
    # local vendor directory)
    WORKDIR /usr/src/app
    COPY Gemfile Gemfile.lock ./
    ENV BUNDLE_FROZEN=true
    RUN gem update --system && \
    gem install bundler:$BUNDLER_VERSION
    RUN bundle install

    # Copy application code to the container image.
    # Note: files listed in .gitignore are not copied
    # (e.g.secret files)
    COPY . .

    # Pre-compile Rails assets (master key needed)
    RUN yarn install
    RUN RAILS_ENV=production bundle exec rake assets:precompile

    # Set Google App Credentials environment variable with Service Account
    ENV GOOGLE_APPLICATION_CREDENTIALS=/usr/src/app/config/sunne_cms_api_runner.key

    # Setup Rails DB password passed on docker command line (see Cloud Build file)
    ARG DATABASE_NAME
    ARG DATABASE_HOST
    ARG DATABASE_USER
    ARG DB_PWD
    ENV DATABASE_PASSWORD=${DB_PWD}
    ENV DATABASE_NAME=${DATABASE_NAME}
    ENV DATABASE_HOST=${DATABASE_HOST}
    ENV DATABASE_USER=${DATABASE_USER}

    # Setting up Rails environment
    ENV RAILS_ENV=${RAILS_ENV}

    # For now we don't have a Nginx/Apache frontend so tell
    # the Puma HTTP server to serve static content
    # (e.g. CSS and Javascript files)
    ENV RAILS_SERVE_STATIC_FILES=true

    # Redirect Rails log to STDOUT for Cloud Run to capture
    ENV RAILS_LOG_TO_STDOUT=true

    # Designate the initial sript to run on container startup
    RUN chmod +x /usr/src/app/entrypoint.sh
    ENTRYPOINT ["/usr/src/app/entrypoint.sh"]

    最佳答案

    John Hanley提到,Cloud Run 不支持后台处理。不过,根据您的要求,您有几个选择。

    选项1

    使用单独的服务来运行您的后台作业,可能使用 Cloud Pub/Sub作业准备好时广播的服务到服务消息订阅。您的 Web 服务可以向订阅返回句柄/ID,调用者可以使用它来监听更新。

    如果合适,后台服务本身可以在其自己的 Cloud Run 容器中运行,或者由于您似乎在使用 NodeJS,您可以将其打包为 Cloud Function .

    选项 2

    将您的容器移至支持后台处理的解决方案,例如 App Engine 或 Kubernetes Engine (GKE)。然而,这两者与 Cloud Run 有非常不同的定价模型,并且根据您的使用模式,最终可能会变得更加昂贵。 (此 Google blog post 分解了 GKE 和 Cloud Run 之间的差异。)

    GKE 可以处理单容器设置,而无需您直接学习或弄乱 Kubernetes。但是,如果您的项目架构发生变化,或者您需要进行任何配置或故障排除,则可能会涉及大量的学习曲线。

    选项 3

    改写代码以使用同步处理,保持请求直到作业完成。这只有在您的工作肯定总是在超时(60 秒?)之前完成并且您的最终用例允许的情况下才可行。这是最简单的解决方案,但限制性最强且容易出现面向用户的错误。

    关于ruby-on-rails - 在 Google Cloud Run 中使用 Sidekiq 的 Rails 作业不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60297681/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com