[Ruby] Learn Digdag from the official Digdag documentation-Language API-Ruby

1 minute read

Target

Digdag official website documentation Operators translation + α The final goal is to make a batch in Rails using Digdag’s Ruby http://docs.digdag.io/operators/scripting.html http://docs.digdag.io/operators/scripting.html

table of contents

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

Language API-Ruby

Programmable workflow in Ruby

``

workflow1.dig
_export:
  rb:
    require:'tasks/my_workflow'

+step1:
  rb>: MyWorkflow.step1

+step2:
  rb>: MyWorkflow.step2

``

my_workflow.rb
class MyWorkflow
  def step1
    puts "ruby step1"
  end

  def step2
    puts "ruby step2"
  end
end

result


$ digdag run workflow1.dig --rerun
2020-07-12 17:37:09 +0900 [INFO](0017@[0:default]+workflow1+step1): rb>: MyWorkflow.step1
ruby step1
2020-07-12 17:37:10 +0900 [INFO](0017@[0:default]+workflow1+step2): rb>: MyWorkflow.step2
ruby step2

Defining variables

Save 1 in my_value in step1 and output the saved variable in step2

``


class MyWorkflow
  def step1
    Digdag.env.store(my_value: 1)
  end

  def step2
    puts "step2: %s" %Digdag.env.params['my_value']
  end
end

Method argument mapping

The variable set in step1 can be received as a parameter of the function in step2

``

my_workflow.rb
class MyWorkflow
  def step1
    Digdag.env.store(my_value1: 1)
    Digdag.env.store(my_value2: 2)
  end

  def step2(my_value1: 0, my_value2: 0)
    puts "my_value1: #{my_value1} my_value2: #{my_value2} "
  end
end

result


$ digdag run workflow1.dig --rerun
2020-07-12 17:47:41 +0900 [INFO](0017@[0:default]+workflow1+step1): rb>: MyWorkflow.step1
2020-07-12 17:47:42 +0900 [INFO](0017@[0:default]+workflow1+step2): rb>: MyWorkflow.step2
my_value1: 1 my_value2: 2

Generating child tasks

You can generate a serve task in Ruby using Digdag.env.add_subtask.

``

my_workflow.rb
class MyWorkflow
  def step1
    puts "step1 start"
    Digdag.env.add_subtask(MyWorkflow, :step3, arg1: 3)
    Digdag.env.store(my_value: 2)
  end

  def step2(my_value: "default")
    puts "step2: %s" %my_value
  end

  def step3(arg1:)
    puts "step3: %s" %arg1
  end
end

As you can see from the result, when step1 is executed, subtask step3 added in step1 is executed.

result


$ digdag run workflow1.dig --rerun
2020-07-12 17:56:27 +0900 [INFO](0017@[0:default]+workflow1+step1): rb>: MyWorkflow.step1
step1 start
2020-07-12 17:56:27 +0900 [INFO](0017@[0:default]+workflow1+step1^sub+subtask0): rb>: ::MyWorkflow.step3
step3: 3
2020-07-12 17:56:28 +0900 [INFO](0017@[0:default]+workflow1+step2): rb>: MyWorkflow.step2
step2: 2

This completes the description of how Digdag works with Ruby. Next time I will make a batch in Ruby and run it on Digdag.