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

Digdag公式ドキュメントからDigdagを学ぶ-Language API-Ruby

Last updated at Posted at 2020-07-14

目標

Digdagの公式サイトのドキュメントのOperatorsの翻訳+α
DigdagのRubyを使ってRailsにバッチを作るまでが最後の目標
http://docs.digdag.io/operators/scripting.htmlhttp://docs.digdag.io/operators/scripting.html

目次

Getting started
Architecture
Concepts
Workflow definition
Scheduling workflow
Operators
Command reference
Language API -Ruby
Digdagで環境毎に設定値を変える(RubyOnRails)
Digdagを用いてRubyOnRails環境でバッチ実装

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
結果
$ 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

step1でmy_valueに1を保存して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

step1で設定した変数は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
結果
$ 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

Digdag.env.add_subtaskを使って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

結果を見るとわかると思いますがstep1が実行される時、step1で追加したサブタスクstep3が実行される。

結果
$ 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

これでDigdagとRubyとの連携についての説明は完了です。
次回からRubyでバッチを作ってDigdag上で実行してみたいと思います。

2
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
2
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?