Rails,无论何时和泊坞窗-cron任务均不会运行 [英] rails, whenever and docker - cron tasks doesn't run

查看:96
本文介绍了Rails,无论何时和泊坞窗-cron任务均不会运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我来自 schedule.rb 的crontasks在docker容器上不起作用,但是 crontab -l <​​/ code>结果已经包含这些行:

My crontasks from schedule.rb doesn't work on docker container, but crontab -l result already contains this lines:

# Begin Whenever generated tasks for: /app/config/schedule.rb
45 19 * * * /bin/bash -l -c 'bundle exec rake stats:cleanup'
45 19 * * * /bin/bash -l -c 'bundle exec rake stats:count'
0 5 * * * /bin/bash -l -c 'bundle exec rake stats:history'
# End Whenever generated tasks for: /app/config/schedule.rb

我可以在容器中手动运行此命令,并且可以运行。

I can run this commands manually in container and it works. It seems like cron doesn't start.

Dockerfile:

Dockerfile:

FROM ruby:2.4.0-slim
RUN apt-get update
RUN apt-get install -qq -y --no-install-recommends build-essential libpq-dev cron postgresql-client
RUN cp /usr/share/zoneinfo/Europe/Moscow /etc/localtime
ENV LANG C.UTF-8
ENV RAILS_ENV production
ENV INSTALL_PATH /app
RUN mkdir $INSTALL_PATH
RUN touch /log/cron.log
ADD Gemfile Gemfile.lock ./
WORKDIR $INSTALL_PATH
RUN bundle install --binstubs --without development test
COPY . .
RUN bundle exec whenever --update-crontab
RUN service cron start
ENTRYPOINT ["bundle", "exec", "puma"]


推荐答案

在Dockerfile中,仅在构建映像时才执行RUN命令。

In a Dockerfile, the RUN command is only execute when building the image.

如果要在启动容器时启动cron,则应在 CMD cron c>。我通过删除 RUN服务cron start 并更改了 ENTRYPOINT 来修改了Dockerfile。

If you want to start cron when you start your container, you should run cron in CMD. I modified your Dockerfile by removing RUN service cron start and changing your ENTRYPOINT.

FROM ruby:2.4.0-slim
RUN apt-get update
RUN apt-get install -qq -y --no-install-recommends build-essential libpq-dev cron postgresql-client
RUN cp /usr/share/zoneinfo/Europe/Moscow /etc/localtime
ENV LANG C.UTF-8
ENV RAILS_ENV production
ENV INSTALL_PATH /app
RUN mkdir $INSTALL_PATH
RUN touch /log/cron.log
ADD Gemfile Gemfile.lock ./
WORKDIR $INSTALL_PATH
RUN bundle install --binstubs --without development test
COPY . .
RUN bundle exec whenever --update-crontab
CMD cron && bundle exec puma

减少图像所具有的层数是一种最佳做法,例如,应该始终在同一条RUN语句中将RUN apt-get update与apt-get install组合在一起,并在执行以下操作后清理apt文件: rm -rf / var / lib / apt / lists / *

It's a best practice to reduce the number of layer an image have, for example, you should always combine RUN apt-get update with apt-get install in the same RUN statement, and clean apt files after: rm -rf /var/lib/apt/lists/*

FROM ruby:2.4.0-slim
RUN apt-get update && \
  apt-get install -qq -y --no-install-recommends build-essential libpq-dev cron postgresql-client \
  rm -rf /var/lib/apt/lists/* && \
  cp /usr/share/zoneinfo/Europe/Moscow /etc/localtime
ENV LANG C.UTF-8
ENV RAILS_ENV production
ENV INSTALL_PATH /app
RUN mkdir $INSTALL_PATH && \
  touch /log/cron.log
ADD Gemfile Gemfile.lock ./
WORKDIR $INSTALL_PATH
RUN bundle install --binstubs --without development test
COPY . .
RUN bundle exec whenever --update-crontab
CMD cron && bundle exec puma

这篇关于Rails,无论何时和泊坞窗-cron任务均不会运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆