簡體   English   中英

Rails 每當 gem 不使用 Ubuntu 和 Docker 執行重復的 crontab 任務時

[英]Rails Whenever gem not executing repetitive crontab task with Ubuntu and Docker Compose

我正在嘗試使用我的 rails 應用程序的 Each gem 運行重復性任務。 它在使用 Docker Compose 創建並托管在 Ubuntu 服務器上的 Docker 容器中運行。

我可以使用 When gem 成功創建和更新 crontab 文件,但它似乎沒有執行任務。

我想在應用程序運行時在后台重復執行的任務是:

rake searchkick:reindex CLASS=Property

我通常在使用“docker-compose run web bash”創建 web 運行容器后從終端成功執行此命令。 這使用 searchkick gem 重新索引了我的 ElasticSearch 服務器。

相關版本如下:

- ruby 2.7.2p137
- rails (6.0.3.6)
- elasticsearch (6.8.3)
  - elasticsearch-api (= 6.8.3)
  - elasticsearch-transport (= 6.8.3)
- searchkick (4.4.4)
  - activemodel (>= 5)
  - elasticsearch (>= 6)
  - hashie
- whenever (1.0.0)
  - chronic (>= 0.6.3)

使用 Dockerfile 安裝 Cron:

RUN apt-get update -qq && \
    apt-get install -y curl \
    build-essential \
    libpq-dev \
    cron \
    vim \
    nano \
    postgresql \
    postgresql-contrib \
    postgresql-client

Dockerfile 在創建容器時運行腳本:

COPY entrypoint.sh /usr/bin/
RUN chmod +x /usr/bin/entrypoint.sh
ENTRYPOINT ["entrypoint.sh"]
EXPOSE 3000

腳本 (entrypoint.sh) 創建一個 crontab 文件。

#!/bin/bash
set -e

# For development check if the gems as installed, if not, then uninstall them.
if ! [ bundle check ]; then
    bundle install
fi

# Remove a potentially pre-existing server.pid for Rails.
rm -f /myapp/tmp/pids/server.pid

# Yarn - Check Files.
yarn install --check-files

# Create empty crontab file.
crontab -l | { cat; echo ""; } | crontab -

# Update crontab file using whenever command.
bundle exec whenever --set 'environment=production' --update-crontab

# Run the command - runs any arguments passed into this entrypoint file.
exec "$@"

上面的 'bundle execwhen --set 'environment=production' --update-crontab' 命令根據下面的 schedule.rb 文件更新 crontab 文件:

set :output, "log/cron_log.log"
env :PATH, ENV['PATH']

# Reindex Property records every 3 mins.
every 3.minutes do
    rake "searchkick:reindex CLASS=Property"
end

我也試過用這個:

every 3.minutes do
    command "cd /myapp && rake searchkick:reindex CLASS=Property"
end

當我執行 'docker-compose run web bash' 來使用 rails 終端時,我在最后看到了這個響應:

no crontab for root
[write] crontab file updated

為了檢查它是否正確創建,我在 rails 應用程序中使用“crontab -l”查看 crontab 文件,它顯示:

# Begin Whenever generated tasks for: /myapp/config/schedule.rb at: 2021-04-24 13:07:12 +0000
PATH=/usr/local/bundle/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

0,3,6,9,12,15,18,21,24,27,30,33,36,39,42,45,48,51,54,57 * * * * /bin/bash -l -c 'cd /myapp && RAILS_ENV=production bundle exec rake searchkick:reindex CLASS=Property --silent >> log/cron_log.log 2>&1'

# End Whenever generated tasks for: /myapp/config/schedule.rb at: 2021-04-24 13:07:12 +0000

更新:使用cron service statuscron service start后,myApp/log/cron_log.log 中的日志顯示如下:

bundler: failed to load command: rake (/usr/local/bin/rake)
Bundler::GemNotFound: Could not find rake-13.0.3 in any of the sources
  /usr/local/lib/ruby/2.7.0/bundler/spec_set.rb:86:in `block in materialize'
  /usr/local/lib/ruby/2.7.0/bundler/spec_set.rb:80:in `map!'
  /usr/local/lib/ruby/2.7.0/bundler/spec_set.rb:80:in `materialize'
  /usr/local/lib/ruby/2.7.0/bundler/definition.rb:170:in `specs'
  /usr/local/lib/ruby/2.7.0/bundler/definition.rb:237:in `specs_for'
  /usr/local/lib/ruby/2.7.0/bundler/definition.rb:226:in `requested_specs'
  /usr/local/lib/ruby/2.7.0/bundler/runtime.rb:101:in `block in definition_method'
  /usr/local/lib/ruby/2.7.0/bundler/runtime.rb:20:in `setup'
  /usr/local/lib/ruby/2.7.0/bundler.rb:149:in `setup'
  /usr/local/lib/ruby/2.7.0/bundler/setup.rb:20:in `block in <top (required)>'
  /usr/local/lib/ruby/2.7.0/bundler/ui/shell.rb:136:in `with_level'
  /usr/local/lib/ruby/2.7.0/bundler/ui/shell.rb:88:in `silence'
  /usr/local/lib/ruby/2.7.0/bundler/setup.rb:20:in `<top (required)>'
bundler: failed to load command: rake (/usr/local/bin/rake)
Bundler::GemNotFound: Could not find rake-13.0.3 in any of the sources
  /usr/local/lib/ruby/2.7.0/bundler/spec_set.rb:86:in `block in materialize'
  /usr/local/lib/ruby/2.7.0/bundler/spec_set.rb:80:in `map!'
  /usr/local/lib/ruby/2.7.0/bundler/spec_set.rb:80:in `materialize'
  /usr/local/lib/ruby/2.7.0/bundler/definition.rb:170:in `specs'
  /usr/local/lib/ruby/2.7.0/bundler/definition.rb:237:in `specs_for'
  /usr/local/lib/ruby/2.7.0/bundler/definition.rb:226:in `requested_specs'
  /usr/local/lib/ruby/2.7.0/bundler/runtime.rb:101:in `block in definition_method'
  /usr/local/lib/ruby/2.7.0/bundler/runtime.rb:20:in `setup'
  /usr/local/lib/ruby/2.7.0/bundler.rb:149:in `setup'
  /usr/local/lib/ruby/2.7.0/bundler/setup.rb:20:in `block in <top (required)>'
  /usr/local/lib/ruby/2.7.0/bundler/ui/shell.rb:136:in `with_level'
  /usr/local/lib/ruby/2.7.0/bundler/ui/shell.rb:88:in `silence'
  /usr/local/lib/ruby/2.7.0/bundler/setup.rb:20:in `<top (required)>'

所以它似乎已經設置了 crontab 文件並且 cronjob 正在嘗試運行,但它沒有找到 rake-13.0.3。 我將檢查這是否是捆綁器 gem 版本與 gemfile 中的問題。

感謝任何幫助。

  1. 您可以在entrypoint.sh中運行cron來啟動 cron 服務(linux)
# Update crontab file using whenever command.
cron && bundle exec whenever --set 'environment=production' --update-crontab
  1. schedule 不知道 gems 路徑,所以會拋出Bundler::GemNotFound錯誤,要解決這個問題,您可以通過在schedule.rb中設置所有 ENV 來避免丟失路徑
set :output, "log/cron_log.log"
ENV.each { |k, v| env(k, v) }
# ...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM