我试图运行一个重复的任务,使用我的rails应用程序的何时创业板。它运行在使用Docker创建的Docker容器中,托管在Ubuntu服务器上。
我可以使用Whenever成功地创建和更新crontab文件,但它似乎没有执行任务。
当应用程序运行时,我想在后台重复执行的任务是:
rake searchkick:reindex CLASS=Property我通常在使用‘docker-组合运行web bash’创建一个web运行容器之后,从终端成功地执行这个命令。这将重新索引我的ElasticSearch服务器,使用searchkick。
有关版本如下:
- 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安装的:
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 exec file 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当我执行使用rails终端的‘docker-组合运行web bash’时,我在最后看到这个响应:
no crontab for root
[write] crontab file updated为了检查创建是否正确,我使用“crontab -l”查看rails应用程序中的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 status和cron service start之后,myApp/log/cronlog.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。我要检查一下,这是否是邦德勒创业板版本的问题,而不是宝石文件中的问题。
感谢你的帮助。
发布于 2021-05-09 07:52:52
可以在entrypoint.sh中运行cron来启动cron服务(linux)。
# Update crontab file using whenever command.
cron && bundle exec whenever --set 'environment=production' --update-crontabschedule.rb
Bundler::GemNotFound错误,为了解决这个问题,可以通过在中设置ENV来避免丢失路径set :output, "log/cron_log.log"
ENV.each { |k, v| env(k, v) }
# ...https://stackoverflow.com/questions/67243203
复制相似问题