0
1

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 5 years have passed since last update.

[Rails]whenever(gem)を用いたバッチ処理の実装

0
Posted at

目標

3分おきに投稿した本を一定数削除する。

開発環境

・Ruby: 2.5.7
・Rails: 5.2.4
・Vagrant: 2.2.7
・VirtualBox: 6.1
・OS: macOS Catalina

実装

1.Gemを追加

Gemfile
gem 'whenever', require: false
ターミナル
$ bundle install
$ bundle exec wheneverize //config/schedule.rbファイルが生成 

2.「data_reset.rb」を作成

app/lib/batch/data_reset.rb
class Batch::DataReset
  def self.data_reset
    Book.delete_all  #データベース内の本を削除
  end
end

◎idを指定したい場合は以下を記載
Book.where.not(id: 1..30).delete_all :id1~30以外は削除

◎作成日以降(12月1日以降)のデータを消したい時は以下を記載
Book.where("books.created_at < ?", "2020-12-1").delete_all

3.「config/application.rb」を編集

config/application.rb
  class Application < Rails::Application
    # Initialize configuration defaults for originally generated Rails version.
    config.paths.add 'lib', eager_load: true #ここを追加
    # Settings in config/environments/* take precedence over those specified here.
    # Application configuration can go into files in config/initializers
    # -- all .rb files in that directory are automatically loaded after loading
    # the framework and any gems in your application.
  end

「data_reset.rb」を有効にするために上記ファイルを変更

4.確認

ターミナル
bundle exec rails runner Batch::DataReset.data_reset //data_resetが行われるか確認
ターミナル
Running via Spring preloader in process [プロセスID] //このようにでたら成功です

5.「schedule.rb」を編集

config/schedule.rb
:
:
require File.expand_path(File.dirname(__FILE__) + "/environment")
rails_env = Rails.env.to_sym
set :environment, rails_env  # 絶対パスから相対パス指定
set :output, 'log/cron.log' # ログの出力先ファイルを設定
every 3.minute do
  begin
    runner "Batch::DataReset.data_reset"
  rescue => e
    Rails.logger.error("aborted rails runner")
    raise e
  end

4.「cron」を反映

wheneverではconfig/schedule.rbに記述した内容がcrontabに反映される仕組みになっています。

ターミナル
$ bundle exec whenever --update-crontab

以下がでていれば成功です

ターミナル
[write] crontab file updated

その他のバッチ処理コマンド

crontab -l ➡︎ 実際にcrontabに反映しているか確認
以下がでていれば動いてる

ターミナル
# Begin Whenever generated tasks for: /home/vagrant/work/アプリ名/config/schedule.rb at: 2020-05-05 03:29:06 +0000
0,2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48,50,52,54,56,58 * * * * /bin/bash -l -c 'cd /home/vagrant/work/アプリ名 && bundle exec bin/rails runner -e [開発環境] '\''Batch::DataReset.data_reset'\'' >> log/cron.log 2>&1'
# End Whenever generated tasks for: /home/vagrant/work/アプリ名/config/schedule.rb at: 2020-05-05 03:29:06 +0000

$ sudo systemctl start crond ➡︎ cron起動

$ sudo systemctl stop crond ➡︎ cronをストップ

$ bundle exec whenever --clear-crontab ➡︎ cronを削除

0
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?