Advertisement
Guest User

Untitled

a guest
Mar 4th, 2015
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.20 KB | None | 0 0
  1. macro sm_agi(agi,agi_failsafe) {
  2.  
  3. // extract script name from primary agi for the global breaker variable name
  4. Set(LOCAL(breaker)=sm_agi_breaker-${CUT(CUT(agi,/,${FIELDQTY(agi,/)}),^,1)});
  5. Set(SM_AGI_STATUS=);
  6.  
  7. if ("${GLOBAL(${breaker})}" == "tripped") {
  8. // globally lock breaker variable to prevent race condition
  9. if (${TRYLOCK(${breaker})}) {
  10. Noop(AGI Breaker '${breaker}' is currently 'tripped' Checking elapsed time and elapsed calls.);
  11. if (${${breaker}_calls} > ${SM_AGI_BREAKER_MAX_CALLS} || ${MATH(${STRFTIME(,,%s)}-${${breaker}_timestamp},int)} > ${SM_AGI_BREAKER_MAX_ELAPSED}) {
  12. System(/bin/echo "AGI breaker '${breaker}' reset on '${SM_NODE}' after '${MATH(${STRFTIME(,,%s)}-${${breaker}_timestamp},int)}' seconds and '${${breaker}_calls}' calls. Normal call processing has resumed." | mail -s "NOTICE: ${SM_NODE} AGI Breaker '${breaker}' Reset" ${SM_AGI_BREAKER_NOTIFY});
  13. Set(ARRAY(GLOBAL(${breaker}),GLOBAL(${breaker}_calls),GLOBAL(${breaker}_elapsed))=,,,);
  14. }
  15. Set(undef=${UNLOCK(${breaker})});
  16. }
  17. }
  18. if ("${GLOBAL(${breaker})}" != "tripped") {
  19.  
  20. // run agi, replacing ^ with ,
  21. AGI(${REPLACE(agi,^,\,)});
  22.  
  23. // the agi should set SM_AGI_STATUS to "FAIL" when it starts and set it to "SUCCESS" just before the AGI exits. This is because if the AGI fails it won't be able to set the SM_AGI_STATUS variable.
  24. if ("${SM_AGI_STATUS}" == "SUCCESS") {
  25. return;
  26. }
  27. if ("${SM_AGI_STATUS}" != "FAIL" && "${AGISTATUS}" == "SUCCESS") {
  28. Set(SM_AGI_STATUS=SUCCESS);
  29. return;
  30. }
  31.  
  32. // agi failed, trip the circuit breaker
  33. if (${TRYLOCK(${breaker})}) {
  34. Set(ARRAY(GLOBAL(${breaker}),GLOBAL(${breaker}_calls),GLOBAL(${breaker}_timestamp))=tripped,1,${STRFTIME(,,%s)});
  35. Set(undef=${UNLOCK(${breaker})});
  36. }
  37.  
  38. System(/bin/echo "AGI '${agi}' failed on channel '${CHANNEL(name)}' with status '${AGISTATUS}' on '${SM_NODE}', tripping AGI breaker '${breaker}'. Failsafe mode enabled, AGI breaker will reset after '${SM_AGI_BREAKER_MAX_ELAPSED}' seconds or '${SM_AGI_BREAKER_MAX_CALLS}' calls" | mail -s "ERROR: ${SM_NODE} AGI Breaker '${breaker}' Tripped" ${SM_AGI_BREAKER_NOTIFY});
  39.  
  40. Playback(custom/sm_agi_fail);
  41.  
  42. }
  43.  
  44. // try using the failsafe
  45. AGI(${REPLACE(agi_failsafe,^,\,)});
  46. if ("${AGISTATUS}" == "SUCCESS") {
  47. if (${LOCK(${breaker})}) {
  48. Set(ARRAY(GLOBAL(${breaker}_calls),SM_AGI_STATUS)=${MATH(${${breaker}_calls}+1,int)},SUCCESS);
  49. Set(undef=${UNLOCK(${breaker})});
  50. }
  51. return;
  52. }
  53.  
  54. System(/bin/echo "Backup AGI '${agi_failsafe}' failed with status '${AGISTATUS}' on '${SM_NODE}'. This is a critical emergency. All calls on this node are failing. Drop whatever you are doing and deal with it." | mail -s "PANIC: ${SM_NODE} AGI FAILED" ${SM_AGI_BREAKER_NOTIFY});
  55.  
  56. // both agi and failsafe agi failed. reset the agi breaker for the next call and hope for the best.
  57. Set(ARRAY(GLOBAL(${breaker}),GLOBAL(${breaker}_calls),GLOBAL(${breaker}_timestamp))=,,,);
  58.  
  59. return;
  60.  
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement