Rails Google Cloud 运行 中使用 Sidekiq 的作业无法正常工作

Rails jobs with Sidekiq in Google Cloud Run not working

我在 Google 云 运行 中有一个可用的 Rails 应用程序,使用:

我已将 Sidekiq 配置为在后台执行一些作业,但是当我尝试执行 Sidekiq 作业时,云 运行 日志中出现此错误。

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

entrypoint.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"]

Cloud 运行 不支持后台作业。您的容器以 HTTP 请求开始,并在请求 returns 时结束。在请求 returns 之后不要期望更多。

Cloud 运行 不是操作系统、任务调度程序、后台线程处理器等

阅读本文以了解云 运行 can/cannot 的作用:

Cloud Run Container Contract

所述,云 运行 不支持后台处理。不过,根据您的要求,您有几个选择。

选项 1

使用单独的服务来 运行 您的后台作业,可能会使用 Cloud Pub/Sub 服务到服务消息订阅在作业准备就绪时进行广播。您的 Web 服务可以 return 一个 handle/ID 订阅,调用者可以使用它来侦听更新。

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

选项 2

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

GKE 可以处理单容器设置,无需您直接学习或摆弄 Kubernetes。但是,如果您的项目架构发生变化,或者您需要进行任何配置或故障排除,则可能需要很长的学习时间。

选项 3

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