@@ -162,6 +162,7 @@ const main = (() => {
162162 params . $invoke = result . $fsm
163163 params . $state = result . $state
164164 params . $stack = result . $stack
165+ params . $callee = result . $callee
165166 } )
166167 }
167168
@@ -176,9 +177,9 @@ const main = (() => {
176177 }
177178
178179 // persist session state to redis
179- function persist ( $fsm , $state , $stack ) {
180+ function persist ( $fsm , $state , $stack , $callee ) {
180181 // ensure using set-if-exists that the session has not been killed
181- return db . lsetAsync ( sessionStateKey , - 1 , JSON . stringify ( { $fsm, $state, $stack } ) )
182+ return db . lsetAsync ( sessionStateKey , - 1 , JSON . stringify ( { $fsm, $state, $stack, $callee } ) )
182183 . catch ( ( ) => gone ( `Session ${ session } has been killed` ) )
183184 }
184185
@@ -204,6 +205,7 @@ const main = (() => {
204205
205206 let state = resuming ? params . $state : ( params . $state || fsm . Entry )
206207 const stack = params . $stack || [ ]
208+ const callee = params . $callee
207209
208210 // wrap params if not a JSON object, branch to error handler if error
209211 function inspect ( ) {
@@ -229,6 +231,7 @@ const main = (() => {
229231 delete params . $sessionId
230232 delete params . $state
231233 delete params . $stack
234+ delete params . $callee
232235 delete params . $blocking
233236
234237 // run function f on current stack
@@ -251,7 +254,12 @@ const main = (() => {
251254 if ( ! state ) {
252255 console . log ( `Entering final state` )
253256 console . log ( JSON . stringify ( params ) )
254- return record ( params ) . then ( ( ) => blocking ? params : ( { $session : session } ) )
257+ return record ( params ) . then ( ( ) => {
258+ if ( callee ) {
259+ return wsk . actions . invoke ( { name : callee . name , params : { $sessionId : callee . session , $result : params } } )
260+ . catch ( error => badRequest ( `Failed to return to callee: ${ encodeError ( error ) . error } ` ) )
261+ }
262+ } ) . then ( ( ) => blocking ? params : ( { $session : session } ) )
255263 }
256264
257265 console . log ( `Entering ${ state } ` )
@@ -300,9 +308,16 @@ const main = (() => {
300308 if ( typeof stack . shift ( ) . let !== 'object' ) return badRequest ( `The state named ${ current } of type End popped an unexpected stack element` )
301309 break
302310 case 'Task' :
303- if ( typeof json . Action === 'string' ) { // invoke user action
311+ if ( typeof json . Action === 'string' && json . Action . substr ( json . Action . length - 4 ) === '.app' ) { // invoke app
312+ params . $callee = { name : process . env . __OW_ACTION_NAME , session }
313+ return persist ( fsm , state , stack , callee )
314+ . then ( ( ) => wsk . actions . invoke ( { name : json . Action , params } )
315+ . catch ( error => badRequest ( `Failed to invoke app ${ json . Action } : ${ encodeError ( error ) . error } ` ) ) )
316+ . then ( activation => db . rpushxAsync ( sessionTraceKey , activation . activationId ) )
317+ . then ( ( ) => blocking ? getSessionResult ( ) : { $session : session } )
318+ } else if ( typeof json . Action === 'string' ) { // invoke user action
304319 const invocation = notify ? { name : json . Action , params, blocking : true } : { name : json . Action , params, notify : process . env . __OW_ACTION_NAME , cause : session }
305- return persist ( fsm , state , stack )
320+ return persist ( fsm , state , stack , callee )
306321 . then ( ( ) => wsk . actions . invoke ( invocation )
307322 . catch ( error => error . error && error . error . response ? error . error : badRequest ( `Failed to invoke action ${ json . Action } : ${ encodeError ( error ) . error } ` ) ) ) // catch error reponses
308323 . then ( activation => db . rpushxAsync ( sessionTraceKey , activation . activationId )
0 commit comments