[Ruby] Batch implementation in RubyOnRails environment using Digdag

1 minute read

About #Digdag Getting started Architecture Concepts Workflow definition Scheduling workflow Operators Command reference Language API -Ruby Change the setting value for each environment with Digdag (RubyOnRails) Batch implementation in RubyOnRails environment using Digdag

Preparation

Ruby on Rails Tutorial Sample Application Download https://github.com/yasslab/sample_app

Run the Ruby on Rails Tutorial sample application to register users and Post. I created a test user and registered 4 posts. ![Screenshot 2020-07-20 19.01.38.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/108475/2b8af674-0f51-de83-2727-(e9969b81429e.png)

This time, I will make a simple batch that outputs the number of posts by the corresponding user with the user name as a parameter. There are two ways to implement/start batch.

Let’s try the following two methods that are often used when writing batch processing in Rails. ① rails runner: Write batch as script ② rake task: Write a batch as a build task

Overview

batch-2.png

Run with #rails runner I used OptionParser for parameter acquisition.

Add batch processing script

Add batch processing script under /lib/scripts/

``

lib/scripts/post_batch.rb
require'optparse'

module Scripts
  class PostBatch
    def initialize
      @option = {}
      OptionParser.new do |opt|
        opt.on('-n VALUE','user name') {|v| @option[:name] = v}
        opt.parse!(ARGV)
      end
    end

    def count
      user = User.find_by(name: @option[:name])
      puts "ID: #{user.id} Name: #{user.name}"
      puts "Posts: #{Micropost.where(user_id: user.id).count}"
    end
  end
end

Automatically load the ruby file under lib

config/application.rb


config.autoload_paths += %W(#{config.root}/lib)

Add rails_runner.dig under workflows and add the following contents

``

rails_runner.dig
+task:
  sh>: bundle exe rails runner Scripts::PostBatch.new.count -n'test'

execute

The ID, name, and number of posts of the test user are output.

``

 execution result
$ digdag run rails_runner.dig --rerun
2020-07-20 19:38:39 +0900 [INFO](0017@[0:default]+rails_runner+task): sh>: bundle exec rails runner Scripts::PostBatch.count'test'
ID: 5 Name: test
Posts: 4

Run with #rake

rake task generation

``


$ rails g task task_post
Running via Spring preloader in process 4255
      create lib/tasks/tast_post.rake

A ```/lib/tasks/task_post.rake

file is generated, so open it and add the following source.





#### **``**
```rb
lib/tasks/task_post.rake
namespace :task_post do
  desc "Get user posts"
  task :count, ['name'] => :environment do |task, args|
    user = User.find_by(name: args.name)
    puts "ID: #{user.id} Name: #{args.name}"
    puts "Posts: #{Micropost.where(user_id: user.id).count}"
  end
end

Added Workflow

``

rake.dig
+task:
  sh>: bundle exec rake task_post:count[test4]

execute

The ID, name, and number of posts of the test user are output.

``

```rb execution result $ digdag run rake.dig –rerun 2020-07-20 20:04:16 +0900 INFO: sh>: bundle exec rake task_post:count[test] ID: 5 Name: test Posts: 4