wskコマンドによる実行
ここで、もう少しこの作成したActionを見ていきたいと思います。この処理をCliであるwskコマンドから実行してみたいと思います。まずはコマンドを確認してみましょう。
$ wsk action list actions /atg_tokida/getWeatherNews private /atg_tokida/weather2slack private
詳細を確認します。これはgetオプションを利用します。実行すると、このweather2slackがisSequenceであることが分かりますね。そして、この処理はJavaScriptで記載されていることが分かります。
$ wsk action get /atg_tokida/weather2slack
ok: got action /atg_tokida/weather2slack
{
"annotations": [
{
"key": "isSequence",
"value": true
}
],
"exec": {
"code": "/**\n * Invokes a sequence of actions, piping the output of each to the input of the next.\n *\n * @param _actions An array of action names to invoke.\n * @param <everything else> Passed as input to the first action.\n */\nfunction main(msg) {\n // The actions to invoke sequentially.\n var actions = msg['_actions'];\n\n if (typeof actions === 'string') {\n try {\n actions = JSON.parse(actions);\n } catch (e) {\n return whisk.error('invalid sequence of actions');\n }\n }\n\n if (!Array.isArray(actions)) {\n return whisk.error('invalid sequence of actions');\n }\n\n console.log(actions.length, 'actions to invoke:', actions);\n\n // The input to the first action.\n var input = msg;\n delete input['_actions'];\n console.log('input to first action:', JSON.stringify(input));\n invokeActions(actions, input, function(result) {\n console.log('chain ending with result', JSON.stringify(result));\n whisk.done(result);\n });\n\n return whisk.async();\n}\n\n/**\n * Invokes a sequence of actions.\n *\n * @param actions Array of action names.\n * @param input Input to the first action.\n * @param terminate Continuation to which the result from the final successful action is passed.\n */\nfunction invokeActions(actions, input, terminate) {\n if (Array.isArray(actions) && actions.length > 0) {\n var params = {\n name: actions[0],\n parameters: input,\n blocking: true,\n next: function(error, activation) {\n if (!error) {\n console.log('invoke action', actions[0]);\n console.log(' id:', activation.activationId);\n console.log(' input:', input);\n console.log(' result:', activation.result);\n actions.shift();\n invokeActions(actions, activation.result, terminate);\n } else {\n console.log('stopped chain at', actions[0], 'because of an error:', error);\n whisk.error(error);\n }\n }\n };\n whisk.invoke(params);\n } else terminate(input);\n}\n",
"kind": "nodejs:6"
},
"limits": {
"logs": 10,
"memory": 256,
"timeout": 60000
},
"name": "weather2slack",
"namespace": "atg_tokida",
"parameters": [
{
"key": "_actions",
"value": [
"/atg_tokida/getWeatherNews",
"/atg_tokida/mySlackChannel/post"
]
}
],
"publish": false,
"version": "0.0.1"
}
次に実行してみたいと思います
$ wsk action invoke --blocking --result weather2slack --param username 'USERNAME' --param password 'PASSWORD' --param latitude '"33.4"' --param longitude '"139.766247"'
{}
このように引数にパラメータを指定して起動すると、Slackに通知されています。Sequenceで繋げたActionも通常の単体のAction同様に利用できることなどが分かります。
Actionの初期値の設定
Actionの「デフォルト」の設定を付けてみたいと思います。プログラムの中で引数がない時の動作を定義してもよいかとも思いますが、OpenWhiskの機能としても定義することができます。これまでに作成したgetWeatherNewsのActionに対して初期値を付けたいと思います。プログラム中でハードコーディングしないことで他のユーザや環境でも柔軟に利用することができると思います。
wsk action update --param username 'USERNAME' --param password 'PASSWORD' --param latitude '"33.4"' --param longitude '"139.766247"' getWeatherNews
これで設定されます。従ってwsk invokeを引数なしで実行することで期待する結果を得ることができます。このように設定したことでSequenceで繋がったweather2slackも同様に引数なしで実行するなどができるようになっています。
$ wsk action invoke -r -b getWeatherNews
{
"text": "Today: Partly cloudy. Lows overnight in the low 80s.",
"type": "the weather company api"
}
$ wsk action invoke -r -b weather2slack
{}
