首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Rails每当gem不使用Ubuntu和Docker组合执行重复crontab任务时

Rails每当gem不使用Ubuntu和Docker组合执行重复crontab任务时
EN

Stack Overflow用户
提问于 2021-04-24 13:18:33
回答 1查看 1.3K关注 0票数 1

我试图运行一个重复的任务,使用我的rails应用程序的何时创业板。它运行在使用Docker创建的Docker容器中,托管在Ubuntu服务器上。

我可以使用Whenever成功地创建和更新crontab文件,但它似乎没有执行任务。

当应用程序运行时,我想在后台重复执行的任务是:

代码语言:javascript
复制
rake searchkick:reindex CLASS=Property

我通常在使用‘docker-组合运行web bash’创建一个web运行容器之后,从终端成功地执行这个命令。这将重新索引我的ElasticSearch服务器,使用searchkick。

有关版本如下:

代码语言:javascript
复制
- 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)

Cron是使用Dockerfile安装的:

代码语言:javascript
复制
RUN apt-get update -qq && \
    apt-get install -y curl \
    build-essential \
    libpq-dev \
    cron \
    vim \
    nano \
    postgresql \
    postgresql-contrib \
    postgresql-client

创建容器时,Dockerfile运行脚本:

代码语言:javascript
复制
COPY entrypoint.sh /usr/bin/
RUN chmod +x /usr/bin/entrypoint.sh
ENTRYPOINT ["entrypoint.sh"]
EXPOSE 3000

脚本(entrypoint.sh)创建一个crontab文件。

代码语言:javascript
复制
#!/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 exec file set 'environment=production‘--update- crontab’命令按照下面的schedule.rb文件更新crontab文件:

代码语言:javascript
复制
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

我也试过这样做:

代码语言:javascript
复制
every 3.minutes do
    command "cd /myapp && rake searchkick:reindex CLASS=Property"
end

当我执行使用rails终端的‘docker-组合运行web bash’时,我在最后看到这个响应:

代码语言:javascript
复制
no crontab for root
[write] crontab file updated

为了检查创建是否正确,我使用“crontab -l”查看rails应用程序中的crontab文件,它显示:

代码语言:javascript
复制
# 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/cronlog.log中的日志显示如下:

代码语言:javascript
复制
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。我要检查一下,这是否是邦德勒创业板版本的问题,而不是宝石文件中的问题。

感谢你的帮助。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-05-09 07:52:52

可以在entrypoint.sh中运行cron来启动cron服务(linux)。

代码语言:javascript
复制
# Update crontab file using whenever command.
cron && bundle exec whenever --set 'environment=production' --update-crontab

schedule.rb

  1. 调度不知道gems路径,因此抛出Bundler::GemNotFound错误,为了解决这个问题,可以通过在中设置ENV来避免丢失路径

代码语言:javascript
复制
set :output, "log/cron_log.log"
ENV.each { |k, v| env(k, v) }
# ...
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67243203

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档