Gosub exiting after answered call

Hello, been working on a dialplan which effective has two gosubs.

  1. Loop through some SIP destinations
  2. Write CDRs where 1 will succeed or fail

I wont get into it but yes I am forced to use chan_sip at this stage as much as I would love to just be using pjsip here.

At the line
same => n,Dial(SIP/${DIALED_EXTEN}@${SRV_HOSTNAME},120,o)
IF the call is answered, when I hangup i receive the below and it does not proceed in the dialplan any further

Spawn extension (srv-lookup-sub, s, 25) exited non-zero on ‘SIP/TestSoftphone-0000bb46’

IF the call loops through failures like CHANNELUNAVAIL, the CDRs are written on all attempts as expected in the [write-cdr-sub]

[internal]
exten => _11XX,1,NoOp(Start manual SRV lookup)
 same => n,Gosub(srv-lookup-sub,s,1(${EXTEN}))
 same => n,NoOp(Returned from gosub)
 same => n,Hangup()

;Hangup Rules for internal context
exten => h,1,NoOp("Call Duration: ${CDR(billsec)}")
 same => n,Gosub(write-cdr-sub,s,1)

;gosub1
[srv-lookup-sub]
exten => s,1,Verbose("Starting SRV Subrouting for ${SRV_RECORD}")
same => n,Set(SRVQUERY_RESULT=${SRVQUERY(${SRV_RECORD})})
 same => n,Set(SRV_RECORD_COUNT=${SRVRESULT(${SRVQUERY_RESULT},getnum)})
 same => n,Set(SRV_COUNT=1)
;Build a list of SRV Records and put in in priority order - I am not considering weight
 same => n,While($[${SRV_COUNT} <= ${SRV_RECORD_COUNT}])
   same => n,Set(SRV_PRIORITY=${SRVRESULT(${SRVQUERY_RESULT},${SRV_COUNT},priority)})
   same => n,Set(SRV_HOST=${SRVRESULT(${SRVQUERY_RESULT},${SRV_COUNT},host)})

   ; Debug message to verify SRV record details
   same => n,NoOp(SRV Record ${SRV_COUNT}:${SRV_HOST} with priority ${SRV_PRIORITY})

   same => n,Set(SRV_KEY=${SRV_HOST}:${SRV_PRIORITY})
   same => n,NoOp(SRV_KEY ${SRV_COUNT} is ${SRV_KEY})
   ; Add priority as a key to the list in a specific format for future sorting
   same => n,Set(PRIORITY_LIST="${PRIORITY_LIST},${SRV_KEY}")

   same => n,Set(SRV_COUNT=$[${SRV_COUNT} + 1])
 same => n,EndWhile()

 same => n,Set(PRIORITY_LIST_SORTED=${SORT(${PRIORITY_LIST})})
 same => n,NoOp("SRV List Sorted: ${PRIORITY_LIST_SORTED}")

 same => n,Set(COUNT=1)

 same => n,While($[${COUNT} <= ${SRV_RECORD_COUNT}])
   same => n,Set(SRV_HOSTNAME=${CUT(PRIORITY_LIST_SORTED,",",${COUNT})})
   same => n,NoOp(Dialing ${COUNT}: ${SRV_HOSTNAME})
   same => n,Dial(SIP/${DIALED_EXTEN}@${SRV_HOSTNAME},120,o)
   same => n,GotoIf($["${DIALSTATUS}" = "ANSWER"]?success,1)
   same => n,GotoIf($[${COUNT} = ${SRV_RECORD_COUNT}]?exit_loop)
   same => n,NoOp(Call failed to ${DIALED_EXTEN}, writing CDR and moving to next priority)
   same => n,Gosub(write-cdr-sub,s,1)
   same => n,Set(COUNT=$[${COUNT} + 1])
 same => n,EndWhile()
 same => n(exit_loop),NoOp(Exiting loop as all attempts failed)
 same => n,Return()

exten => success,1,NoOp(Call successfully connected to ${DIALED_EXTEN}@${SRV_HOSTNAME})
 same => n,Return()

;gosub2
[write-cdr-sub]
exten => s,1,Verbose("Call Duration: ${CDR(billsec)}")
;write some cdrs
 same => n,Return()

That’s how dialplan works. Dial() stops execution in the successful case like that. If you truly want it to continue if the CALLED channel hangs up then you could us the ‘g’ option[1], otherwise people use hangup handlers[2] to execute logic upon hangup.

[1] Dial - Asterisk Documentation
[2] Hangup Handlers - Asterisk Documentation

Hey,

Thanks, thanks I tried the g but no luck.
I think the problem is around hanging up within a gosub. I have changed it around a bit so the Dialing and hangups are within [internal] which does what I was expecting

[internal]
exten => _11XX,1,NoOp(Start manual SRV lookup)
same => n,Gosub(srv-lookup-sub,s,1)
same => n,NoOp(Returned from gosub)
same => n,Gosub(srv-dial-sub,s,1(${EXTEN}))
same => n,Set(COUNT=1)
same => n,While($[${COUNT} <= ${SRV_RECORD_COUNT}])
same => n,Set(SRV_HOSTNAME=${CUT(PRIORITY_LIST_SORTED,“,”,${COUNT})})
same => n,NoOp(Dialing ${COUNT}: ${SRV_HOSTNAME} SRV_RECORD_COUNT is ${SRV_RECORD_COUNT})
same => n,Dial(SIP/${DIALED_EXTEN}@${SRV_HOSTNAME},120,o)
same => n,GotoIf($[“${DIALSTATUS}” = “ANSWER”]?success,1)
same => n,GotoIf($[${COUNT} = ${SRV_RECORD_COUNT}]?exit_loop)
same => n,NoOp(Call failed to ${DIALED_EXTEN}, writing CDR and moving to next priority)
same => n,Gosub(write-cdr-sub,s,1)
same => n,Set(COUNT=$[${COUNT} + 1])
same => n,EndWhile()
same => n(exit_loop),NoOp(Exiting loop as all attempts failed)
same => n,Hangup()

exten => success,1,NoOp(Call successfully connected to ${DIALED_EXTEN}@${SRV_HOSTNAME})
same => n,Hangup()

;Hangup Rules for internal context
exten => h,1,NoOp(“Call Duration: ${CDR(billsec)}”)
same => n,Gosub(write-cdr-sub,s,1)

Logs
– Executing [145009@internal:7] Set(“SIP/TestSoftphone-0000bb8a”, “SRV_HOSTNAME=pbx.company.com”) in new stack
– Executing [145009@internal:8] NoOp(“SIP/TestSoftphone-0000bb8a”, “Dialing 1: pbx.company.com SRV_RECORD_COUNT is 2”) in new stack
– Executing [1100@internal:9] Dial(“SIP/TestSoftphone-0000bb8a”, “SIP/1100@pbx.company.com,120,o”) in new stack

– Channel SIP/TestSoftphone-0000bb8a left ‘native_rtp’ basic-bridge <623c9154-706a-484a-8b20-d7fce6617ff8>
– Channel SIP/pbx.company.com-0000bb8b left ‘native_rtp’ basic-bridge <623c9154-706a-484a-8b20-d7fce6617ff8>
== Spawn extension (internal, 1100, 9) exited non-zero on ‘SIP/TestSoftphone-0000bb8a’
– Executing [h@internal:1] NoOp(“SIP/TestSoftphone-0000bb8a”, "“Call Duration: 1"”) in new stack
– Executing [h@internal:2] Gosub(“SIP/TestSoftphone-0000bb8a”, “write-cdr-sub,s,1”) in new stack
– Executing [s@write-cdr-sub:1] Verbose(“SIP/TestSoftphone-0000bb8a”, "“Call Duration: 1"”) in new stack

This is why people use hangup handlers, because they follow the channel such that you don’t have to figure out where the “h” extension should go.

ok i have never used hangup handlers, I will have a try tomorrow. I did try break it out further and use the “h” to return back to the original extension. Ran into the exact same problem however.

Do you think a hangup handler will allow me to achieve what I am after, or am I trying to do something which is simply not supported by hanging up in a gosub, to return and call another gosub

[internal]
exten => _11XX,1,NoOp(Start manual SRV lookup)
 same => n,Gosub(srv-lookup-sub,s,1)
 same => n,NoOp(Returned from gosub)
 same => n,Gosub(srv-dial-sub,s,1(${EXTEN}))

;Hangup Rules for interpbx context
exten => h,1,NoOp("Call Duration: ${CDR(billsec)}")
same => n,Gosub(write-cdr-sub,s,1)

[srv-dial-sub]
 exten => s,1,Verbose(Starting SRV Dialing subrouting)
 same => n,Set(DIALED_EXTEN=${ARG1}) ; Capture the passed extension
 same => n,Set(COUNT=1)

 same => n,While($[${COUNT} <= ${SRV_RECORD_COUNT}])
   same => n,Set(SRV_HOSTNAME=${CUT(PRIORITY_LIST_SORTED,",",${COUNT})})
   same => n,NoOp(Dialing ${COUNT}: ${SRV_HOSTNAME} SRV_RECORD_COUNT is ${SRV_RECORD_COUNT})
   same => n,Dial(SIP/${DIALED_EXTEN}@${SRV_HOSTNAME},120,o)
   same => n,GotoIf($["${DIALSTATUS}" = "ANSWER"]?success,1)
   same => n,GotoIf($[${COUNT} = ${SRV_RECORD_COUNT}]?exit_loop)
   same => n,NoOp(Call failed to ${DIALED_EXTEN}, writing CDR and moving to next priority)
   same => n,Gosub(write-cdr-sub,s,1)
   same => n,Set(COUNT=$[${COUNT} + 1])
 same => n,EndWhile()
 same => n(exit_loop),NoOp(Exiting loop as all attempts failed)
 same => n,Return()

exten => success,1,NoOp(Call successfully connected to ${DIALED_EXTEN}@${SRV_HOSTNAME})
 same => n,Return()

exten => h,1,NoOp("Call Duration: ${CDR(billsec)}")
 same => n,Return()

I would not expect returning like that to work. Once hung up, existing dialplan execution should be considered stopped. Hangup handlers/h extension effectively start new execution.

Hi,
Thankyou very much @jcolp
With this knowledge inhand putting the hangup within the Gosub that is doing the dialing is working perfectly

[internal]
exten => _11XX,1,NoOp(Start manual SRV lookup)
 same => n,Set(SRV_RECORD=_sip._tcp.sbc.company.com)
 same => n,Gosub(srv-lookup-sub,s,1(${EXTEN}))
 same => n,NoOp(Returned from gosub)
 same => n,Gosub(srv-dial-sub,s,1(${EXTEN}))

[srv-lookup-sub]
exten => s,1,Verbose("Do Things")
 same => n,Return()

[write-cdr-sub]
exten => s,1,Verbose("Write CDRs")
 same => n,Return()

[srv-dial-sub]
exten => s,1,Verbose("Loop and dial, break on success")
 same => n,While($[${COUNT} <= ${SRV_RECORD_COUNT}])
  same => n,Dial(SIP/${x}@${y},120,o)
  same => n,Set(COUNT=$[${COUNT} + 1])
 same => n,EndWhile()
 same => n,Hangup()

exten => h,1,NoOp("Call Duration: ${CDR(billsec)}")
 same => n,Gosub(write-cdr-sub,s,1)

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.