Skip to content

Instantly share code, notes, and snippets.

@suzuki-hoge
Last active February 21, 2018 14:57
Show Gist options
  • Save suzuki-hoge/5d369d46438e52fb4221333025384421 to your computer and use it in GitHub Desktop.
Save suzuki-hoge/5d369d46438e52fb4221333025384421 to your computer and use it in GitHub Desktop.
chunked executor (tmp)

chunked executor

まだ雑だけどそのうちもう少し整える

データが複数件書いてあるファイルパスを受け取り、それに対して任意の変換と処理を行うスクリプトがあるとする。それを複数サイズに分割して実行したい。
負荷のために分割をしたり、ひとまず先頭だけを流して結果を検証したりしたいため。

本来やりたいこと

  • 国際電話番号とステータスがあるとする
$ cat origin/origin.txt
819012340001,active
819012340002,active
819012340003,dead
819012340004,active
819012340005,active
819012340006,active
819012340007,active
819012340008,active
819012340009,dead
819012340010,active
  • 全件にパッチを当てたいとする
  • active の国際電話番号だけ形式変換をして curl を発行したい
  • ログも出す
$ python origin/patch.py origin/origin.txt
  • 結果
$ cat patched.log 
curl http://localhost/foo/bar/patch -d 'msisdn=090-1234-0001'
curl http://localhost/foo/bar/patch -d 'msisdn=090-1234-0002'
curl http://localhost/foo/bar/patch -d 'msisdn=090-1234-0004'
curl http://localhost/foo/bar/patch -d 'msisdn=090-1234-0005'
curl http://localhost/foo/bar/patch -d 'msisdn=090-1234-0006'
curl http://localhost/foo/bar/patch -d 'msisdn=090-1234-0007'
curl http://localhost/foo/bar/patch -d 'msisdn=090-1234-0008'
curl http://localhost/foo/bar/patch -d 'msisdn=090-1234-0010'

これを分割実行をしたい

chunked_executor を作った

  • help
$ chunked_executor -h                                                                                                  
usage: chunked_executor [-h] [--chunk CHUNK] [--command COMMAND] [--origin ORIGIN] [--log LOG]

execute shell command step by chunked.

optional arguments:
  -h, --help         show this help message and exit
  --chunk CHUNK      chunk size.
  --command COMMAND  execute by current shell, replace the path argument with $chunked.
  --origin ORIGIN    origin input text.
  --log LOG          chunked log path.
  • 引数のファイルパスの部分を任意の件数になる様に分割した一時ファイルに置き換えてループする
  • 4件ずつに分割、もとのコマンド(パス引数は$chunkedとする)、全件のファイルパス、ログパス
  • 途中で続行を求められる
$ chunked_executor --chunk 4 --command 'python origin/patch.py $chunked' --origin origin/origin.txt --log ./chunked.log

command: "python origin/patch.py tmp_chunked_0_to_4". execute? [y/n]:  y

command: "python origin/patch.py tmp_chunked_4_to_8". execute? [y/n]:  y

command: "python origin/patch.py tmp_chunked_8_to_10". execute? [y/n]:  n
exit
  • どの範囲で実行したかのログが出る
$ cat chunked.log 
python origin/patch.py tmp_chunked_0_to_4
python origin/patch.py tmp_chunked_4_to_8
  • もとのスクリプトが吐いていたログ
    • 8 to 10 は実行されていない
$ cat patched.log 
curl http://localhost/foo/bar/patch -d 'msisdn=090-1234-0001'
curl http://localhost/foo/bar/patch -d 'msisdn=090-1234-0002'
curl http://localhost/foo/bar/patch -d 'msisdn=090-1234-0004'
curl http://localhost/foo/bar/patch -d 'msisdn=090-1234-0005'
curl http://localhost/foo/bar/patch -d 'msisdn=090-1234-0006'
curl http://localhost/foo/bar/patch -d 'msisdn=090-1234-0007'
curl http://localhost/foo/bar/patch -d 'msisdn=090-1234-0008'

もっとやりたいこと

  • 最初は 100 件、それで確認が取れたら 5000 件ずつ、とかやりたい
  • 今がtcshならtcsh--commandを実行したい
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment