LoginSignup
10
7

More than 5 years have passed since last update.

LINE Notify + Digdag Treasure Data社 の OSS ワークフローエンジン をやってみた

Last updated at Posted at 2016-10-14

LINE Notify + Digdag Treasure Data社 の OSS ワークフローエンジン をやってみた

0.はじめに

少し前から気になっていました Treasure Data社のOSS ワークフローエンジン Digdag (Jenkins2の軽量版のようなもの)の素振りも兼ねて

LINE Notify + Digdag をやってみました。

用途としては処理時間が長いワークフローやスケジュールでの定期実行の結果通知などで使えそうです。
別にLINEじゃなくても他にChatツールはありますが...

参考:LINE Engineers' Blog コマンドラインから LINE にメッセージを送れる LINE Notify

なお、今回紹介するサンプルコードは、Digdagのinit時に作成されるサンプルのdigファイルをベースに
ワークフローの開始時と、正常終了時 or エラー終了時に以下フォーマットで LINE Notify として通知します。

YYYY-MM-DD HH:mm:ss digdag ワークフロー名 [start|end|error]

line_notify.png

1.環境

CentOS release 6.8 (Final)
Digdag v0.8.17

2.使い方

Digdagがインストールされている環境で以下を実行します。
DigdagのインストールについてはDigdag公式サイト Getting started
を参照してください。

なお、コード一式についてはgithub上のこちらのrepoへあげていますので、
以下の通りgit clone するか、後述するサンプルコードを参照ください。
また、いずれの場合でも tasks配下のシェルへ実行権限付与chmod -R +x tasks/をしないとエラーとなりますので注意してください。

git clone https://github.com/tbuchi888/digdag_line_notify.git
cd digdag_line_notify
chmod -R +x tasks/

何度もrerunできるように-aオプションと、-pオプションでトークンを指定して実行します。

digdag run test.dig -a -p accese_token=YOUR_LINES_ACCESE_TOKEN

なお、トークンを-pオプションではなく、digファイルに直接定義する場合は以下となります。

digdag run test.dig -a

3.サンプルコード

ファイル構成

./tasks/line_notify.sh
./test.dig

テスト用のワークフローdigファイル
実行時(digdag run)に-pオプションでトークンを指定しない場合は、
以下_export:の# accese_token: "YOUR_LINES_ACCESE_TOKEN"のコメントを外して、あなたのトークンに書き換えてください。

なお、このサンプルはあくまでテスト用で意味はありませんが、メッセージの変数定義(_export:)や、間のタスクの内容を少し変えれば応用できるのはと思います。

test.dig
timezone: Asia/Tokyo

_export:
# When you do not want to use the -p option at the run, you exclude the following comment out, and please rewrite the your token
#  accese_token: "YOUR_LINES_ACCESE_TOKEN"
  workflow_name: "test"
  start_msg:     "digdag ${workflow_name} start"
  end_msg:       "digdag ${workflow_name} finish"
  error_msg:     "digdag ${workflow_name} error"

+strat:
  sh>: tasks/line_notify.sh ${accese_token} "${moment().format("YYYY-MM-DD HH:mm:ss Z")}:${start_msg}"

+repeat:
  for_each>:
    order: [1st, 2nd, 3rd, 4th, 5th]
    animal: [dog, cat, mouse]
  _do:
    echo>: ${order} ${animal}
  _parallel: true

# This is for _error confirmation.
#+fail:
#  fail>: "fail!"

+end:
  sh>: tasks/line_notify.sh ${accese_token} "${moment().format("YYYY-MM-DD HH:mm:ss Z")}:${end_msg}"

_error:
  sh>: tasks/line_notify.sh ${accese_token} "${moment().format("YYYY-MM-DD HH:mm:ss Z")}:${error_msg}"

LINE Notifyを呼ぶ簡単なシェルスクリプト
こちらで投稿したものと同じものです

tasks/line_notify.sh
#!/bin/sh

ACCESS_TOKEN=$1
MSG=$2

curl -X POST -H "Authorization: Bearer $ACCESS_TOKEN" -F "message=$MSG" https://notify-api.line.me/api/notify

4.参考 実行結果

LINE側
line_notify.png

Digdag側の標準出力

# digdag run test.dig -a -p accese_token=YOUR_LINES_ACCESE_TOKEN
2016-10-14 15:38:15 +0900: Digdag v0.8.17
2016-10-14 15:38:17 +0900 [WARN] (main): Reusing the last session time 2016-10-14T00:00:00+09:00.
2016-10-14 15:38:17 +0900 [INFO] (main): Using session /CURRENT_DIR/.digdag/status/20161014T000000+0900.
2016-10-14 15:38:17 +0900 [INFO] (main): Starting a new session project id=1 workflow name=test session_time=2016-10-14T00:00:00+09:00
2016-10-14 15:38:20 +0900 [INFO] (0016@+test+strat): sh>: tasks/line_notify.sh YOUR_LINES_ACCESE_TOKEN "2016-10-14 15:38:19 +09:00:digdag test start"
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
107   215    0    29  186   186     49    315 --:--:-- --:--:-- --:--:--  5636
{"status":200,"message":"ok"}2016-10-14 15:38:21 +0900 [INFO] (0016@+test+repeat): for_each>: {order=[1st, 2nd, 3rd, 4th, 5th], animal=[dog, cat, mouse]}
2016-10-14 15:38:26 +0900 [INFO] (0021@+test+repeat^sub+for-order=2nd&animal=dog): echo>: 2nd dog
2nd dog
2016-10-14 15:38:26 +0900 [INFO] (0020@+test+repeat^sub+for-order=1st&animal=mouse): echo>: 1st mouse
1st mouse
2016-10-14 15:38:26 +0900 [INFO] (0022@+test+repeat^sub+for-order=2nd&animal=cat): echo>: 2nd cat
2nd cat
2016-10-14 15:38:26 +0900 [INFO] (0019@+test+repeat^sub+for-order=1st&animal=cat): echo>: 1st cat
1st cat
2016-10-14 15:38:26 +0900 [INFO] (0016@+test+repeat^sub+for-order=1st&animal=dog): echo>: 1st dog
1st dog
2016-10-14 15:38:27 +0900 [INFO] (0023@+test+repeat^sub+for-order=2nd&animal=mouse): echo>: 2nd mouse
2nd mouse
2016-10-14 15:38:27 +0900 [INFO] (0029@+test+repeat^sub+for-order=4th&animal=mouse): echo>: 4th mouse
4th mouse
2016-10-14 15:38:27 +0900 [INFO] (0024@+test+repeat^sub+for-order=3rd&animal=dog): echo>: 3rd dog
3rd dog
2016-10-14 15:38:27 +0900 [INFO] (0026@+test+repeat^sub+for-order=3rd&animal=mouse): echo>: 3rd mouse
3rd mouse
2016-10-14 15:38:27 +0900 [INFO] (0031@+test+repeat^sub+for-order=5th&animal=cat): echo>: 5th cat
5th cat
2016-10-14 15:38:27 +0900 [INFO] (0032@+test+repeat^sub+for-order=5th&animal=mouse): echo>: 5th mouse
5th mouse
2016-10-14 15:38:27 +0900 [INFO] (0025@+test+repeat^sub+for-order=3rd&animal=cat): echo>: 3rd cat
3rd cat
2016-10-14 15:38:27 +0900 [INFO] (0030@+test+repeat^sub+for-order=5th&animal=dog): echo>: 5th dog
5th dog
2016-10-14 15:38:27 +0900 [INFO] (0027@+test+repeat^sub+for-order=4th&animal=dog): echo>: 4th dog
4th dog
2016-10-14 15:38:27 +0900 [INFO] (0028@+test+repeat^sub+for-order=4th&animal=cat): echo>: 4th cat
4th cat
2016-10-14 15:38:27 +0900 [INFO] (0028@+test+end): sh>: tasks/line_notify.sh YOUR_LINES_ACCESE_TOKEN "2016-10-14 15:38:27 +09:00:digdag test finish"
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
108   216    0    29  187   187    180   1163 --:--:-- --:--:-- --:--:--  5194
{"status":200,"message":"ok"}Success. Task state is saved at /CURRENT_DIR/.digdag/status/20161014T000000+0900 directory.
  * Use --session <daily | hourly | "yyyy-MM-dd[ HH:mm:ss]"> to not reuse the last session time.
  * Use --rerun, --start +NAME, or --goal +NAME argument to rerun skipped tasks.
#
10
7
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
10
7