Learn Digdag from Digdag official documentation-Language API-Ruby

Target

Translation of Operators of the document of Digdag's official website + α The final goal is to create a batch in Rails using Digdag's Ruby http://docs.digdag.io/operators/scripting.htmlhttp://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]([email protected][0:default]+workflow1+step1): rb>: MyWorkflow.step1
ruby step1
2020-07-12 17:37:10 +0900 [INFO]([email protected][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

python


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 Variables set in step1 can be received as function parameters 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]([email protected][0:default]+workflow1+step1): rb>: MyWorkflow.step1
2020-07-12 17:47:42 +0900 [INFO]([email protected][0:default]+workflow1+step2): rb>: MyWorkflow.step2
my_value1: 1 my_value2: 2 

Generating child tasks You can use Digdag.env.add_subtask to generate serve tasks in Ruby.

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, the subtask step3 added in step1 is executed.

result


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

This completes the explanation of how Digdag works with Ruby. From the next time, I would like to make a batch in Ruby and execute it on Digdag.

Recommended Posts

Learn Digdag from Digdag official documentation-Language API-Ruby
Learn Digdag from Digdag Official Documentation-Ah Concepts
Learn Digdag from Digdag Official Documentation-Getting started
Learn Digdag from Digdag Official Documentation-Scheduling workflow
Learn Digdag from Digdag official documentation-Language API-Ruby
Learn Digdag from Digdag Official Documentation-Ah Concepts
Learn Digdag from Digdag Official Documentation-Scheduling workflow
Learn Digdag from Digdag official documentation-Language API-Ruby
Learn Digdag from Digdag Official Documentation-Ah Concepts
Learn Digdag from Digdag Official Documentation-Getting started
Learn Digdag from Digdag Official Documentation-Scheduling workflow
Learn Digdag from Digdag official documentation-Language API-Ruby
Learn Digdag from Digdag Official Documentation-Ah Concepts