Invoking many OpenWhisk actions from another one
I have a project where I need to store a number of items into a data store. I have an OpenWhisk action that stores the items so I wrote an action that takes advantage of the OpenWhisk JS client library to do invoke my store action once for each item in an array that this action receives. This is the JavaScript code that I used:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
"use strict"; const openwhisk = require('openwhisk'); async function main(params) { if (!params.items) { return {error: "No items"} } const action = "my_api/store") const ow = openwhisk(); console.log(params.items.length + " items to be invoked") let actions = params.items.map(function (item) { return ow.actions.invoke({actionName: action, params: {item: item}}); }); try { const responses = await Promise.all(actions); } catch (err) { console.error('Error invoking actions', err) return Promise.reject({error: err}); } console.log(actions.length + " actions invoked") return {number_stored: actions.length} } exports.main = main; |
This is a NodeJS v8 action (–kind nodejs:8 when… continue reading.