0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

gem wheneverをdockerで動かす方法

Last updated at Posted at 2021-01-22

環境

Docker Engine 20.10.2
Ruby 2.7.0
Bundler 2.1.4

手順

wheneverをGemfileへ

# Gemfile

source 'https://rubygems.org'
git_source(:github) do |repo| "https://github.com/#{repo}.git" end

gem 'whenever'

コンテナにcronをインストール

# Dockerfile

FROM ruby:2.7.0

RUN apt-get update && apt-get install -y cron && apt-get install -y vim

WORKDIR /usr/src/app
COPY Gemfile .
COPY Gemfile.lock .
RUN bundle install

コンテナのsetupとwheneverの設定

$ docker build -t test_image .
$ docker create --name="test_container" -it -v "$PWD":/var/src/app test_image
$ docker start test_container
$ docker exec -it test_container /bin/bash
/usr/src/app# bundle exec wheneverize .

以下の設定ファイルができるのでカスタマイズする

# schedule.rb

#
# Use this file to easily define all of your cron jobs.
#
# It's helpful, but not entirely necessary to understand cron before proceeding.
# http://en.wikipedia.org/wiki/Cron

# Example:
#
set :output, "log/cron_log.log"
#
every 1.hours do
  # command
end
#
# every 4.days do
#   runner "AnotherModel.prune_old_records"
# end

# Learn more: http://github.com/javan/whenever

もう一度コンテナに入りコマンドを実行


$ docker exec -it test_container /bin/bash
/usr/src/app# bundle exec whenever
# =>
# 0 0,2,4,6,8,10,12,14,16,18,20,22 * * * /bin/bash -l -c '/usr/bin/some_great_command >> /path/to/my/cron_log.log 2>&1'
# 0 0,2,4,6,8,10,12,14,16,18,20,22 * * * /bin/bash -l -c 'cd /usr/src/app && bundle exec script/runner -e production '\''MyModel.some_method'\'' >> /path/to/my/cron_log.log 2>&1'
# 0 0,2,4,6,8,10,12,14,16,18,20,22 * * * /bin/bash -l -c 'cd /usr/src/app && RAILS_ENV=production bundle exec rake some:great:rake:task --silent >> /path/to/my/cron_log.log 2>&1'
# 0 0 1,5,9,13,17,21,25,29 * * /bin/bash -l -c 'cd /usr/src/app && bundle exec script/runner -e production '\''AnotherModel.prune_old_records'\'' >> /path/to/my/cron_log.log 2>&1'

cronを起動

/usr/src/app# service cron start
#=> [ ok ] Starting periodic command scheduler: cron.

cronが動いているか確認

/usr/src/app# service cron status
#=> [ ok ] cron is running.

参考

gem whenever

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?