[RUBY] [Rails] Implementation of batch processing using whenever (gem)

Goal

Delete a certain number of books posted every 3 minutes.

Development environment

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

Implementation

1. Add Gem

Gemfile


gem 'whenever', require: false

Terminal


$ bundle install
$ bundle exec wheneverize //config/schedule.rb file generated

2. Create " data_reset.rb "

app/lib/batch/data_reset.rb


class Batch::DataReset
  def self.data_reset
    Book.delete_all  #Delete books in database
  end
end

◎ If you want to specify the id, write the following Book.where.not (id: 1..30) .delete_all: Delete all except id1 ~ 30

◎ If you want to delete the data after the creation date (after December 1st), enter the following Book.where("books.created_at < ?", "2020-12-1").delete_all

3. Edit "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 #Add here
    # 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

Modify the above file to enable " data_reset.rb "

4. Confirmation

Terminal


bundle exec rails runner Batch::DataReset.data_reset //data_Check if reset is done

Terminal


Running via Spring preloader in process [Process ID] //If it appears like this, it is a success

5. Edit " schedule.rb "

config/schedule.rb


:
:
require File.expand_path(File.dirname(__FILE__) + "/environment")
rails_env = Rails.env.to_sym
set :environment, rails_env  #Specify a relative path from an absolute path
set :output, 'log/cron.log' #Set the log output destination file
every 3.minute do
  begin
    runner "Batch::DataReset.data_reset"
  rescue => e
    Rails.logger.error("aborted rails runner")
    raise e
  end

4. Reflect " cron "

In whenever, the contents described in config/schedule.rb are reflected in crontab.

Terminal


$ bundle exec whenever --update-crontab

If you see the following, you are successful

Terminal


[write] crontab file updated

Other batch processing commands

crontab -l ➡︎ Check if it is actually reflected in crontab If the following appears, it works

Terminal


# Begin Whenever generated tasks for: /home/vagrant/work/app name/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/app name&& bundle exec bin/rails runner -e [Development environment] '\''Batch::DataReset.data_reset'\'' >> log/cron.log 2>&1'
# End Whenever generated tasks for: /home/vagrant/work/app name/config/schedule.rb at: 2020-05-05 03:29:06 +0000

$ sudo systemctl start crond ➡︎ cron start

$ sudo systemctl stop crond ➡︎ Stop cron

$ bundle exec whenever --clear-crontab ➡︎ Remove cron

Recommended Posts

[Rails] Implementation of batch processing using whenever (gem)
[Rails] Implementation of coupon function (with automatic deletion function using batch processing)
[Rails] Regular batch processing by whenever
[Rails] Implementation of tagging function using intermediate table (without Gem)
[Rails] Implementation of search function using gem's ransack
[Rails 6] Implementation of inquiry function using Action Mailer
[Rails] Implementation of image enlargement function using lightbox2
[Rails] Implementation of PV number ranking using impressionist
[Rails] Implementation of image slide show using Bootstrap 3
[Rails] Implementation of multi-layer category function using ancestry "Preparation"
[Rails] Implementation of multi-layer category function using ancestry "seed"
Rails implementation of ajax removal
[Rails 6] Implementation of search function
[Rails] Implementation of category function
[Rails] Implementation of tutorial function
[Rails] Implementation of like function
[Rails] Implementation of multi-layer category function using ancestry "Editing form"
[Rails] Implementation of automatic address input using jpostal and jp_prefecture
[Rails] Implementation of user logic deletion
[Rails] Implementation of CSV import function
[Rails] Asynchronous implementation of like function
[Rails] Implementation of image preview function
[Rails] About implementation of like function
[Rails] Implementation of user withdrawal function
[Rails] Implementation of CSV export function
Implementation of asynchronous processing in Tomcat
Implementation of validation using regular expressions
[Rails] Implementation of many-to-many category functions
[Rails] gem ancestry category function implementation
[Kotlin] Example of processing using Enum
[Rails] Implementation of new registration function in wizard format using devise
[Rails] Implementation of tag function using acts-as-taggable-on and tag input completion function using tag-it
[Rails] Manage multiple models using devise gem
Visualize Rails server processing time using Server Timing
[Android] Implementation of side-scrolling ListView using RecyclerView
Implementation of user authentication function using devise (2)
Implementation of multi-tenant asynchronous processing in Tomcat
[Rails 6.0] About batch saving of multiple records
[Rails] I will explain the implementation procedure of the follow function using form_with.
Implementation of user authentication function using devise (1)
Rails [For beginners] Implementation of comment function
[Rails 6] Implementation of SNS (Twitter) sharing function
Implementation of user authentication function using devise (3)
Batch implementation in RubyOnRails environment using Digdag
[Vue.js] Implementation of menu function Implementation version rails6
[Ruby on rails] Implementation of like function
[Rails] Implementation of validation that maintains uniqueness
[Vue.js] Implementation of menu function Vue.js introduction rails6
[Rails] Set validation for the search function using Rakuten API (from the implementation of Rakuten API)
Implement share button in Rails 6 without using Gem
[FCM] Implementation of message transmission using FCM + Spring boot
[Rails] Display of multi-layered data using select boxes
Story of implementing login function using gem sorcery
[Rails] Schedule management using Full Calendar Yesterday's implementation
[Rails] Implementation of retweet function in SNS application
[Rails] Implementation of "notify notification in some way"
[Java / Spring Boot] Spring security ⑤ --Implementation of logout processing
[Rails] Creating a breadcrumb trail using Gem gretel
[Note] Summary of rails login function using devise ①
Implementation of GKAccessPoint
Implementation of Google Sign-In using Google OAuth 2.0 authentication (server edition)